1
0
Fork 0
quotes-bot-python/bot.py
2025-02-01 16:48:31 -06:00

145 lines
4.7 KiB
Python

import simplematrixbotlib as botLibrary
import json
import fetch
version = "0.2.0-dev"
print("Imag/quotes bot version " + version)
print("Loading config…")
try:
with open('config.json', 'r') as f:
config = json.load(f)
print("Loaded config.")
except FileNotFoundError:
print("Config file not found. Did you read the README?")
except:
print("Something bad and unforseen happened, please report the bug to @n:everypizza.im")
if config['bot']['debugMode'] == True:
debugMode = True
else:
debugMode = False
print("Logging in…")
credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password'])
bot = botLibrary.Bot(credentials)
PREFIX = "!q"
print("Logged in.")
print("Ready!")
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("help")):
return
if debugMode == False:
message = (f"""
Help
============================
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
{PREFIX} help - show this message
{PREFIX} get - fetch a image from the defined instance
{PREFIX} source - send a link to the source code
{PREFIX} version - show the bot version
""")
else:
message = (f"""
Help
============================
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
{PREFIX} help - show this message
{PREFIX} get - fetch a image from the defined instance
{PREFIX} source - send a link to the source code
{PREFIX} version - show the bot version
---
NOTE: Debug mode is on. Output will be more verbose.
""")
await bot.api.send_text_message(room.room_id, message)
@bot.listener.on_message_event
async def make_choice(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("get")):
return
temp = True
if not match.args():
temp = False
else:
id = match.args()
id = str(id[1])
quoteImage = fetch.fetchQuote(id)
quoteData = fetch.fetchQuoteData(id)
def formatData(data):
dataToFormat = data
created = str(data['created'])
description = str(data['desc'])
imageid = str(data['iid'])
ocr = str(data['ocr'])
score = str(data['score'])
return created, description, imageid, ocr, score
if debugMode == False:
created, description, imageid, ocr, score = formatData(quoteData)
message1 = (f"""
Created: {created}
Description: {description}
Image ID: {imageid}
OCR (Tesseract): {ocr}
Rating: {score}
""")
else:
created, description, imageid, ocr, score = formatData(quoteData)
message1 = (f"""
Created: {created}
Description: {description}
Image ID: {imageid}
OCR (Tesseract): {ocr}
Rating: {score}
""")
message2 = (f"""
Command recived (DEBUG).
(We're supposed to fetch quote number {id} now.)
""")
if debugMode == True:
await bot.api.send_text_message(room.room_id, message2)
await bot.api.send_text_message(room.room_id, message1)
else:
await bot.api.send_text_message(room.room_id, message1)
imageMessage = (quoteImage)
await bot.api.send_image_message(room.room_id, imageMessage) # https://quotes.everypizza.im/image/178
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("source")):
return
message = (f"""
quotes-bot-python by Nyx Tutt (@n:everypizza.im)
https://git.everypizza.im/n/quotes-bot-python/
""")
await bot.api.send_text_message(room.room_id, message)
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("version")):
return
message = (f"""
quotes-bot-python version {version}
""")
await bot.api.send_text_message(room.room_id, message)
bot.run()