Coder Social home page Coder Social logo

maubot-webhook's Introduction

maubot-webhook

A maubot plugin to send messages using webhooks.

Features

  • Jinja2 templating
  • JSON support
  • HTTP Basic and Token Bearer authorization

Installation

Either download an .mbp file from the release assets or build one yourself. Then, upload it to your maubot instance.

Furthermore this plugin requires Jinja2 for template rendering. But since maubot already depends on Jinja2, you shouldn't have to install it manually.

Usage

Create a new instance in the maubot management interface and select me.jkhsjdhjs.maubot.webhook as Type. The client selected as Primary user will be used to send the messages.

Each instance of this plugin provides a single webhook. To create multiple webhooks, just instantiate this plugin multiple times.

Example

path: /send
method: POST
room: '!AAAAAAAAAAAAAAAAAA:example.com'
message: |
    **{{ json.title }}**
    {% for text in json.list %}
    - {{ text }}
    {% endfor %}
auth_type: Basic
auth_token: abc:123
markdown: true
force_json: false
$ curl -X POST -H "Content-Type: application/json" -u abc:123 https://your.maubot.instance/_matrix/maubot/plugin/<instance ID>/send -d '
{
    "title": "This is a test message:",
    "list": [
        "Hello",
        "World!"
    ]
}'

Screenshot of the resulting message

Configuration

This plugin has the following settings you can configure:

path

The path the webhook will be available at. It must start with a / or be empty. It is relative to the webapp base URL of the instance:

https://your.maubot.instance/_matrix/maubot/plugin/<instance ID>/<path>

The URL under which the webhook is made available is logged on instance startup, so if you're unsure, you can check the logs.

The path supports variable resources, which can be used to extract information from the request URL to format the room and the message. Further information on this can be found in the formatting section.

The instance has to be restarted for changes to this setting to take effect: Click the Running switch to stop the instance, then save. Click the Running switch again to start it, then save again.

method

Specifies the HTTP method that can be used on the given path. Should be one of GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS or * for any method. This setting is case-insensitive.

See also: https://docs.aiohttp.org/en/stable/web_reference.html?highlight=add_route#aiohttp.web.UrlDispatcher.add_route

The instance has to be restarted for changes to this setting to take effect: Click the Running switch to stop the instance, then save. Click the Running switch again to start it, then save again.

room

The room the message shall be sent to when the webhook is triggered. Supports formatting as defined below.

message

The message that is sent when the webhook is triggered. Supports formatting as defined below.

auth_type

This can be used to protect a webhook against unauthorized access. Can be one of Basic for HTTP basic auth with username and password or Bearer for bearer token auth. Leave empty to disable authorization. The username/password or token is specified via the auth_token option.

auth_token

This specifies the username/password or token for authorization, depending on auth_type. If auth_type is Basic, this must be the username and password, separated by a colon (<username>:<password>). If auth_type is Bearer, this is the token used for token bearer authorization, so requests must carry an Authorization: Bearer <token> header.

markdown

This setting takes a boolean and specifies if the message should be sent as Markdown or as plaintext. If false (the default), the message will be sent as plaintext. Otherwise it will be sent as Markdown.

force_json

This setting takes a boolean and specifies whether the request body should be interpreted and parsed as json, even if the content type says otherwise.

Formatting

The room and message options can be formatted with Jinja2 using values from the path, the query string and the request body. Values extracted from the path are available via the path variable. Similarly, query parameters are available via the query variable. The request body in plain text is available as body.

If a request with content-type application/json is received (or if force_json is enabled), the request body will be parsed as such and made available via the json variable.

For more information on Jinja2 templates please refer to https://jinja.palletsprojects.com/en/3.1.x/templates/.
For more information on URL path templates in aiohttp, see https://docs.aiohttp.org/en/stable/web_quickstart.html#variable-resources.

Building

Use the mbc tool to build this plugin:

mbc build

Optionally use the -u switch to upload it to your maubot instance, if configured:

mbc build -u

Since .mbp files are just zip archives with a different name, you can also just zip the files of this repository:

zip -9r plugin.mbp *

License

This project is licensed under the GNU Affero General Public License v3.0, see LICENSE.

maubot-webhook's People

Contributors

jkhsjdhjs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.