Compare commits

..

24 commits
0.1.0 ... main

Author SHA1 Message Date
n
cbfdd831a7 Blunt tried 💀 2025-02-02 02:09:48 -06:00
n
528e1e81a1 fix minor security vuln 2025-02-02 02:05:54 -06:00
n
0d5b7cb854 tried to do something, failed in the end 2025-02-02 01:49:55 -06:00
Nyx
96ba34f17b Merge pull request 'shorten code for die command' (#2) from paige/quotes-bot-python:main into main
Reviewed-on: #2
2025-02-02 01:34:55 -06:00
b283f66bb8 shorten code for die command 2025-02-02 07:32:06 +00:00
Nyx
ee73b777ba Merge pull request 'use nice timestamps (like ISO 8601)' (#1) from paige/quotes-bot-python:main into main
Reviewed-on: #1
2025-02-02 00:49:01 -06:00
e4df14c3b7 use nice timestamps (like ISO 8601) 2025-02-02 06:31:39 +00:00
n
68e3e2020c fix 329877432705476439 2025-02-02 00:13:27 -06:00
n
b572834329 Fuck you zayd 2025-02-02 00:11:49 -06:00
n
b685fa6e18 fix indents 2025-02-02 00:07:53 -06:00
n
216c1bf4d7 version+ 2025-02-01 17:25:24 -06:00
n
8deaabf5d8 stub top/bottom quotes 2025-02-01 17:22:34 -06:00
n
66ff8f1474 add !q die, admin 2025-02-01 17:11:21 -06:00
n
96ba32b610 Version 0.2.0 2025-02-01 16:48:45 -06:00
n
7d7bba643e Parse data 2025-02-01 16:48:31 -06:00
n
0306704d69 more error handling 2025-01-31 21:20:16 -06:00
n
87cc4999dc add error image 2025-01-31 21:19:30 -06:00
n
2b3409f9f3 fix (thank you paige) 2025-01-31 21:16:11 -06:00
n
87e8e051c0 ok 2025-01-31 21:15:43 -06:00
n
5392f00a49 0.2.0-dev 2025-01-31 21:13:05 -06:00
n
022064e227 Also fetch the data 2025-01-31 21:09:33 -06:00
n
d83463d27a increment version 2025-01-31 20:58:18 -06:00
n
5c3f9031dc Finish debug mode 2025-01-31 20:54:58 -06:00
n
94e871556e start work on a debugMode 2025-01-31 20:51:27 -06:00
4 changed files with 118 additions and 24 deletions

117
bot.py
View file

@ -1,8 +1,9 @@
import simplematrixbotlib as botLibrary import simplematrixbotlib as botLibrary
import json import json
import fetch import fetch
import time
version = "0.1.0" version = "0.3.0"
print("Imag/quotes bot version " + version) print("Imag/quotes bot version " + version)
print("Loading config…") print("Loading config…")
@ -15,8 +16,14 @@ except FileNotFoundError:
except: except:
print("Something bad and unforseen happened, please report the bug to @n:everypizza.im") 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…") print("Logging in…")
credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password']) credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password'])
admin = config['bot']['admin']
bot = botLibrary.Bot(credentials) bot = botLibrary.Bot(credentials)
PREFIX = "!q" PREFIX = "!q"
print("Logged in.") print("Logged in.")
@ -27,16 +34,29 @@ async def help_message(room, message):
if not (match.is_not_from_this_bot() and match.prefix() if not (match.is_not_from_this_bot() and match.prefix()
and match.command("help")): and match.command("help")):
return 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
{PREFIX} die - kills the bot (only admins can do this!)""")
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
{PREFIX} die - kills the bot (only admins can do this!)
---
NOTE: Debug mode is on. Output will be more verbose.""")
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
""")
await bot.api.send_text_message(room.room_id, message) await bot.api.send_text_message(room.room_id, message)
@ -51,14 +71,57 @@ async def make_choice(room, message):
temp = False temp = False
else: else:
id = match.args() id = match.args()
id = str(id[1]) try:
quoteImage = fetch.fetchQuote(id) id = int(id[1])
message = (f""" except ValueError:
Command recived (DEBUG). youTriedMessage = ("Blunt tried :skull:")
(We're supposed to fetch quote number {id} now.) await bot.api.send_text_message(room.room_id, youTriedMessage)
""")
await bot.api.send_text_message(room.room_id, message) quoteImage = fetch.fetchQuote(id)
quoteData = fetch.fetchQuoteData(id)
def formatData(data):
if data is not None:
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(data['created']))
dataToFormat = data
created = timestamp
description = str(data['desc'])
imageid = str(data['iid'])
ocr = str(data['ocr'])
score = str(data['score'])
return created, description, imageid, ocr, score
else:
dataToFormat = data
created = "Error"
description = "Error"
imageid = "Error"
ocr = "Error"
score = "Error"
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) imageMessage = (quoteImage)
await bot.api.send_image_message(room.room_id, imageMessage) # https://quotes.everypizza.im/image/178 await bot.api.send_image_message(room.room_id, imageMessage) # https://quotes.everypizza.im/image/178
@ -69,10 +132,8 @@ async def help_message(room, message):
and match.command("source")): and match.command("source")):
return return
message = (f""" message = (f"""quotes-bot-python by Nyx Tutt (@n:everypizza.im)
quotes-bot-python by Nyx Tutt (@n:everypizza.im) https://git.everypizza.im/n/quotes-bot-python/""")
https://git.everypizza.im/n/quotes-bot-python/
""")
await bot.api.send_text_message(room.room_id, message) await bot.api.send_text_message(room.room_id, message)
@ -83,10 +144,18 @@ async def help_message(room, message):
and match.command("version")): and match.command("version")):
return return
message = (f""" message = (f"""quotes-bot-python version {version}""")
quotes-bot-python version {version}
""")
await bot.api.send_text_message(room.room_id, message) 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 match.is_not_from_this_bot() and match.prefix() and match.command("die"):
if match.is_from_userid(admin):
await bot.api.send_text_message(room.room_id, "Ok")
exit(0)
else:
await bot.api.send_text_message(room.room_id, "You don't have permissions for that.")
bot.run() bot.run()

View file

@ -7,5 +7,9 @@
"imag":{ "imag":{
"_comment": "Don't put a slash at an end of this. That took quite a bit of running around in circles to fix.", "_comment": "Don't put a slash at an end of this. That took quite a bit of running around in circles to fix.",
"server": "https://quotes.example.com" "server": "https://quotes.example.com"
},
"bot": {
"debugMode": true,
"admin": "@alice:example.com"
} }
} }

BIN
error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View file

@ -17,4 +17,25 @@ def fetchQuote(quoteId):
return filename return filename
else: else:
print(f"An error occured while saving the file: {quote.status_code}") print(f"An error occured while saving the file: {quote.status_code}")
return "error.png"
def fetchQuoteData(quoteId):
processedId = str(config['imag']['server'] + "/api/image/" + str(quoteId))
print(processedId)
quoteData = requests.get(processedId)
code = 200
if quoteData.status_code == 200:
return quoteData.json()
# print(str(json.dumps(quoteData)))
print(quoteData.json())
else:
print(f"An error occured while saving the file: {quoteData.status_code}")
return None return None
def fetchTopQuotes(instance):
raise NotImplementedError
def fetchBottomQuotes(instance):
raise NotImplementedError
fetchQuoteData(178)