matrix.py 1.06 KB
Newer Older
kaiyou's avatar
kaiyou committed
1
2
import os
import json
3
import time
kaiyou's avatar
kaiyou committed
4
5
6

from matrix_client import client as matrix_client

7
from amonit.check import base
kaiyou's avatar
kaiyou committed
8
9


10
11
def get_client(account):
    """ Create a Matrix client
kaiyou's avatar
kaiyou committed
12
    """
13
14
    return matrix_client.MatrixHttpApi(
        account["hs"], token=account["token"]
kaiyou's avatar
kaiyou committed
15
16
    )

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

def send_to_matrix(context, account, roomid, message):
    """ Send a message to a Matrix room
    """
    client = get_client(account)
    client.send_message(roomid, message)


def lookup_in_matrix(context, account, roomid, message):
    """ Look for the message in recent messages
    """
    client = get_client(account)
    sync = client.sync(since=context.get("next_batch", None),
                       filter=json.dumps({"room":{"rooms":[roomid]}}))
    context["next_batch"] = sync["next_batch"]
    if roomid not in sync["rooms"]["join"]:
        return False
    for event in sync["rooms"]["join"][roomid]["timeline"]["events"]:
        if event["content"].get("body", "") == message:
            return True


@base.send_then_receive(send_to_matrix, lookup_in_matrix)
def send_then_receive():
    pass