A Matrix bot that serves URL previews from a trusted server to the whole room.
Matrix is a protocol to build messaging apps, for example, Element and FluffyChat, similar to Discord and WhatsApp.
Matrix operates like email — different organizations govern their Matrix servers independently, but users across organizations can communicate with each other.
Matrix-URL-Previewer-Bot extracts links from Matrix chatrooms and display previews like this:
Due to the distributed nature of Matrix, HTTP requests using our User-Agent may originate from all over the world. In case if you need to protect your website from malicious attackers and crawlers that pretend to be Matrix-URL-Previewer-Bot, please consider the following:
-
You may identify requests by the User-Agent keywords
Matrix-URL-Previewer-Bot
, but please understand that, they identify both legitimate Matrix-URL-Previewer-Bot and malicious ones pretenting to be Matrix-URL-Previewer-Bot.(Note: The User-Agent keyword
Synapse (bot;
used by Synapse also belongs to Matrix-related apps for a similar purpose.) -
You may impose rate-limiting, because a well-behaved Matrix-URL-Previewer-Bot caches the result for 1 hour by default.
-
You may prevent malicious crawlers by serving only the page title and the Open Graph metadata, omitting the page body, because Matrix-URL-Previewer-Bot doesn’t need the page body.
-
Supporting encrypted chatrooms without leaking information to untrusted homeservers.
We expect Matrix-URL-Previewer-Bot be operated by chatroom admins. They are considered more trusted, than the participating homeservers, from a room member’s perspective.
-
Ensuring all members of a chatroom, no matter which homeserver they belong to, see the same URL preview.
-
Supporting custom rewrite rules to deal with tricky websites.
-
Install the Rust development environment on your server.
Alternatively, if your server isn’t powerful enough for the Rust compiler, you can also build it statically elsewhere and transfer the compiled program to the server. The detailed steps are omitted here.
-
Create a Matrix account for the bot. I suggest registering your bot on a self-hosted Synapse server.
$ register_new_matrix_user --no-admin -c homeserver.yaml -t bot
The bot account has to use password authentication. Multi-factor authentication and single sign-on are unsupported, as they can’t function unattended.
-
Create a configuration file
config.toml
based onexample-config.toml
. -
Perform the setup procedure.
$ cargo run --release setup --config=config.toml Matrix homeserver: <HOMESERVER> User name: <USERNAME> Password: <PASSWORD>
Depending on whether a backup exists on the server, you may be asked:
Backup recovery key: <RECOVERY KEY>
Or:
Are you ready to reset the cryptographic identity to enable server-side backup (y/n)? y Please move <DATA PATH>/recovery-key.txt to a safe place, then press ENTER to continue:
-
Run the bot.
$ cargo run --release run --config=config.toml
This step can be set to start automatically on computer bootup.
-
Configure the account profile.
Use a Matrix client, for example, Element, to log into your bot account. Type in your recovery key when asked.
Set the profile name to “Link Preview” and the profile picture to
transparent.png
, a 68-byte transparent PNG image. -
Remove any unverified registration session.
The
register_new_matrix_user
program mentioned above may have created an unverified session. This session should be removed to prevent encryption problems.Go to settings. In the Sessions tab, sign out any unverified or unrecognized sessions.
-
Join your desired chatrooms.
Matrix-URL-Previewer-Bot doesn’t respond to any room invitations to prevent abuse. Therefore, you need to join the chatrooms manually through Element.
-
Test whether it works.
-
Sign out of Element.
-
If encryption or account recovery goes wrong.
Please refer to matrixbot-ezlogin/Readme.md for the troubleshooting steps.
-
Matrix-URL-Previewer-Bot can’t preview images yet.
This is because Matrix doesn’t yet support mixing images and text in a single encrypted message.
-
When a user edits their message in a quick succession, a race condition may prevent Matrix-URL-Previewer-Bot from correctly respond to the last edit.
The user can workaround this issue by editing the affected message one more time.