From 9df07b4bd72753bb0435f19156f0a2bddcb96e75 Mon Sep 17 00:00:00 2001 From: tfa Date: Wed, 14 Dec 2022 12:25:48 +0100 Subject: [PATCH] First commit --- base-config.yaml | 2 ++ maubot.yaml | 10 +++++++ tcl.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 base-config.yaml create mode 100644 maubot.yaml create mode 100644 tcl.py diff --git a/base-config.yaml b/base-config.yaml new file mode 100644 index 0000000..e86da29 --- /dev/null +++ b/base-config.yaml @@ -0,0 +1,2 @@ +mail: mail@domain.tld +password: unsuperpassword diff --git a/maubot.yaml b/maubot.yaml new file mode 100644 index 0000000..b1be7fc --- /dev/null +++ b/maubot.yaml @@ -0,0 +1,10 @@ +maubot: 0.1.0 +id: tfa.tcl +version: 0.0.1 +license: AGPL-3.0-or-later +modules: +- tcl +main_class: Tcl +config: true +extra_files: +- base-config.yaml diff --git a/tcl.py b/tcl.py new file mode 100644 index 0000000..4e828b4 --- /dev/null +++ b/tcl.py @@ -0,0 +1,74 @@ +from typing import Type +from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper +from maubot import Plugin, MessageEvent +from maubot.handlers import command +from datetime import datetime +import aiohttp +import asyncio +import json +import base64 + +class Config(BaseProxyConfig): + def do_update(self, helper: ConfigUpdateHelper) -> None: + helper.copy("mail") + helper.copy("password") + + +class Tcl(Plugin): + async def start(self) -> None: + self.config.load_and_update() + + + @command.new(name="tcl", help="") + @command.argument("pattern", pass_raw=True, required=True) + async def tclInfos(self, evt: MessageEvent, pattern: str) -> None: + await evt.mark_read() + if not pattern: + await evt.respond( + """ + !tcl alerte ligne -> Retourne les alertes TCL sur la ligne donnée + """ + ) + else: + self.log.info("Commande : " + pattern) + if pattern.__contains__("alerte"): + chunks = pattern.split(None, 1) + if len(chunks) != 2: + await evt.respond( + """ + !tcl alerte ligne -> Retourne les alertes TCL sur la ligne donnée + """ + ) + url = 'https://download.data.grandlyon.com/ws/rdata/tcl_sytral.tclalertetrafic_2/all.json?maxfeatures=200&start=1' + strToEncode = self.config["mail"] + ':' + self.config["password"] + base64string = base64.b64encode(strToEncode.encode('utf-8')) + resp = await self.http.get(url, headers={'Authorization' : 'Basic ' + base64string.decode('utf-8')}) + ans = await resp.text() + try: + resp.raise_for_status() + except aiohttp.ClientError as Error: + await evt.respond(str(Error)) + return + objet = json.loads(ans) + try: + values = objet["values"] + alerte=False + for value in values: + if(value["ligne_cli"] == chunks[1]): + alerte=True + debut = datetime.fromisoformat(value["debut"]) + fin = datetime.fromisoformat(value["fin"]) + respText = "

" + value["type"] + "

" + value["titre"] + "

" \ + + value["message"] + "

" + debut.strftime("%d/%m/%Y à %H:%M") + " au " \ + + fin.strftime("%d/%m/%Y à %H:%M") + "

" + await evt.respond(respText, allow_html=True) + if(alerte==False): + await evt.respond("

Pas d'alerte sur cette ligne

", allow_html=True) + except KeyError: + await evt.respond("Erreur d'analyse JSON ") + @classmethod + def get_config_class(cls) -> Type[BaseProxyConfig]: + return Config + + +