Skip to content

Commit

Permalink
feat(): Add Slack-Discord message mirroring project (#66)
Browse files Browse the repository at this point in the history
Refs: eng-1510

---------

Co-authored-by: Daniel Abraham <[email protected]>
  • Loading branch information
pashafateev and daabr authored Sep 27, 2024
1 parent c659290 commit daf5362
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
80 changes: 80 additions & 0 deletions slack_discord_sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Slack-Discord Message Mirroring Workflow

This project automates the process of mirroring messages between Slack and Discord channels. It listens for new message events in Discord or Slack and sends them to the corresponding channel on the other platform. This project is intended as a starting point rather than a complete solution.

## Benefits

- **Cross-Platform Communication:** Automatically mirrors messages between Discord and Slack, keeping teams connected across both platforms.
- **Ease of Use:** Demonstrates how to easily integrate AutoKitteh with Slack and Discord APIs.
- **Low Complexity:** The workflow is implemented with minimal code and setup.

## How It Works

- **Detect Discord and Slack Messages:** The program listens for new message events in Discord or Slack.
- **Mirror Messages:** When a message is sent in one platform, it is immediately forwarded to the corresponding channel on the other platform.

## Installation and Usage

- [Install AutoKitteh](https://docs.autokitteh.com/get_started/install)

### Configure Integrations

- [Discord](https://docs.autokitteh.com/integrations/discord/connection)
- [Slack](https://docs.autokitteh.com/integrations/slack/config)

### Clone the Repository

```shell
git clone https://github.com/autokitteh/kittehub.git
cd kittehub/slack_discord_sync
```

Alternatively, you can copy the individual files in this directory.

### Run the AutoKitteh Server

Simply run this command:

```shell
ak up --mode dev
```

### Apply Manifest and Deploy Project

1. Navigate to the `slack_discord_sync` directory:

```shell
cd slack_discord_sync
```

2. Apply manifest and deploy project by running the following command:

```shell
ak deploy --manifest autokitteh.yaml
```
The output of this command will be important for initializing connections in the following step if you're using the CLI.

For example, for each configured connection, you will see a line that looks similar to the one below:

```shell
[exec] create_connection "slack_discord_sync/discord_conn": con_01j36p9gj6e2nt87p9vap6rbmz created
```

`con_01j36p9gj6e2nt87p9vap6rbmz` is the connection ID.

### Initialize Connections

> [!IMPORTANT]
> `slack_conn` and `discord_conn` need to be initialized using the IDs from the previous step.
Using the connection IDs from the previous step, run these commands:

```shell
ak connection init discord_conn <connection ID>
ak connection init slack_conn <connection ID>
```

### Trigger the Workflow

The workflow is triggered when a message is sent in either Slack or Discord, where the bot is present.
26 changes: 26 additions & 0 deletions slack_discord_sync/autokitteh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This YAML file is a declarative manifest that describes the setup
# of an AutoKitteh project that mirrors messages between Slack and
# Discord using AutoKitteh integrations.

version: v1

project:
name: slack_discord_sync
vars:
- name: DISCORD_CHANNEL_ID
value:
- name: SLACK_CHANNEL_ID
value:
connections:
- name: discord_conn
integration: discord
- name: slack_conn
integration: slack
triggers:
- name: on_discord_message
connection: discord_conn
event_type: message_create
call: program.py:on_discord_message
- name: on_slack_message
connection: slack_conn
call: program.py:on_slack_message
60 changes: 60 additions & 0 deletions slack_discord_sync/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
This script mirrors messages between Slack and Discord channels using
AutoKitteh's Slack and Discord clients.
Discord documentation:
- https://discordpy.readthedocs.io/
Note:
The `discord` import is crucial for enabling specific Discord API
configurations, such as intents and error handling, but all functional
API calls are made through the autokitteh `ak_discord` wrapper, which
streamlines authentication and secret management.
"""

import os

from autokitteh import discord as ak_discord
from autokitteh import slack
import discord


DISCORD_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
SLACK_CHANNEL_ID = os.getenv("SLACK_CHANNEL_ID")

# Discord intents that enable the bot to read message content
intents = discord.Intents.default()
intents.message_content = True

client = ak_discord.discord_client("discord_conn", intents)
slack_api = slack.slack_client("slack_conn")

# Stores the latest message received from Slack, to be posted to Discord
slack_message = None


def on_discord_message(event):
slack_api.chat_postMessage(channel=SLACK_CHANNEL_ID, text=event.data["content"])


def on_slack_message(event):
global slack_message
slack_message = event.data["text"]
client.run(ak_discord.bot_token("discord_conn"))


@client.event
async def on_ready():
"""An asynchronous event triggered when the Discord bot
successfully connects. It fetches the Discord channel by ID and sends
the latest message received from Slack to the channel, then closes the
client connection."""
try:
channel = await client.fetch_channel(DISCORD_CHANNEL_ID)
except discord.DiscordException as e:
print(f"Could not find Discord channel with ID: {DISCORD_CHANNEL_ID}: {e}")
return

await channel.send(slack_message)

await client.close()

0 comments on commit daf5362

Please sign in to comment.