start adding matrix bot stuff

This commit is contained in:
helican 2023-12-16 16:47:58 +01:00
parent 1d2439f25e
commit 5f9ce2966c

View file

@ -5,6 +5,13 @@ import sys
import json import json
import pprint import pprint
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from nio import AsyncClient, MatrixRoom, RoomMessageText
import asyncio
# for nio output
import logging
logging.basicConfig(level=logging.INFO)
# Stuff # Stuff
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
@ -134,12 +141,48 @@ def auer_calc_total(items):
total = total + count * auer_item_getprice(item) total = total + count * auer_item_getprice(item)
return total return total
async def bot_main():
# Initialize the Matrix client
client = AsyncClient("https://matrix.org")
# Replace 'your_username', 'your_password', and 'room_id' with your actual Matrix credentials
username = "your_username"
password = "your_password"
room_id = "room_id"
await login(client, username, password)
# Start the event listener in the background
event_listener_task = asyncio.create_task(event_listener())
try:
while True:
# Send a periodic message every 60 seconds
await send_periodic_message(room_id, "Hello, world!")
# Sleep for 60 seconds
await asyncio.sleep(60)
except KeyboardInterrupt:
print("Bot stopped by the user.")
finally:
# Stop the event listener task
event_listener_task.cancel()
# Close the Matrix client connection
await client.close()
def main(): def main():
used = {} used = {}
needed = 0 needed = 0
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-c", help="Filename for json config instead of stdin") parser.add_argument("-c", help="Filename for json config instead of stdin")
parser.add_argument('--bot', action=argparse.BooleanOptionalAction, help="Start in bot mode instead of standalone script")
parser.add_argument("-m", help="Filename for matrix bot configuration")
args = parser.parse_args() args = parser.parse_args()
if args.bot:
bot_main()
else:
# parse command line # parse command line
if args.c is not None: if args.c is not None:
inf = open(args.c) inf = open(args.c)