From 5f9ce2966c7db3140d257e91abad90565e393762 Mon Sep 17 00:00:00 2001 From: helican Date: Sat, 16 Dec 2023 16:47:58 +0100 Subject: [PATCH] start adding matrix bot stuff --- auerbot.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/auerbot.py b/auerbot.py index 71793a2..34c69e3 100755 --- a/auerbot.py +++ b/auerbot.py @@ -5,6 +5,13 @@ import sys import json import pprint from bs4 import BeautifulSoup +from nio import AsyncClient, MatrixRoom, RoomMessageText +import asyncio + + +# for nio output +import logging +logging.basicConfig(level=logging.INFO) # Stuff pp = pprint.PrettyPrinter(indent=4) @@ -134,25 +141,61 @@ def auer_calc_total(items): total = total + count * auer_item_getprice(item) 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(): used = {} needed = 0 parser = argparse.ArgumentParser() 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() - # parse command line - if args.c is not None: - inf = open(args.c) + + if args.bot: + bot_main() else: - inf = sys.stdin + # parse command line + if args.c is not None: + inf = open(args.c) + else: + inf = sys.stdin - conf = json.load(inf) - ppe.pprint(conf) + conf = json.load(inf) + ppe.pprint(conf) - auer_request(conf) + auer_request(conf) - if inf is not sys.stdin: - inf.close() + if inf is not sys.stdin: + inf.close() if __name__ == "__main__": main()