Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Discord bot that converts Youtube links to tournesol links #1728

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions infra/ansible/roles/discordbot/files/discord_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

import discord
import re
import os
import requests

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

yt_url_re = re.compile(r"\b(?:https?:)?(?:\/\/)?(?:youtu\.be\/|(?:www\.|m\.)?youtube\.com\/(?:watch|v|embed|live)(?:\.php)?(?:\?\S*v=|\/))([a-zA-Z0-9\_-]{11})(?:[\?&][a-zA-Z0-9\_-]+=[a-zA-Z0-9\_-]+)*(?:[&\/\#].*)?\b")


token = os.environ["DISCORD_BOT_TOKEN"]
tournesol_api_url = os.environ["TOURNESOL_API_URL"]


def video_exists(video_id: str) -> bool:
resp = requests.head(f"{tournesol_api_url}/video/{video_id}/")
if resp.status_code != requests.codes.ok:
print(f"video {video_id} not found in Tournesol")
return False
print(f"video {video_id} found in Tournesol")
return True


@client.event
async def on_ready():
print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
if message.author == client.user:
return
url_matches = set(yt_url_re.findall(message.content))
if len(url_matches) > 0:
print(f'Found a message containing video ids: {", ".join(url_matches)}')
links = [f"https://tournesol.app/entities/yt:{video_id}" for video_id in url_matches if video_exists(video_id)]
if len(links) > 0:
msg = f"Please use Tournesol links when posting on social media to help us promote the project:\n" + "\n".join(links)
# if message.content:
await message.channel.send(msg)


client.run(token)
5 changes: 5 additions & 0 deletions infra/ansible/roles/discordbot/handlers/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Restart Discord Bot
systemd:
name: discordbot.service
state: restarted
daemon_reload: true
31 changes: 31 additions & 0 deletions infra/ansible/roles/discordbot/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
- name: Create Virtualenv for Discord Bot
pip:
name:
- "discord.py==2.3.2"
- "pip==23.2.1"
- "requests==2.31.0"
virtualenv: /srv/tournesol-discordbot/venv
virtualenv_python: python3.9
become: yes

- name: Copy Discord Bot service file
template:
src: discordbot.service.j2
dest: /etc/systemd/system/discordbot.service
notify: Restart Discord Bot

- name: Copy Discord Bot script file
copy:
src: discord_bot.py
dest: /usr/local/bin/discord_bot.py
mode: u=rx,o=,g=
notify: Restart Discord Bot

- name: Enable and start Discord Bot
systemd:
name: discordbot.service
enabled: true
state: started
daemon_reload: true

- meta: flush_handlers
12 changes: 12 additions & 0 deletions infra/ansible/roles/discordbot/templates/discordbot.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Discord bot service
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/bash -c "source /srv/tournesol-discordbot/venv/bin/activate && /usr/local/bin/discord_bot.py"
Environment="DISCORD_BOT_TOKEN={{discord_bot_token}}"
Environment="TOURNESOL_API_URL={{api_scheme}}://{{api_domain_name}}"

[Install]
WantedBy=multi-user.target
2 changes: 2 additions & 0 deletions infra/ansible/roles/django/templates/settings.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TWITTERBOT_CREDENTIALS:
"ACCESS_TOKEN_SECRET": "{{access_token_secret_twitterbot_en}}",
}

DISCORD_BOT_TOKEN: {{discord_bot_token}}

DISCORD_CHANNEL_WEBHOOKS: {
infra_alert: "{{ discord_infra_alert_webhook }}",
infra_alert_private: "{{ discord_infra_alert_private_webhook }}",
Expand Down
5 changes: 5 additions & 0 deletions infra/ansible/scripts/deploy-without-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ if [[ "${2:-""}" == "fast" ]]
then
echo "Using setup-fast.yaml"
SETUP_FILE="setup-fast.yml"
elif [[ "${2:-""}" == "discord" ]]
then
echo "Using setup-discord.yaml"
SETUP_FILE="setup-discord.yml"
else
echo "Using setup.yaml"
SETUP_FILE="setup.yml"
Expand Down Expand Up @@ -53,6 +57,7 @@ ansible-playbook -i inventory.yml -l "$ANSIBLE_HOST" "$SETUP_FILE" \
-e "consumer_secret_twitterbot_en=${TWBOT_CONSUMER_SECRET_EN:-""}" \
-e "access_token_twitterbot_en=${TWBOT_ACCESS_TOKEN_EN:-""}" \
-e "access_token_secret_twitterbot_en=${TWBOT_ACCESS_TOKEN_SECRET_EN:-""}" \
-e "discord_bot_token=${DISCORD_BOT_TOKEN:-""}" \
-e "discord_infra_alert_webhook=${DISCORD_INFRA_ALERT_WEBHOOK:-""}" \
-e "discord_infra_alert_private_webhook=${DISCORD_INFRA_ALERT_PRIVATE_WEBHOOK:-""}" \
-e "discord_twitter_webhook=${DISCORD_TWITTER_WEBHOOK:-""}" \
Expand Down
1 change: 1 addition & 0 deletions infra/ansible/scripts/forget-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ unset TWBOT_CONSUMER_KEY_EN
unset TWBOT_CONSUMER_SECRET_EN
unset TWBOT_ACCESS_TOKEN_EN
unset TWBOT_ACCESS_TOKEN_SECRET_EN
unset DISCORD_BOT_TOKEN
unset DISCORD_INFRA_ALERT_WEBHOOK
unset DISCORD_INFRA_ALERT_PRIVATE_WEBHOOK
unset DISCORD_TWITTER_WEBHOOK
Expand Down
13 changes: 9 additions & 4 deletions infra/ansible/scripts/get-vm-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
export VM_ADDR="${1:-"staging.tournesol.app"}"
export VM_USER="${2:-"$USER"}"

function get_settings_value() {
local jq_filter=$1;
SETTINGS_FILE_JSON="$(\
ssh "$VM_USER@$VM_ADDR" -- \
'sudo python3 -c '\''import yaml,json; print(json.dumps(yaml.safe_load(open("/etc/tournesol/settings.yaml"))))'\'' \
| jq -r' "'$jq_filter | values'"
'sudo python3 -c '\''import yaml,json; print(json.dumps(yaml.safe_load(open("/etc/tournesol/settings.yaml"))))'\'''\
)"

function get_settings_value() {
jq -r $1 <<< "$SETTINGS_FILE_JSON"
}

DJANGO_SECRET_KEY=$(get_settings_value .SECRET_KEY)
Expand Down Expand Up @@ -88,6 +90,9 @@ export TWBOT_ACCESS_TOKEN_EN
TWBOT_ACCESS_TOKEN_SECRET_EN=$(get_settings_value .TWITTERBOT_CREDENTIALS.\"@TournesolBot\".ACCESS_TOKEN_SECRET)
export TWBOT_ACCESS_TOKEN_SECRET_EN

DISCORD_BOT_TOKEN=$(get_settings_value .DISCORD_BOT_TOKEN)
export DISCORD_BOT_TOKEN

DISCORD_INFRA_ALERT_WEBHOOK=$(get_settings_value .DISCORD_CHANNEL_WEBHOOKS.infra_alert)
export DISCORD_INFRA_ALERT_WEBHOOK

Expand Down
4 changes: 4 additions & 0 deletions infra/ansible/setup-discord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: tournesol
roles:
- discordbot
become: true
Loading