Skip to content

Commit

Permalink
Add dev channel
Browse files Browse the repository at this point in the history
  • Loading branch information
lloesche committed Jun 17, 2024
1 parent ff38dfa commit 8f868d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
7 changes: 6 additions & 1 deletion somebot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
SLACK_APP_TOKEN = os.getenv("SLACK_APP_TOKEN")
SLACK_SIGNING_KEY = os.getenv("SLACK_SIGNING_KEY")
DISCORD_FORWARD_CHANNEL_ID = os.getenv("DISCORD_FORWARD_CHANNEL_ID")
DISCORD_FORWARD_CHANNEL_ID_DEV = os.getenv("DISCORD_FORWARD_CHANNEL_ID_DEV")

if (
not DISCORD_TOKEN
Expand All @@ -48,6 +49,7 @@
sys.exit(1)

DISCORD_FORWARD_CHANNEL_ID = int(DISCORD_FORWARD_CHANNEL_ID)
DISCORD_FORWARD_CHANNEL_ID_DEV = int(DISCORD_FORWARD_CHANNEL_ID_DEV)


def start_discord_bot(bot: SomeDiscordBot) -> None:
Expand All @@ -72,7 +74,10 @@ def main() -> None:
message_queue = queue.Queue()

discord_bot = SomeDiscordBot(
message_queue=message_queue, shutdown_event=shutdown_event, channel_id=DISCORD_FORWARD_CHANNEL_ID
message_queue=message_queue,
shutdown_event=shutdown_event,
channel_id=DISCORD_FORWARD_CHANNEL_ID,
channel_id_dev=DISCORD_FORWARD_CHANNEL_ID_DEV,
)
discord_bot_thread = threading.Thread(
target=start_discord_bot, args=(discord_bot,), daemon=True, name="discord_bot"
Expand Down
19 changes: 17 additions & 2 deletions somebot/discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@

class SomeDiscordBot(discord.Client):
def __init__(
self, *args, message_queue: queue.Queue, shutdown_event: threading.Event, channel_id: int, **kwargs
self,
*args,
message_queue: queue.Queue,
shutdown_event: threading.Event,
channel_id: int,
channel_id_dev: int,
**kwargs,
) -> None:
kwargs.update({"intents": discord.Intents.all()})
super().__init__(*args, **kwargs)
self.message_queue = message_queue
self.shutdown_event = shutdown_event
self.last_reminded = {}
self.discord_channel_id = channel_id
self.discord_channel_id_dev = channel_id_dev
self.discord_channel = None
self.discord_channel_dev = None

async def on_ready(self) -> None:
log.info(f"{self.user.name} has connected to Discord!")
self.discord_channel = self.get_channel(self.discord_channel_id)
self.discord_channel_dev = self.get_channel(self.discord_channel_id_dev)
asyncio.create_task(self.process_message_queue())

async def on_message(self, message: discord.Message) -> None:
Expand All @@ -56,11 +65,17 @@ async def process_message_queue(self) -> None:
while not self.shutdown_event.is_set():
try:
message = await asyncio.to_thread(self.get_message)
channel = message.get("channel_name")
log.debug("Got message from queue, converting Slack to Discord format.")
message = convert_slack_message_to_discord(message)
if self.discord_channel:
if channel == "alerts" and self.discord_channel:
log.debug(f"Sending message to {self.discord_channel}")
await self.discord_channel.send(content=message.get("content"), embeds=message.get("embeds", []))
elif channel == "alerts-dev" and self.discord_channel_dev:
log.debug(f"Sending message to {self.discord_channel_dev}")
await self.discord_channel_dev.send(
content=message.get("content"), embeds=message.get("embeds", [])
)
except queue.Empty:
pass

Expand Down
17 changes: 17 additions & 0 deletions somebot/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def __init__(self, bot_token: str, app_token: str, signing_key: str, message_que
self.handler = SocketModeHandler(self.app, app_token)
self.client = WebClient(token=bot_token)
self.uid2name = {}
self.cid2name = {}

self.app.message()(self.handle_message_events)

def run(self):
# self.fetch_user_id_to_username_mapping()
self.fetch_channel_id_to_name_mapping()
self.handler.start()

def fetch_user_id_to_username_mapping(self):
Expand All @@ -32,7 +34,22 @@ def fetch_user_id_to_username_mapping(self):
except SlackApiError as e:
print(f"Error fetching users list: {e}")

def fetch_channel_id_to_name_mapping(self):
print("Fetching channel list")
try:
for response in self.client.conversations_list(limit=200, types="public_channel,private_channel"):
channels = response["channels"]
for channel in channels:
if channel.get("name") and channel.get("id") and channel.get("is_channel"):
print(f"Adding channel {channel['name']} with id {channel['id']} to mapping")
self.cid2name[channel["id"]] = channel["name"]
except SlackApiError as e:
print(f"Error fetching channels list: {e}")

def handle_message_events(self, event, say):
if event.get("type") == "message" and event.get("subtype") == "bot_message":
log.debug("Got message in Slack, sending to queue")
channel_id = event.get("channel")
channel_name = self.cid2name.get(channel_id)
event["channel_name"] = channel_name
self.message_queue.put(event)

0 comments on commit 8f868d1

Please sign in to comment.