start adding matrix bot stuff
This commit is contained in:
parent
1d2439f25e
commit
5f9ce2966c
1 changed files with 52 additions and 9 deletions
61
auerbot.py
61
auerbot.py
|
@ -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,25 +141,61 @@ 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()
|
||||||
# parse command line
|
|
||||||
if args.c is not None:
|
if args.bot:
|
||||||
inf = open(args.c)
|
bot_main()
|
||||||
else:
|
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)
|
conf = json.load(inf)
|
||||||
ppe.pprint(conf)
|
ppe.pprint(conf)
|
||||||
|
|
||||||
auer_request(conf)
|
auer_request(conf)
|
||||||
|
|
||||||
if inf is not sys.stdin:
|
if inf is not sys.stdin:
|
||||||
inf.close()
|
inf.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue