1
1
import json
2
- from typing import Dict , List , Optional
2
+ from typing import Dict , List , Optional , Tuple , Union
3
3
4
4
from .errors import ChatClientError
5
5
from .types import chat1
@@ -9,6 +9,13 @@ class ChatClient:
9
9
def __init__ (self , bot ):
10
10
self .bot = bot
11
11
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
+
12
19
async def list (self ) -> List [chat1 .ConvSummary ]:
13
20
"""
14
21
Lists your chats, with info on which ones have unread messages.
@@ -19,45 +26,50 @@ async def list(self) -> List[chat1.ConvSummary]:
19
26
return chat_list .conversations or []
20
27
21
28
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 ,
23
32
) -> List [Optional [chat1 .MsgSummary ]]:
24
33
"""
25
34
Reads the messages in a channel.
26
35
"""
27
36
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 }
29
39
if pagination is not None :
30
40
read_options ["pagination" ] = pagination .to_dict ()
31
41
read_request = {"method" : "read" , "params" : {"options" : read_options }}
32
42
res = await self .execute (read_request )
33
43
thread = chat1 .Thread .from_dict (res )
34
44
return [message .msg for message in (thread .messages or [])]
35
45
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 :
37
49
await self .bot .ensure_initialized ()
50
+ ch_key , ch_val = self ._channel_or_conv_id (channel )
38
51
res = await self .execute (
39
52
{
40
53
"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 }}},
47
55
}
48
56
)
49
57
return chat1 .SendRes .from_dict (res )
50
58
51
59
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 ,
53
64
) -> chat1 .SendRes :
54
65
await self .bot .ensure_initialized ()
66
+ ch_key , ch_val = self ._channel_or_conv_id (channel )
55
67
res = await self .execute (
56
68
{
57
69
"method" : "reaction" ,
58
70
"params" : {
59
71
"options" : {
60
- "channel" : channel . to_dict () ,
72
+ ch_key : ch_val ,
61
73
"message_id" : message_id ,
62
74
"message" : {"body" : reaction },
63
75
}
@@ -67,15 +79,19 @@ async def react(
67
79
return chat1 .SendRes .from_dict (res )
68
80
69
81
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 ,
71
86
) -> chat1 .SendRes :
72
87
await self .bot .ensure_initialized ()
88
+ ch_key , ch_val = self ._channel_or_conv_id (channel )
73
89
res = await self .execute (
74
90
{
75
91
"method" : "edit" ,
76
92
"params" : {
77
93
"options" : {
78
- "channel" : channel . to_dict () ,
94
+ ch_key : ch_val ,
79
95
"message_id" : message_id ,
80
96
"message" : {"body" : message },
81
97
}
@@ -85,33 +101,37 @@ async def edit(
85
101
return chat1 .SendRes .from_dict (res )
86
102
87
103
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 ,
89
108
) -> chat1 .SendRes :
90
109
await self .bot .ensure_initialized ()
110
+ ch_key , ch_val = self ._channel_or_conv_id (channel )
91
111
res = await self .execute (
92
112
{
93
113
"method" : "attach" ,
94
114
"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 }
100
116
},
101
117
}
102
118
)
103
119
return chat1 .SendRes .from_dict (res )
104
120
105
121
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 ,
107
126
) -> chat1 .SendRes :
108
127
await self .bot .ensure_initialized ()
128
+ ch_key , ch_val = self ._channel_or_conv_id (channel )
109
129
res = await self .execute (
110
130
{
111
131
"method" : "download" ,
112
132
"params" : {
113
133
"options" : {
114
- "channel" : channel . to_dict () ,
134
+ ch_key : ch_val ,
115
135
"message_id" : message_id ,
116
136
"output" : output ,
117
137
}
0 commit comments