Compare commits

...

3 commits

Author SHA1 Message Date
2da355c739 Don't store session details 2022-03-27 19:35:46 +02:00
9987bb87db Run black 2022-03-27 19:35:40 +02:00
421fc9b98a Reorganize files to convert to python package 2022-03-27 19:33:46 +02:00
5 changed files with 49 additions and 44 deletions

View file

@ -1,26 +0,0 @@
import toml
class Config:
command_prefix: str
matrix_homeserver: str
matrix_user: str
matrix_access_token: str
spaceping_token: str
def __init__(self, path=None):
if path is None:
path = "/etc/itsyndikat-bot.toml"
config = toml.load(path)
self.command_prefix = config['app']['command_prefix']
matrix = config['matrix']
self.matrix_homeserver = matrix['homeserver']
self.matrix_username = matrix['username']
self.matrix_access_token = matrix['access_token']
self.spaceping_token = config['spaceping']['api_token']

18
main.py → its_matrix_bot/__init__.py Normal file → Executable file
View file

@ -1,11 +1,10 @@
import argparse
import datetime
from nio.rooms import MatrixRoom
from nio.events.room_events import RoomMessage
import simplematrixbotlib as botlib
from its_api import ItSyndikatApi
from config import Config
from .its_api import ItSyndikatApi
from .config import Config
class ItSyndikatBot:
@ -21,6 +20,7 @@ class ItSyndikatBot:
config.matrix_homeserver,
config.matrix_username,
access_token=config.matrix_access_token,
session_stored_file="",
)
self.its_api = ItSyndikatApi(config)
@ -77,15 +77,3 @@ class ItSyndikatBot:
async def spaceping(self, room: MatrixRoom, message: RoomMessage):
await self.its_api.ping()
await self.reply(room, message, "Hello Space!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="IT-Syndikat matrix bot")
parser.add_argument(
"-c", "--config",
help="path to the config file",
)
args = parser.parse_args()
ItSyndikatBot(Config(args.config))

View file

@ -0,0 +1,15 @@
import argparse
from . import ItSyndikatBot
from .config import Config
parser = argparse.ArgumentParser(description="IT-Syndikat matrix bot")
parser.add_argument(
"-c",
"--config",
help="path to the config file",
)
args = parser.parse_args()
ItSyndikatBot(Config(args.config))

26
its_matrix_bot/config.py Normal file
View file

@ -0,0 +1,26 @@
import toml
class Config:
command_prefix: str
matrix_homeserver: str
matrix_user: str
matrix_access_token: str
spaceping_token: str
def __init__(self, path=None):
if path is None:
path = "/etc/itsyndikat-bot.toml"
config = toml.load(path)
self.command_prefix = config["app"]["command_prefix"]
matrix = config["matrix"]
self.matrix_homeserver = matrix["homeserver"]
self.matrix_username = matrix["username"]
self.matrix_access_token = matrix["access_token"]
self.spaceping_token = config["spaceping"]["api_token"]

View file

@ -1,6 +1,7 @@
import aiohttp
from config import Config
from .config import Config
class ItSyndikatApi:
base_url: str
@ -15,10 +16,11 @@ class ItSyndikatApi:
async with session.get(self.base_url + "status.php") as response:
return await response.json()
async def ping(self):
params = {"apikey": self.config.spaceping_token}
async with aiohttp.ClientSession() as session:
async with session.post(self.base_url + "ping.php", params=params) as response:
async with session.post(
self.base_url + "ping.php", params=params
) as response:
await response.text()