HUB
Sign inSign up

geluk/matrix-webhook-gateway

By geluk

•Updated over 1 year ago

Webhook gateway for Matrix / Synapse.

Image
0

10K+

geluk/matrix-webhook-gateway repository overview

⁠Tags

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.

⁠Installation

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

⁠Configuration

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.

⁠Usage

⁠Set up a webhook

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.

⁠Call the webhook

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.

⁠Plugins

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

⁠Writing plugins

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.

⁠Plugin loading process

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:

  1. The contents of the plugin file are hashed.
  2. 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).
  3. If no file is found, the plugin is copied to ./plugins/__workdir and compiled. This path needs to be within the working directory of the application, otherwise type resolutions during compilation will fail.
  4. The compiled plugin is written to webhooks.plugin_cache_directory.
  5. The cached plugin is copied to ./plugins/__workdir to ensure any imports are resolved correctly during the loading process.
  6. The plugin is require()d and some basic integrity checks are performed against the resulting object to validate that it is correctly formed.
  7. The init() function is executed, and the plugin is added to the list of active plugins.

⁠Development setup

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

⁠References

  • apprise⁠, a platform-independent notification framework.
  • turt2live⁠, whose project inspired me to develop this application.

⁠Useful documentation

Tag summary
Recent tags
Content type
Image
Digest
Size
123.1 MB
Last updated
about 5 years ago
docker pull geluk/matrix-webhook-gateway:latest