aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-02-03 01:27:31 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commit4de39f91d9d8942816b8967a115010c466b8f252 (patch)
treee019d0f6437f131ce3192991b17ef2263ee442e8 /slack
parent684e7d701a4eb21c3b95954c3ca2e38d78ee451c (diff)
downloadwee-slack-4de39f91d9d8942816b8967a115010c466b8f252.tar.gz
Add messages received over rtm to channels
Diffstat (limited to 'slack')
-rw-r--r--slack/slack_conversation.py9
-rw-r--r--slack/slack_workspace.py24
2 files changed, 29 insertions, 4 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index b65ad98..e72e293 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import time
+from collections import OrderedDict
from contextlib import contextmanager
from typing import TYPE_CHECKING, List, Optional
@@ -38,6 +39,7 @@ class SlackConversation:
self.workspace = workspace
self._info = info
self._members: Optional[List[str]] = None
+ self._messages: OrderedDict[str, SlackMessage] = OrderedDict()
# TODO: buffer_pointer may be accessed by buffer_switch before it's initialized
self.buffer_pointer: str = ""
self.is_loading = False
@@ -150,6 +152,8 @@ class SlackConversation:
start = time.time()
messages = [SlackMessage(self, message) for message in history["messages"]]
+ for message in messages:
+ self._messages[message.ts] = message
sender_user_ids = [m.sender_user_id for m in messages if m.sender_user_id]
await self.workspace.users.initialize_items(sender_user_ids)
@@ -165,6 +169,11 @@ class SlackConversation:
self.history_filled = True
self.history_pending = False
+ async def add_message(self, message: SlackMessage):
+ self._messages[message.ts] = message
+ message_rendered = await message.render_message()
+ weechat.prnt(self.buffer_pointer, message_rendered)
+
def _buffer_input_cb(self, data: str, buffer: str, input_data: str) -> int:
weechat.prnt(buffer, "Text: %s" % input_data)
return weechat.WEECHAT_RC_OK
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
index 0d7e8c9..638a17b 100644
--- a/slack/slack_workspace.py
+++ b/slack/slack_workspace.py
@@ -20,6 +20,7 @@ from slack.proxy import Proxy
from slack.shared import shared
from slack.slack_api import SlackApi
from slack.slack_conversation import SlackConversation
+from slack.slack_message import SlackMessage
from slack.slack_user import SlackBot, SlackUser, SlackUsergroup
from slack.task import Future, Task, create_task, gather, run_async
from slack.util import get_callback_name
@@ -29,6 +30,7 @@ if TYPE_CHECKING:
from slack_api.slack_conversations_info import SlackConversationsInfo
from slack_api.slack_usergroups_info import SlackUsergroupInfo
from slack_api.slack_users_info import SlackUserInfo
+ from slack_rtm.slack_rtm_message import SlackRtmMessage
else:
SlackBotInfo = object
SlackConversationsInfo = object
@@ -257,10 +259,24 @@ class SlackWorkspace:
elif opcode != ABNF.OPCODE_TEXT:
return weechat.WEECHAT_RC_OK
- self._ws_recv(json.loads(recv_data.decode()))
-
- def _ws_recv(self, data: object):
- print(f"received: {data}")
+ run_async(self._ws_recv(json.loads(recv_data.decode())))
+
+ async def _ws_recv(self, data: SlackRtmMessage):
+ if data["type"] == "message":
+ if "subtype" in data and data["subtype"] == "message_changed":
+ pass
+ elif "subtype" in data and data["subtype"] == "message_deleted":
+ pass
+ elif "subtype" in data and data["subtype"] == "message_replied":
+ pass
+ else:
+ channel_id = data["channel"]
+ if channel_id in self.open_conversations:
+ channel = self.open_conversations[channel_id]
+ message = SlackMessage(channel, data)
+ await channel.add_message(message)
+ else:
+ weechat.prnt("", f"\t{self.name} received: {json.dumps(data)}")
def ping(self):
if not self.is_connected: