Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 624f977

Browse files
authored
Union[chat1.Channel, Chat1.ConvIDStr] (#45)
* Union[chat1.Channel, Chat1.ConvIDStr] * typos * lint * isort * fmt * ignore venv * run acceptance tests
1 parent 09e75e4 commit 624f977

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ max-complexity = 18
55
select = B,C,E,F,W,T4,B9
66
per-file-ignores =
77
pykeybasebot/__init__.py:F401,F403
8-
exclude = pykeybasebot/types/
8+
exclude = pykeybasebot/types/,venv/

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__/
44
.mypy_cache/
55
test_config.yaml
66
*.egg-info
7+
venv

pykeybasebot/bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def _initialize(self):
168168
# raise an exception because we can't authenticate
169169
raise Exception(f"failed to initialize with oneshot {oneshot_result}")
170170
await self.submit(
171-
"chat notification-settings -disable-typing {self.disable_typing}"
171+
f"chat notification-settings -disable-typing={self.disable_typing}"
172172
)
173173

174174
async def teardown(self):

pykeybasebot/chat_client.py

+42-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import Dict, List, Optional
2+
from typing import Dict, List, Optional, Tuple, Union
33

44
from .errors import ChatClientError
55
from .types import chat1
@@ -9,6 +9,13 @@ class ChatClient:
99
def __init__(self, bot):
1010
self.bot = bot
1111

12+
def _channel_or_conv_id(
13+
self, channel: Union[chat1.ChatChannel, chat1.ConvIDStr]
14+
) -> Tuple[str, Union[str, dict]]:
15+
if isinstance(channel, chat1.ChatChannel):
16+
return "channel", channel.to_dict()
17+
return "conversation_id", str(channel)
18+
1219
async def list(self) -> List[chat1.ConvSummary]:
1320
"""
1421
Lists your chats, with info on which ones have unread messages.
@@ -19,45 +26,50 @@ async def list(self) -> List[chat1.ConvSummary]:
1926
return chat_list.conversations or []
2027

2128
async def read(
22-
self, channel: chat1.ChatChannel, pagination: Optional[chat1.Pagination]
29+
self,
30+
channel: Union[chat1.ChatChannel, chat1.ConvIDStr],
31+
pagination: Optional[chat1.Pagination] = None,
2332
) -> List[Optional[chat1.MsgSummary]]:
2433
"""
2534
Reads the messages in a channel.
2635
"""
2736
await self.bot.ensure_initialized()
28-
read_options = {"channel": channel.to_dict()}
37+
ch_key, ch_val = self._channel_or_conv_id(channel)
38+
read_options = {ch_key: ch_val}
2939
if pagination is not None:
3040
read_options["pagination"] = pagination.to_dict()
3141
read_request = {"method": "read", "params": {"options": read_options}}
3242
res = await self.execute(read_request)
3343
thread = chat1.Thread.from_dict(res)
3444
return [message.msg for message in (thread.messages or [])]
3545

36-
async def send(self, channel: chat1.ChatChannel, message: str) -> chat1.SendRes:
46+
async def send(
47+
self, channel: Union[chat1.ChatChannel, chat1.ConvIDStr], message: str
48+
) -> chat1.SendRes:
3749
await self.bot.ensure_initialized()
50+
ch_key, ch_val = self._channel_or_conv_id(channel)
3851
res = await self.execute(
3952
{
4053
"method": "send",
41-
"params": {
42-
"options": {
43-
"channel": channel.to_dict(),
44-
"message": {"body": message},
45-
}
46-
},
54+
"params": {"options": {ch_key: ch_val, "message": {"body": message}}},
4755
}
4856
)
4957
return chat1.SendRes.from_dict(res)
5058

5159
async def react(
52-
self, channel: chat1.ChatChannel, message_id: chat1.MessageID, reaction: str
60+
self,
61+
channel: Union[chat1.ChatChannel, chat1.ConvIDStr],
62+
message_id: chat1.MessageID,
63+
reaction: str,
5364
) -> chat1.SendRes:
5465
await self.bot.ensure_initialized()
66+
ch_key, ch_val = self._channel_or_conv_id(channel)
5567
res = await self.execute(
5668
{
5769
"method": "reaction",
5870
"params": {
5971
"options": {
60-
"channel": channel.to_dict(),
72+
ch_key: ch_val,
6173
"message_id": message_id,
6274
"message": {"body": reaction},
6375
}
@@ -67,15 +79,19 @@ async def react(
6779
return chat1.SendRes.from_dict(res)
6880

6981
async def edit(
70-
self, channel: chat1.ChatChannel, message_id: chat1.MessageID, message: str
82+
self,
83+
channel: Union[chat1.ChatChannel, chat1.ConvIDStr],
84+
message_id: chat1.MessageID,
85+
message: str,
7186
) -> chat1.SendRes:
7287
await self.bot.ensure_initialized()
88+
ch_key, ch_val = self._channel_or_conv_id(channel)
7389
res = await self.execute(
7490
{
7591
"method": "edit",
7692
"params": {
7793
"options": {
78-
"channel": channel.to_dict(),
94+
ch_key: ch_val,
7995
"message_id": message_id,
8096
"message": {"body": message},
8197
}
@@ -85,33 +101,37 @@ async def edit(
85101
return chat1.SendRes.from_dict(res)
86102

87103
async def attach(
88-
self, channel: chat1.ChatChannel, filename: str, title: str
104+
self,
105+
channel: Union[chat1.ChatChannel, chat1.ConvIDStr],
106+
filename: str,
107+
title: str,
89108
) -> chat1.SendRes:
90109
await self.bot.ensure_initialized()
110+
ch_key, ch_val = self._channel_or_conv_id(channel)
91111
res = await self.execute(
92112
{
93113
"method": "attach",
94114
"params": {
95-
"options": {
96-
"channel": channel.to_dict(),
97-
"filename": filename,
98-
"title": title,
99-
}
115+
"options": {ch_key: ch_val, "filename": filename, "title": title}
100116
},
101117
}
102118
)
103119
return chat1.SendRes.from_dict(res)
104120

105121
async def download(
106-
self, channel: chat1.ChatChannel, message_id: int, output: str
122+
self,
123+
channel: Union[chat1.ChatChannel, chat1.ConvIDStr],
124+
message_id: int,
125+
output: str,
107126
) -> chat1.SendRes:
108127
await self.bot.ensure_initialized()
128+
ch_key, ch_val = self._channel_or_conv_id(channel)
109129
res = await self.execute(
110130
{
111131
"method": "download",
112132
"params": {
113133
"options": {
114-
"channel": channel.to_dict(),
134+
ch_key: ch_val,
115135
"message_id": message_id,
116136
"output": output,
117137
}

tests/acceptance/chat_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def channel(config):
3232
)
3333

3434

35+
@pytest.fixture()
36+
def conv_id(config):
37+
return chat1.ConvIDStr(f"{config['teams']['acme']['conv_id']}")
38+
39+
3540
def noop_handler(*args, **kwargs):
3641
pass
3742

@@ -80,3 +85,11 @@ async def test_read(setup_bot, channel):
8085
messages = await bot.chat.read(channel)
8186
for message in messages:
8287
assert type(message) is chat1.MsgSummary
88+
89+
90+
@pytest.mark.asyncio
91+
async def test_read_conv_id(setup_bot, conv_id):
92+
bot = setup_bot("alice")
93+
messages = await bot.chat.read(conv_id)
94+
for message in messages:
95+
assert type(message) is chat1.MsgSummary

tests/acceptance/test_config.example.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ teams:
1414
name: "acme"
1515
# The channel to use
1616
topicname: "mysupercoolchannel"
17+
# ConvID of the channel, if you do a `list` in chat api you can find the
18+
# convID. E.g. `keybase chat api -p -m '{"method": "read", "params": {"options": {"channel": {"name":"acme", "members_type":"team", "topic_name":"mysupercoolchannel"}}}}'`
19+
conv_id: "00001bca4f13b0a7b81bfbc2acd7f8cf829bf53845ac87cdd8b62617d5aaa084"

0 commit comments

Comments
 (0)