Giter Club home page Giter Club logo

matrix-webhook's Introduction

Matrix Webhook

Tests Lints Docker-Hub Release pre-commit.ci status

Code style: black Ruff codecov Maintainability PyPI version

Post a message to a matrix room with a simple HTTP POST

Install

python3 -m pip install matrix-webhook
# OR
docker pull nim65s/matrix-webhook

Start

Create a matrix user for the bot, and launch this app with the following arguments and/or environment variables (environment variables update defaults, arguments take precedence):

matrix-webhook -h
# OR
python -m matrix_webhook -h
# OR
poetry run matrix-webhook -h
# OR
nix run github:nim65s/matrix-webhook -- -h
# OR
docker run --rm -it nim65s/matrix-webhook -h
usage: python -m matrix_webhook [-h] [-H HOST] [-P PORT] [-u MATRIX_URL] -i MATRIX_ID (-p MATRIX_PW | -t MATRIX_TOKEN) -k API_KEY [-v]

Configuration for Matrix Webhook.

options:
  -h, --help            show this help message and exit
  -H HOST, --host HOST  host to listen to. Default: `''`. Environment variable: `HOST`
  -P PORT, --port PORT  port to listed to. Default: 4785. Environment variable: `PORT`
  -u MATRIX_URL, --matrix-url MATRIX_URL
                        matrix homeserver url. Default: `https://matrix.org`. Environment variable: `MATRIX_URL`
  -i MATRIX_ID, --matrix-id MATRIX_ID
                        matrix user-id. Required. Environment variable: `MATRIX_ID`
  -p MATRIX_PW, --matrix-pw MATRIX_PW
                        matrix password. Either this or token required. Environment variable: `MATRIX_PW`
  -t MATRIX_TOKEN, --matrix-token MATRIX_TOKEN
                        matrix access token. Either this or password required. Environment variable: `MATRIX_TOKEN`
  -k API_KEY, --api-key API_KEY
                        shared secret to use this service. Required. Environment variable: `API_KEY`
  -v, --verbose         increment verbosity level

Dev

poetry install
# or python3 -m pip install --user markdown matrix-nio
python3 -m matrix_webhook

Prod

A docker-compose.yml is provided:

  • Use Traefik on the web docker network, eg. with proxyta.net
  • Put the configuration into a .env file
  • Configure your DNS for ${CHATONS_SERVICE:-matrixwebhook}.${CHATONS_DOMAIN:-localhost}
docker-compose up -d

Healthcheck

For load balancers which require a healthcheck endpoint to validate the availability of the service, the /health path can be used. The endpoint will return a HTTP 200 status and a json document.

To the Healthcheck endpoint with Traefik and docker-compose, you can add:

services:
  bot:
    labels:
      traefik.http.services.matrix-webhook.loadbalancer.healthcheck.path: /health

Test / Usage

curl -d '{"body":"new contrib from toto: [44](http://radio.localhost/map/#44)", "key": "secret"}' \
  'http://matrixwebhook.localhost/!DPrUlnwOhBEfYwsDLh:matrix.org'

(or localhost:4785 without docker)

For Github

Add a JSON webhook with ?formatter=github, and put the API_KEY as secret

For Grafana

Add a webhook with an URL ending with ?formatter=grafana&key=API_KEY

For Gitlab

At the group level, Gitlab does not permit to setup webhooks. A workaround consists to use Google Chat or Microsoft Teams notification integration with a custom URL (Gitlab does not check if the url begins with the normal url of the service).

Google Chat

Add a Google Chat integration with an URL ending with ?formatter=gitlab_gchat&key=API_KEY

Microsoft Teams

Add a Microsoft Teams integration with an URL ending with ?formatter=gitlab_teams&key=API_KEY

Gitlab Webhook

At the project level, you can add a webhook with an URL ending with ?formatter=gitlab_webhook and put your API_KEY as secret token. Not yet as pretty as other formatters, contributions welcome !

Github Release Notifier

To receiver notifications about new releases of projects hosted at github.com you can add a matrix webhook ending with ?formatter=grn&key=API_KEY to Github Release Notifier (grn).

Test room

#matrix-webhook:tetaneutral.net

