1
1
import logging
2
2
3
- from slack_sdk import WebClient
3
+ from typing import TypeVar
4
+ from slack_sdk .web .client import WebClient
4
5
from slack_sdk .errors import SlackApiError
5
6
6
7
8
+ T = TypeVar ("T" )
9
+
10
+
7
11
class SlackClient :
8
- def __init__ (self , token ) :
12
+ def __init__ (self , token : str ) -> None :
9
13
self .client = WebClient (token )
10
14
11
- def __get_response (self , response ) :
15
+ def __get_response (self , response : T ) -> T :
12
16
if not response ["ok" ]:
13
17
raise SlackApiError (response ["error" ])
14
18
return response
15
19
16
- def get_member_id_by_channel (self , channel_id , cursor = None ):
20
+ def get_member_id_by_channel (
21
+ self , channel_id : str , cursor : None | str = None
22
+ ) -> list [str ]:
17
23
response = self .client .conversations_members (channel = channel_id , cursor = cursor )
18
24
response = self .__get_response (response )
19
25
@@ -26,7 +32,7 @@ def get_member_id_by_channel(self, channel_id, cursor=None):
26
32
member_id += self .get_member_id_by_channel (channel_id , next_cursor )
27
33
return member_id
28
34
29
- def get_reaction_members (self , channel_id , timestamp ) :
35
+ def get_reaction_members (self , channel_id : str , timestamp : str ) -> set [ str ] :
30
36
response = self .client .reactions_get (channel = channel_id , timestamp = timestamp )
31
37
response = self .__get_response (response )
32
38
@@ -42,7 +48,9 @@ def get_reaction_members(self, channel_id, timestamp):
42
48
user_id .update (reaction ["users" ])
43
49
return user_id
44
50
45
- def post_remind_message (self , channel_id , timestamp , users ):
51
+ def post_remind_message (
52
+ self , channel_id : str , timestamp : str , users : list [str ]
53
+ ) -> None :
46
54
user_list = ""
47
55
for user in users :
48
56
user_list += "<@" + user + "> "
@@ -55,7 +63,9 @@ def post_remind_message(self, channel_id, timestamp, users):
55
63
response = self .__get_response (response )
56
64
return
57
65
58
- def post_message (self , channel_id , user , timestamp , message ):
66
+ def post_message (
67
+ self , channel_id : str , user : str , timestamp : str , message : str
68
+ ) -> None :
59
69
response = self .client .chat_postMessage (
60
70
channel = channel_id ,
61
71
text = message ,
@@ -66,14 +76,69 @@ def post_message(self, channel_id, user, timestamp, message):
66
76
response = self .__get_response (response )
67
77
return
68
78
69
- def is_bot_user (self , user ) :
79
+ def is_bot_user (self , user : str ) -> bool :
70
80
response = self .client .users_info (user = user )
71
81
response = self .__get_response (response )
72
82
return response ["user" ].get ("is_bot" )
73
83
74
- def get_message_info (self , channel_id , timestamp ) :
84
+ def get_message_info (self , channel_id : str , timestamp : str ) -> str :
75
85
response = self .client .conversations_history (
76
86
channel = channel_id , oldest = timestamp , limit = 1 , inclusive = True
77
87
)
78
88
response = self .__get_response (response )
79
89
return response ["messages" ][0 ]["text" ]
90
+
91
+ def get_conversation_history (
92
+ self ,
93
+ channel_id : str ,
94
+ * ,
95
+ limit : int = 200 ,
96
+ oldest : None | str = None ,
97
+ latest : None | str = None ,
98
+ ) -> list [dict ]:
99
+ response = self .client .conversations_history (
100
+ channel = channel_id , limit = limit , oldest = oldest , latest = latest
101
+ )
102
+ return self .__get_response (response )
103
+
104
+ def get_all_conversation_histories (
105
+ self ,
106
+ channel_id : str ,
107
+ * ,
108
+ limit : int = 200 ,
109
+ oldest : None | str = None ,
110
+ latest : None | str = None ,
111
+ ) -> list [dict ]:
112
+ all_messages = []
113
+ cursor : None | str = None
114
+
115
+ while True :
116
+ response = self .client .conversations_history (
117
+ channel = channel_id ,
118
+ limit = limit ,
119
+ oldest = oldest ,
120
+ latest = latest ,
121
+ cursor = cursor ,
122
+ )
123
+
124
+ all_messages .extend (response ["messages" ])
125
+
126
+ if not (cursor := response .get ("response_metadata" , {}).get ("next_cursor" )):
127
+ break
128
+
129
+ return all_messages
130
+
131
+ def get_conversations_replies (self , channel_id : str , timestamp : str ) -> list [dict ]:
132
+ response = self .client .conversations_replies (channel = channel_id , ts = timestamp )
133
+ response = self .__get_response (response )
134
+ return response ["messages" ]
135
+
136
+ def get_conversation_members (self , channel_id : str ) -> list [str ]:
137
+ response = self .client .conversations_members (channel = channel_id , limit = 1000 )
138
+ response = self .__get_response (response )
139
+ return response ["members" ]
140
+
141
+ def get_user_name (self , user_id : str ) -> str :
142
+ response = self .client .users_info (user = user_id )
143
+ response = self .__get_response (response )
144
+ return response ["user" ]["real_name" ]
0 commit comments