Jump to content

Tool:Bridgebot

From Wikitech
Toolforge tools
Description A bot for bridging IRC and other chat platform channels
Keywords irc, golang, matrix, telegram, discord
Maintainer(s) BryanDavis (View all)
Source code gitlab:toolforge-repos/bridgebot
License GNU General Public License 3.0 or later
Issues Open tasks · Report a bug
Admin log Tools.bridgebot/SAL

Bridgebot is a deployment of the FLOSS matterbridge software. It can be used to relay messages between multiple different chat systems. The current deployment in Toolforge is bridging various Wikimedia related channels across platforms including Libera.Chat, Telegram, Discord and Matrix.

Bot accounts

The bot must be invited/added to chat channels in order to create bridges. Currently the bot has these accounts on various chat platforms:

libera.chat IRC
  • account: wm-bridgebot
  • nicks: wm-bridgebot, wm-bb
Telegram
Discord
Matrix
  • account: @wm-bb:matrix.org

Maintenance

$ ssh login.toolforge.org
$ become bridgebot
$ toolforge jobs list
+----------------+------------------+----------------------------+
|   Job name:    |    Job type:     |          Status:           |
+----------------+------------------+----------------------------+
| static-cleaner | schedule: @daily | Waiting for scheduled time |
|      bnc       |    continuous    |          Running           |
|   bridgebot    |    continuous    |          Running           |
+----------------+------------------+----------------------------+
$ kubectl logs --all-containers=true --ignore-errors --max-log-requests 10 --prefix=true \
  --since=10m --follow \
  --selector 'app.kubernetes.io/name in (bridgebot, bnc)'

To deploy new code or config changes, first build a new container image:

$ toolforge build start https://gitlab.wikimedia.org/toolforge-repos/bridgebot

Then restart the bridgebot job:

$ toolforge jobs restart bridgebot

Joining a new channel

Maintainers of the tool can configure the bot to join a new channel by adding a new [[gateway]] section to etc/bridgebot.toml, making a new build, and restarting the bridgebot job.

Telegram

If one side of the bridge is Telegram you will need these things to be done:

  1. Have an admin of the Telegram group invite the "@wmtelegram_bot" user to the group
  2. Have someone in the Telegram group post the message /chatId (This command is case-sensitive.)
  3. Collect the Telegram "Chat" id number from @wmtelegram_bot's reply to the bot command
  4. File a task like phab:T303563

Discord

If one side of the bridge is Discord you will need these things to be done:

  1. Have an admin of the Discord server invite the bot to their server using the bot's invite link

Matrix

Nothing special needs to be done to bridge a Matrix room as long as the bot account is able to join the room. Note that the bridge is known to relay previously sent Matrix messages, which could be an issue when bridging rooms with significant history. (It's possible that tweaking the room's history visibility settings may avoid this, to be tested...)

SAL

2024-07-05

  • 20:55 wmbot~bd808@tools-bastion-12: Restarted bridgebot job to pick up new container build (T368317, MR!8)
  • 17:00 wmbot~bd808@tools-bastion-12: Replaced tool-bridgebot/znc container with tool-containers/bnc container (T366970)

2024-06-24

  • 19:12 wmbot~lucaswerkmeister@tools-bastion-13: toolforge jobs restart bridgebot # *without* new image; bot had stopped bridging cloud IRC and telegram for no obvious reason and hopefully this fixes it
  • 19:10 wmbot~lucaswerkmeister@tools-bastion-13: toolforge build delete bridgebot-buildpacks-p... (more)

Common problems

Duplicate messages being sent from irc to other bridges

The bot can get into a state where every message that it sees from irc gets sent multiple times to other bridged channels. This is caused by incorrect handling of irc server connection messages by the bot when the intermediate ZNC service reconnects to libra.chat. The fix is simply to restart the bot:

$ ssh login.toolforge.org
$ become bridgebot
$ toolforge jobs restart bridgebot
$ dologmsg 'Double IRC messages to other bridges'

You can combine this into a single command:

$ ssh login.toolforge.org -- become bridgebot "toolforge jobs restart bridgebot && dologmsg 'Double IRC messages to other bridges'"

Technical details

The bot runs from Toolforge under the "bridgebot" account as a pair of jobs each running a build service managed container. One job runs ZNC via gitlab:toolforge-repos/wikibugs2-znc and the other runs matterbridge via gitlab:toolforge-repos/bridgebot. Various credentials are stored in toolforge envvars.

There used to be a relatively complex hand build set of Deployments that powered the tool that may be of interest to some.

toolforge-repos/bridgebot and bridgebot job

The tool's bridgebot job runs an image built from gitlab:toolforge-repos/bridgebot. The repo contains tiny golang application that wraps the upstream matterbridge application. As of 2024-04-25, the version of matterbridge that is imported is actually a local soft fork from gitlab:toolforge-repos/bridgebot-matterbridge.

bnc job

The tool's bnc job runs the image from tool-containers/bnc:latest. This irc bouncer is used to work around issues like irc disconnect/reconnect loops that caused problems for the service in the past.

jobs.yaml

$HOME/jobs.yaml
# https://wikitech.wikimedia.org/wiki/Help:Toolforge/Jobs_framework
---
# ZNC bouncer to sit between matterbridge and libra.chat
# https://gitlab.wikimedia.org/toolforge-repos/wikibugs2-znc
- name: bnc
  command: bouncer
  image: tool-containers/bnc:latest
  cpu: 250m
  mem: 256Mi
  continuous: true
  emails: none
  mount: none
  no-filelog: true
  port: 6667

# Matterbridge in our custom container
- name: bridgebot
  command: bot
  image: tool-bridgebot/tool-bridgebot:latest
  cpu: "1"
  mem: 1G
  continuous: true
  emails: onfailure
  mount: all
  no-filelog: true
  # Mount is needed for storing media

# Delete old media files once per day
# https://github.com/42wim/matterbridge/wiki/Mediaserver-setup-(advanced)#sidenote
- name: static-cleaner
  # >- means replace newlines with spaces (folded), no newline at end (strip)
  command: >-
    /usr/bin/find
    /data/project/bridgebot/www/static
    -mindepth 1
    -mtime +30
    -delete
    >/dev/null
    2>>/data/project/bridgebot/logs/static-cleaner.err
    ;
    true
  # ^ find exits nonzero due to not deleting nonempty subdirs;
  # `true` hides this
  image: bookworm
  schedule: "@daily"
  emails: none
  mount: all
  no-filelog: true

remotenickformat.tengo

The etc/remotenickformat.tengo tengo script is used to do per-channel customization of nick formats used when cross-posting messages between services.

Note: Any syntax errors in the script will stop messages from passing across all bridges until they are corrected.