Unit tests

docker-compose -f test.yml up --exit-code-from tests --force-recreate --build

matrix-webhook's People

Contributors

ananace avatar bboehmke avatar chronicc avatar dependabot[bot] avatar ghislainc avatar homeworkprod avatar kusold avatar nim65s avatar nrekretep avatar pre-commit-ci[bot] avatar svenseeberg avatar timo-schluessler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

matrix-webhook's Issues

Install on Unraid/Docker

Great tool!

I would like to setup it on my unraid machine - but dont know how to set the variables. Can you please show me?

Unbenannt-2

Best regards!

Gitlab error: "Missing body, key"

Hi,

when I test "matrix-webhook" with curl -d '{"body":"...", "key": "..."}' https://... the body message is posted to the group. However, the Gitlab test shows this error message:

Hook executed successfully but returned HTTP 400 {"status": 400, "ret": "Missing body, key"}
for projects or groups (where it is possible on premium plans).

Or is it intentionally possible only via Google-Chat- or Microsoft-Teams-Integration?

BTW, Microsoft-Teams looks nice, while Google-Chat has some problems with Line-Feeds (at least one missing after "Compare changes", then multiple LF between commits).

But thank you so much for this great webhook gateway. It just works. I had a lot of trouble (took me too much time) with other matrix application service solution, which finally cannot cope (at least currently) with Gitlab group hooks.

New Grafana notifications not formatted correctly

The recent update of Grafana revamps the whole notification system, including a changed format. In my case the formatting of the message is lost and looks like this:

image

It should probably look like this:

image

I'm currently investigating why this is happening and will probably contribute a PR when I found the issue.

Automatic relogin after disconnect

When connection to matrix server is lost, the webhook should automatically reconnect and re-login. The function join_room seems to be meant to re-login automatically, but in my case at least it doesn't, because CLIENT.join() doesn't return with the expected JoinError but in fact throws (see matrix-nio/matrix-nio#487).
Should this be worked around or fixed upstream?

Can't handle special "Consent" flows

Using this webhook bot on an instance (hosted via Element.io) with custom Consent flows the bot errors out with an unhandled exception:

alertbot_1  | 2021-10-12 11:50:45,698 - aiohttp.server - 393 - ERROR - Error handling request
alertbot_1  | Traceback (most recent call last):
alertbot_1  |   File "/usr/local/lib/python3.9/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
alertbot_1  |     resp = await self._request_handler(request)
alertbot_1  |   File "/matrix_webhook/handler.py", line 85, in matrix_webhook
alertbot_1  |     return await utils.send_room_message(data["room_id"], content)
alertbot_1  |   File "/matrix_webhook/utils.py", line 40, in send_room_message
alertbot_1  |     ERROR_MAP[resp.status_code], resp.message
alertbot_1  | KeyError: 'M_CONSENT_NOT_GIVEN'

On this instance a newly created user (in this case the bot user) is redirected to a consent page and has to accept it before being able to participate and join rooms.

If not easily fixable (navigating and accepting to consent pages will probably be hard) this error should at least be printed to the log, even without the --verbose flag.

Grafana/Signoz Webhook Error Code 400

Dear Dev,

I have deployed matrix-webhook

If I hit manual using curl, All is running well

curl -d '{"body":"Test WEBHOOK MATRIX", "key": "xxxxxxxxxxxxx"}'   'http://matrix-webhook.xxxxxx.xxx:8443/!lcGnHYvcRftvBdvRgr:matrix.xxxxxx.xxx'

{"status": 200, "ret": "OK"}

And when I added alert I webhook in signoz or grafana I got error 400 code.

level=error ts=2024-03-26T07:42:18.583Z caller=api.go:808 component=api version=v1 msg="API error" err="server_error: unexpected status code 400:

I have added in last URL for grafana format

?formatter=grafana&key=MYTOKEN

like :

http://webhook.xxxxxx.xxx:8443/!lcGnHYvcRsftvBdvxRgr:webhook.xxxxxx.xxx?formatter=grafana&key=MYTOKEN

Use nix flakes

to allow easy packaging and deployment on this app on nix systems, and also to add tests with gitlab/grafana/etc.

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.