From b4e76b75e4bbcd67aec4ef86344b270d58873f4a Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 29 Mar 2022 19:27:45 +0200 Subject: [PATCH] Add open/close notifications Uses only polling for now, websocket support to be added later. --- config.toml.example | 3 +++ its_matrix_bot/__init__.py | 46 ++++++++++++++++++++++++++++++++++++-- its_matrix_bot/__main__.py | 4 +++- its_matrix_bot/config.py | 5 +++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/config.toml.example b/config.toml.example index cf895c6..805f5c7 100644 --- a/config.toml.example +++ b/config.toml.example @@ -8,3 +8,6 @@ access_token = ... [spaceping] api_token = ... + +[isitopen] +announce_rooms = ["!room_id:homeserver.example"] diff --git a/its_matrix_bot/__init__.py b/its_matrix_bot/__init__.py index 1730b7f..28610f8 100755 --- a/its_matrix_bot/__init__.py +++ b/its_matrix_bot/__init__.py @@ -1,3 +1,4 @@ +import asyncio import datetime from nio.rooms import MatrixRoom from nio.events.room_events import RoomMessage @@ -16,6 +17,8 @@ class ItSyndikatBot: def __init__(self, config: Config): self.config = config + self.current_open_state = None + creds = botlib.Creds( config.matrix_homeserver, config.matrix_username, @@ -28,7 +31,27 @@ class ItSyndikatBot: self.bot = botlib.Bot(creds) self.bot.listener.on_message_event(self.on_message) - self.bot.run() + + async def run(self): + async def poll_for_changes(): + while True: + try: + status = await self.its_api.status() + new_state = status["state"]["open"] + if ( + self.current_open_state is not None + and new_state != self.current_open_state + ): + await self.announce_open_change(new_state) + self.current_open_state = new_state + except Exception as e: + pass + + await asyncio.sleep(60) + + asyncio.create_task(poll_for_changes()) + + await self.bot.main() async def on_message(self, room, message): m = botlib.MessageMatch(room, message, self.bot, self.config.command_prefix) @@ -45,6 +68,23 @@ class ItSyndikatBot: room.room_id, f"Unknown command: {m.command()}" ) + async def announce_open_change(self, now_open: bool): + room_ids = self.config.isitopen_announce_rooms + if now_open: + message = "opening IT-Syndikat - Ohai!" + else: + message = "closing IT-Syndikat - nap time!" + + for room_id in room_ids: + await self.bot.api.async_client.room_send( + room_id=room_id, + message_type="m.room.message", + content={ + "msgtype": "m.notice", + "body": message, + }, + ) + async def reply(self, room, message, reply): await self.bot.api.async_client.room_send( room_id=room.room_id, @@ -64,7 +104,9 @@ class ItSyndikatBot: async def isitopen(self, room, message): try: status = await self.its_api.status() - if status["state"]["open"]: + is_open = status["state"]["open"] + self.current_open_state = is_open + if is_open: date = datetime.datetime.fromtimestamp(status["state"]["lastchange"]) text = f"positive! space has been open since {date}" else: diff --git a/its_matrix_bot/__main__.py b/its_matrix_bot/__main__.py index d3dafd9..7dea5ae 100644 --- a/its_matrix_bot/__main__.py +++ b/its_matrix_bot/__main__.py @@ -1,3 +1,4 @@ +import asyncio import argparse from . import ItSyndikatBot @@ -12,4 +13,5 @@ parser.add_argument( args = parser.parse_args() -ItSyndikatBot(Config(args.config)) +bot = ItSyndikatBot(Config(args.config)) +asyncio.run(bot.run()) diff --git a/its_matrix_bot/config.py b/its_matrix_bot/config.py index d2425ec..f5b82ae 100644 --- a/its_matrix_bot/config.py +++ b/its_matrix_bot/config.py @@ -1,4 +1,5 @@ import toml +from typing import List class Config: @@ -10,6 +11,8 @@ class Config: spaceping_token: str + isitopen_announce_rooms: List[str] + def __init__(self, path=None): if path is None: path = "/etc/itsyndikat-bot.toml" @@ -24,3 +27,5 @@ class Config: self.matrix_access_token = matrix["access_token"] self.spaceping_token = config["spaceping"]["api_token"] + + self.isitopen_announce_rooms = config["isitopen"]["announce_rooms"]