-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_connection.py
329 lines (271 loc) · 13.5 KB
/
slack_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import pathlib
from abc import ABC, abstractmethod
from typing import Optional, List, Tuple, Union
from slack_sdk import WebClient, errors
from slack_sdk.web.async_client import AsyncWebClient
from connectify.datetime_services import current_timestamp_utc
from connectify.slack_services.enums import SlackMessageResponseEnum
from connectify.slack_services.errors import SlackNotAFutureDateException
class BaseSlackConnection(ABC):
def __init__(self, access_token: str, channel_id: str, timeout: Optional[int] = 30):
self._access_token: str = access_token
self._channel_id: str = channel_id
self._client: WebClient = WebClient(self._access_token, timeout=timeout)
@property
def channel_id(self):
return self._channel_id
@abstractmethod
def channel_conversation_history(self,
channel_id: str = None,
after_timestamp: Optional[int] = None,
before_timestamp: Optional[int] = None,
limit: int = 100
) -> Tuple[str, list]:
raise NotImplementedError
@abstractmethod
def get_channel_latest_message(self, channel_id: str = None) -> Tuple[str, dict]:
raise NotImplementedError
@abstractmethod
def send_text_message(self, msg: str, channel_id: str = None) -> str:
raise NotImplementedError
@abstractmethod
def send_scheduled_text_message(self, msg: str, timestamp: int, channel_id: str = None) -> str:
raise NotImplementedError
@abstractmethod
def send_file_message(self,
file_path: Union[str, pathlib.Path],
title: Optional[str] = None,
msg: Optional[str] = None,
channel_id: Optional[str] = None) -> str:
raise NotImplementedError
class SlackConnection(BaseSlackConnection):
def __init__(self, access_token: str, channel_id: str, timeout: Optional[int] = 30):
super().__init__(access_token=access_token, channel_id=channel_id, timeout=timeout)
def channel_conversation_history(self,
channel_id: str = None,
after_timestamp: Optional[int] = None,
before_timestamp: Optional[int] = None,
limit: int = 100
) -> Tuple[str, list]:
"""
Get channel conversion history for given channel
:param channel_id: specific channel id or initialized one
:param after_timestamp: only messages after this timestamp will be fetched, default to 1 minute before
:param before_timestamp: only messages before this timestamp will be fetched
:param limit: number of messages to read
:return: status, messages list
"""
channel_id = channel_id or self.channel_id
before_timestamp = str(before_timestamp) if before_timestamp else None
after_timestamp = str(after_timestamp) if after_timestamp else None
try:
messages: List[dict] = list()
cursor = None
while True:
response = self._client.conversations_history(channel=channel_id,
cursor=cursor,
inclusive=True,
latest=before_timestamp,
oldest=after_timestamp,
limit=100)
if response.status_code != 200:
raise errors.SlackApiError
cursor_messages = response.data.get("messages", [])
if not cursor_messages:
break
messages.extend(cursor_messages)
cursor = response.data.get("response_metadata", {}).get("next_cursor")
if not cursor:
break
if len(messages) > limit:
break
return SlackMessageResponseEnum.SUCCESS, messages[: limit]
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR, []
def get_channel_latest_message(self, channel_id: str = None) -> Tuple[str, dict]:
"""
Fetch latest message from specific channel.
:param channel_id: channel or initialized id
:return: status, message tuple
"""
status, messages = self.channel_conversation_history(channel_id, limit=1)
if status == SlackMessageResponseEnum.ERROR:
return status, {}
if not messages:
return status, {}
return status, messages[0]
def send_text_message(self, msg: str, channel_id: str = None) -> str:
"""
Send text type message to specific channel
:param msg: message to send
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
try:
response = self._client.chat_postMessage(channel=channel_id, text=msg)
if response["ok"]:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError:
return SlackMessageResponseEnum.ERROR
def send_scheduled_text_message(self, msg: str, timestamp: int, channel_id: str = None) -> str:
"""
Schedule text message send for specific time
:param msg: message to send
:param timestamp: unix epoch timestamp UTX, should be at least 15 seconds after current time
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
# slack api doesn't work for time closer to 10 seconds
if timestamp <= current_timestamp_utc() + 10:
raise SlackNotAFutureDateException
try:
response = self._client.chat_scheduleMessage(channel=channel_id, post_at=timestamp, text=msg)
if response:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR
def send_file_message(self,
file_path: Union[str, pathlib.Path],
title: Optional[str] = None,
msg: Optional[str] = None,
channel_id: Optional[str] = None) -> str:
"""
Send file to channel
:param file_path: local path to file.
:param title: file title
:param msg: text message to send along with file
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
try:
response = self._client.files_upload_v2(filename=pathlib.Path(file_path).name,
file=str(file_path),
channel=channel_id,
title=title,
initial_comment=msg)
if response["ok"]:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR
class AsyncSlackConnection(BaseSlackConnection):
def __init__(self, access_token: str, channel_id: str, timeout: Optional[int] = 30):
super().__init__(access_token=access_token, channel_id=channel_id, timeout=timeout)
self._client = AsyncWebClient(token=self._access_token, timeout=timeout)
async def channel_conversation_history(self,
channel_id: str = None,
after_timestamp: Optional[int] = None,
before_timestamp: Optional[int] = None,
limit: int = 100
) -> Tuple[str, list]:
"""
Get channel conversion history for given channel
:param channel_id: specific channel id or initialized one
:param after_timestamp: only messages after this timestamp will be fetched, default to 1 minute before
:param before_timestamp: only messages before this timestamp will be fetched
:param limit: number of messages to read
:return: status, messages list
"""
channel_id = channel_id or self.channel_id
before_timestamp = str(before_timestamp) if before_timestamp else None
after_timestamp = str(after_timestamp) if after_timestamp else None
try:
messages: List[dict] = list()
cursor = None
while True:
response = await self._client.conversations_history(channel=channel_id,
cursor=cursor,
inclusive=True,
latest=before_timestamp,
oldest=after_timestamp,
limit=100)
if response.status_code != 200:
raise errors.SlackApiError
cursor_messages = response.data.get("messages", [])
if not cursor_messages:
break
messages.extend(cursor_messages)
cursor = response.data.get("response_metadata", {}).get("next_cursor")
if not cursor:
break
if len(messages) > limit:
break
return SlackMessageResponseEnum.SUCCESS, messages[: limit]
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR, []
async def get_channel_latest_message(self, channel_id: str = None) -> Tuple[str, dict]:
"""
Fetch latest message from specific channel.
:param channel_id: channel or initialized id
:return: status, message tuple
"""
status, messages = await self.channel_conversation_history(channel_id, limit=1)
if status == SlackMessageResponseEnum.ERROR:
return status, {}
if not messages:
return status, {}
return status, messages[0]
async def send_text_message(self, msg: str, channel_id: str = None) -> str:
"""
Send text type message to specific channel
:param msg: message to send
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
try:
response = await self._client.chat_postMessage(channel=channel_id, text=msg)
if response["ok"]:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError:
return SlackMessageResponseEnum.ERROR
async def send_scheduled_text_message(self, msg: str, timestamp: int, channel_id: str = None) -> str:
"""
Schedule text message send for specific time
:param msg: message to send
:param timestamp: unix epoch timestamp UTX, should be at least 15 seconds after current time
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
# slack api doesn't work for time closer to 10 seconds
if timestamp <= current_timestamp_utc() + 10:
raise SlackNotAFutureDateException
try:
response = await self._client.chat_scheduleMessage(channel=channel_id, post_at=timestamp, text=msg)
if response:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR
async def send_file_message(self,
file_path: Union[str, pathlib.Path],
title: Optional[str] = None,
msg: Optional[str] = None,
channel_id: Optional[str] = None) -> str:
"""
Send file to channel
:param file_path: local path to file.
:param title: file title
:param msg: text message to send along with file
:param channel_id: specific channel id or initialized one
:return: status
"""
channel_id = channel_id or self.channel_id
try:
response = await self._client.files_upload_v2(filename=pathlib.Path(file_path).name,
file=str(file_path),
channel=channel_id,
title=title,
initial_comment=msg)
if response["ok"]:
return SlackMessageResponseEnum.SUCCESS
raise errors.SlackApiError
except errors.SlackApiError as e:
return SlackMessageResponseEnum.ERROR