Compare commits
No commits in common. "834ea75fd864a359cf271aed1885cc9c07f412f6" and "4cdd60d48f3f53ef705c4669d74b23323763ad22" have entirely different histories.
834ea75fd8
...
4cdd60d48f
4 changed files with 3 additions and 62 deletions
|
@ -8,6 +8,3 @@ access_token = ...
|
||||||
|
|
||||||
[spaceping]
|
[spaceping]
|
||||||
api_token = ...
|
api_token = ...
|
||||||
|
|
||||||
[isitopen]
|
|
||||||
announce_rooms = ["!room_id:homeserver.example"]
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
import asyncio
|
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
|
||||||
from nio.rooms import MatrixRoom
|
from nio.rooms import MatrixRoom
|
||||||
from nio.events.room_events import RoomMessage
|
from nio.events.room_events import RoomMessage
|
||||||
import simplematrixbotlib as botlib
|
import simplematrixbotlib as botlib
|
||||||
|
@ -18,8 +16,6 @@ class ItSyndikatBot:
|
||||||
def __init__(self, config: Config):
|
def __init__(self, config: Config):
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
self.current_open_state = None
|
|
||||||
|
|
||||||
creds = botlib.Creds(
|
creds = botlib.Creds(
|
||||||
config.matrix_homeserver,
|
config.matrix_homeserver,
|
||||||
config.matrix_username,
|
config.matrix_username,
|
||||||
|
@ -32,28 +28,7 @@ class ItSyndikatBot:
|
||||||
self.bot = botlib.Bot(creds)
|
self.bot = botlib.Bot(creds)
|
||||||
|
|
||||||
self.bot.listener.on_message_event(self.on_message)
|
self.bot.listener.on_message_event(self.on_message)
|
||||||
|
self.bot.run()
|
||||||
async def run(self):
|
|
||||||
async def poll_for_changes():
|
|
||||||
while True:
|
|
||||||
logging.debug("Polling open state")
|
|
||||||
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:
|
|
||||||
logging.error(f"Polling for open state failed: {e}")
|
|
||||||
|
|
||||||
await asyncio.sleep(60)
|
|
||||||
|
|
||||||
asyncio.create_task(poll_for_changes())
|
|
||||||
|
|
||||||
await self.bot.main()
|
|
||||||
|
|
||||||
async def on_message(self, room, message):
|
async def on_message(self, room, message):
|
||||||
m = botlib.MessageMatch(room, message, self.bot, self.config.command_prefix)
|
m = botlib.MessageMatch(room, message, self.bot, self.config.command_prefix)
|
||||||
|
@ -70,25 +45,6 @@ class ItSyndikatBot:
|
||||||
room.room_id, f"Unknown command: {m.command()}"
|
room.room_id, f"Unknown command: {m.command()}"
|
||||||
)
|
)
|
||||||
|
|
||||||
async def announce_open_change(self, now_open: bool):
|
|
||||||
logging.info("Open state changed: now " + ("open" if now_open else "closed"))
|
|
||||||
|
|
||||||
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):
|
async def reply(self, room, message, reply):
|
||||||
await self.bot.api.async_client.room_send(
|
await self.bot.api.async_client.room_send(
|
||||||
room_id=room.room_id,
|
room_id=room.room_id,
|
||||||
|
@ -108,9 +64,7 @@ class ItSyndikatBot:
|
||||||
async def isitopen(self, room, message):
|
async def isitopen(self, room, message):
|
||||||
try:
|
try:
|
||||||
status = await self.its_api.status()
|
status = await self.its_api.status()
|
||||||
is_open = status["state"]["open"]
|
if status["state"]["open"]:
|
||||||
self.current_open_state = is_open
|
|
||||||
if is_open:
|
|
||||||
date = datetime.datetime.fromtimestamp(status["state"]["lastchange"])
|
date = datetime.datetime.fromtimestamp(status["state"]["lastchange"])
|
||||||
text = f"positive! space has been open since {date}"
|
text = f"positive! space has been open since {date}"
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
import asyncio
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
|
||||||
|
|
||||||
from . import ItSyndikatBot
|
from . import ItSyndikatBot
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="IT-Syndikat matrix bot")
|
parser = argparse.ArgumentParser(description="IT-Syndikat matrix bot")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-c",
|
"-c",
|
||||||
|
@ -16,5 +12,4 @@ parser.add_argument(
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
bot = ItSyndikatBot(Config(args.config))
|
ItSyndikatBot(Config(args.config))
|
||||||
asyncio.run(bot.run())
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import toml
|
import toml
|
||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
@ -11,8 +10,6 @@ class Config:
|
||||||
|
|
||||||
spaceping_token: str
|
spaceping_token: str
|
||||||
|
|
||||||
isitopen_announce_rooms: List[str]
|
|
||||||
|
|
||||||
def __init__(self, path=None):
|
def __init__(self, path=None):
|
||||||
if path is None:
|
if path is None:
|
||||||
path = "/etc/itsyndikat-bot.toml"
|
path = "/etc/itsyndikat-bot.toml"
|
||||||
|
@ -27,5 +24,3 @@ class Config:
|
||||||
self.matrix_access_token = matrix["access_token"]
|
self.matrix_access_token = matrix["access_token"]
|
||||||
|
|
||||||
self.spaceping_token = config["spaceping"]["api_token"]
|
self.spaceping_token = config["spaceping"]["api_token"]
|
||||||
|
|
||||||
self.isitopen_announce_rooms = config["isitopen"]["announce_rooms"]
|
|
||||||
|
|
Loading…
Reference in a new issue