Webhook gateway for Matrix / Synapse.
10K+
Tags are published for every major, minor, and patch release. For instance, version x.y.z generates and/or updates the following tags:
x (major)x.y (minor)x.y.z (patch)The major tag will be updated on every minor or patch release. The minor tag will be updated on every patch release. Patch tags are guaranteed to never be overwritten, so they can be used to pin your container to a specific image.
In addition to this, the latest tag is always updated to the latest stable release.
Lastly, the dev tag is always set to the latest CI build. Note that these versions may be unstable and are not fit for production environments. In particular, be wary of potential breaking changes. These are always announced in the release notes, but by tracking the dev tag you will receive these changes before a new release is created.
To get started quickly, an example compose file is provided below:
version: "3.6"
services:
matrix-webhooks:
image: geluk/matrix-webhook-gateway:latest
volumes:
- ./data:/data
- ./config:/config
ports:
- 8020:8020
- 8023:8023
restart: unless-stopped
A new configuration file (/config/gateway-config.yaml) is generated on first startup.
The webhook gateway will use the settings in this file to automatically generate an appservice registration file (default location: /data/appservice-webhook-gateway.yaml). You should copy this file to your Matrix server, and add it to your Synapse configuration:
app_service_config_files:
- "./appservices/appservice-webhook-gateway.yaml"
If your webhook and Matrix server live on the same host, you can also choose to directly point Matrix to the generated appservice file.
Invite the bot to a new channel:
/invite @_webhook:yourmatrixserver.tld
To create a webhook in the channel, enter the following command:
-hook create <webhook_name>
The bot will create a new user for this webhook, invite it, and send you the webhook URL in a private message.
To list all enabled webhooks in a channel, use:
-hook list
To delete a webhook, use:
-hook delete <id>
Where <id> is the numeric ID of a webhook as returned by -hook list.
To call the webhook, send a POST request with a JSON body to the webhook URL.
Several formats are supported, such as Slack:
{
"text": "I am a webhook message",
"username": "Webhook",
"icon_url": "https://example.com/hook.png"
}
Discord:
{
"content": "I am a webhook message",
"username": "Webhook",
"icon_url": "https://example.com/hook.png"
}
Apprise:
{
"version": "1.0",
"title": "A new message",
"message": "I am a webhook message"
}
As well as the format exposed by turt2live/matrix-appservice-webhooks
{
"text": "Hello world!",
"format": "plain",
"displayName": "My Cool Webhook",
"avatarUrl": "http://i.imgur.com/IDOBtEJ.png"
}
Other formats can be supported by writing a custom plugin, see below.
To support arbitrary JSON POSTs, you can write a custom plugin to interpret the message body and format it into something the webhook gateway understands.
A plugin is a Typescript file dropped into /data/plugins, which needs to conform to a specific format.
A sample plugin can be found here.
this plugin may also be interesting to look at. It generates a message from an alert notification as sent by Prometheus Alertmanager.
To execute a plugin, you must append its name (as specified by the format key in your plugin definition) to the webhook URL. For instance, to execute the sample plugin, send a POST to https://your-webhook-gateway-url/hook/your-webhook/sample
If you're just looking to write a simple plugin, the easiest way to get started is by copying the sample plugin, adapting it to your needs, and installing it in the right directory.
For more complicated plugins, I recommend cloning the repository and developing
your plugin in the ./plugins/develop directory.
The loading process of plugins is a bit involved, and not necessarily optimal. In other words, I'm open to suggestions! If you know of a way to streamline it, please do create an issue or a pull request.
Here's how it currently works:
On startup, the directory specified in the configuration file
(webhooks.plugin_directory) is scanned recursively, and the plugins in it are
loaded one by one. The loading process of a plugin looks like this:
webhooks.plugin_cache_directory is checked for a file with a matching hash.
If a file is found, the cached plugin is loaded immediately (see step 5)../plugins/__workdir and
compiled. This path needs to be within the working directory of the
application, otherwise type resolutions during compilation will fail.webhooks.plugin_cache_directory../plugins/__workdir to ensure any imports
are resolved correctly during the loading process.require()d and some basic integrity checks are performed
against the resulting object to validate that it is correctly formed.init() function is executed, and the plugin is added to the list of
active plugins.Run start-matrix.sh in ./local-dev to set up a local appservice
development environment with Docker. This will start up a Synapse and an Element
instance, which you can use for testing the appservice.
cd local-dev
./start-matrix.sh
Go to http://localhost:8009 and log in using the following credentials:
Username: dev
Password: appservice-dev
Start the app service:
npm ci
npm run start
Invite the webhook management user using the following Matrix command:
/invite @webhook:matrix.local
docker pull geluk/matrix-webhook-gateway:latest