From d0897b6c1b91495db572e16565d647b461447d87 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Tue, 14 Nov 2023 19:10:30 +0100 Subject: [PATCH 1/3] Do not crash when a message can't be sent --- irc.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/irc.py b/irc.py index 4f7c18d..6cc709f 100644 --- a/irc.py +++ b/irc.py @@ -67,6 +67,7 @@ class Replies(Enum): RPL_ENDOFNAMES = 366 ERR_NOSUCHNICK = 401 ERR_NOSUCHCHANNEL = 403 + ERR_CANNOTSENDTOCHAN = 404 ERR_UNKNOWNCOMMAND = 421 ERR_FILEERROR = 424 ERR_ERRONEUSNICKNAME = 432 @@ -297,20 +298,23 @@ async def send_slack_message(self, dest: bytes, msg: bytes, action: bool, re_sen message = await self._addmagic(msg.decode('utf8'), dest_object) - if isinstance(dest_object, slack.User): - await self.sl_client.send_message_to_user( - dest_object, - message, - action, - re_send_to_irc, - ) - else: - await self.sl_client.send_message( - dest_object, - message, - action, - re_send_to_irc - ) + try: + if isinstance(dest_object, slack.User): + await self.sl_client.send_message_to_user( + dest_object, + message, + action, + re_send_to_irc, + ) + else: + await self.sl_client.send_message( + dest_object, + message, + action, + re_send_to_irc + ) + except Exception as e: + await self._sendreply(Replies.ERR_CANNOTSENDTOCHAN, f'Message sending failed: {e}') async def _listhandler(self, cmd: bytes) -> None: for c in await self.sl_client.channels(refresh=True): From e263c94aa5b37d9b8aad01a147f278ede642276d Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Tue, 14 Nov 2023 19:10:48 +0100 Subject: [PATCH 2/3] CHANGELOG --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 693795a..b89a5ce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ is not needed. Thanks to: Jiri Bohac * Ignore chats with deleted users when downloading history +* Remove calls to removed API calls 1.21 * Introduce lsi-send to send files from the shell From 053592db8973e453dae63f7090ee200c3a9ccdf4 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Tue, 14 Nov 2023 19:27:17 +0100 Subject: [PATCH 3/3] Use the new API to open a /query --- slack.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/slack.py b/slack.py index 9941960..4c526b6 100644 --- a/slack.py +++ b/slack.py @@ -871,25 +871,18 @@ async def send_message_to_user(self, user: User, msg: str, action: bool, re_send # channel id is cached channel_id = self._imcache[user.id] else: - # Find the channel id - found = False - # Iterate over all the existing conversations - for i in await self.get_ims(): - if i.user == user.id: - channel_id = i.id - found = True - break - # A conversation does not exist, create one - if not found: - r = await self.client.api_call( - "im.open", - return_im=True, - user=user.id, - ) - response = self.tload(r, Response) - if not response.ok: - raise ResponseException(response.error) - channel_id = r['channel']['id'] + # If the conversation is not in cache, reopen it + # It is faster than querying the list of conversations + # anyway + r = await self.client.api_call( + "conversations.open", + prevent_creation=False, + users=user.id, + ) + response = self.tload(r, Response) + if not response.ok: + raise ResponseException(response.error) + channel_id = r['channel']['id'] self._imcache[user.id] = channel_id