Compare commits
No commits in common. "main" and "0.1.0" have entirely different histories.
4 changed files with 23 additions and 117 deletions
105
bot.py
105
bot.py
|
@ -1,9 +1,8 @@
|
|||
import simplematrixbotlib as botLibrary
|
||||
import json
|
||||
import fetch
|
||||
import time
|
||||
|
||||
version = "0.3.0"
|
||||
version = "0.1.0"
|
||||
|
||||
print("Imag/quotes bot version " + version)
|
||||
print("Loading config…")
|
||||
|
@ -16,14 +15,8 @@ except FileNotFoundError:
|
|||
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'])
|
||||
admin = config['bot']['admin']
|
||||
bot = botLibrary.Bot(credentials)
|
||||
PREFIX = "!q"
|
||||
print("Logged in.")
|
||||
|
@ -34,29 +27,16 @@ async def help_message(room, message):
|
|||
if not (match.is_not_from_this_bot() and match.prefix()
|
||||
and match.command("help")):
|
||||
return
|
||||
if debugMode == False:
|
||||
message = (f"""Help
|
||||
|
||||
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.""")
|
||||
|
||||
""")
|
||||
|
||||
await bot.api.send_text_message(room.room_id, message)
|
||||
|
||||
|
@ -71,57 +51,14 @@ async def make_choice(room, message):
|
|||
temp = False
|
||||
else:
|
||||
id = match.args()
|
||||
try:
|
||||
id = int(id[1])
|
||||
except ValueError:
|
||||
youTriedMessage = ("Blunt tried :skull:")
|
||||
await bot.api.send_text_message(room.room_id, youTriedMessage)
|
||||
|
||||
id = str(id[1])
|
||||
quoteImage = fetch.fetchQuote(id)
|
||||
quoteData = fetch.fetchQuoteData(id)
|
||||
message = (f"""
|
||||
Command recived (DEBUG).
|
||||
(We're supposed to fetch quote number {id} now.)
|
||||
""")
|
||||
|
||||
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)
|
||||
await bot.api.send_text_message(room.room_id, message)
|
||||
imageMessage = (quoteImage)
|
||||
await bot.api.send_image_message(room.room_id, imageMessage) # https://quotes.everypizza.im/image/178
|
||||
|
||||
|
@ -132,8 +69,10 @@ async def help_message(room, message):
|
|||
and match.command("source")):
|
||||
return
|
||||
|
||||
message = (f"""quotes-bot-python by Nyx Tutt (@n:everypizza.im)
|
||||
https://git.everypizza.im/n/quotes-bot-python/""")
|
||||
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)
|
||||
|
||||
|
@ -144,18 +83,10 @@ async def help_message(room, message):
|
|||
and match.command("version")):
|
||||
return
|
||||
|
||||
message = (f"""quotes-bot-python version {version}""")
|
||||
message = (f"""
|
||||
quotes-bot-python version {version}
|
||||
""")
|
||||
|
||||
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()
|
||||
|
|
|
@ -7,9 +7,5 @@
|
|||
"imag":{
|
||||
"_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"
|
||||
},
|
||||
"bot": {
|
||||
"debugMode": true,
|
||||
"admin": "@alice:example.com"
|
||||
}
|
||||
}
|
||||
|
|
BIN
error.png
BIN
error.png
Binary file not shown.
Before Width: | Height: | Size: 17 KiB |
21
fetch.py
21
fetch.py
|
@ -17,25 +17,4 @@ def fetchQuote(quoteId):
|
|||
return filename
|
||||
else:
|
||||
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
|
||||
|
||||
def fetchTopQuotes(instance):
|
||||
raise NotImplementedError
|
||||
|
||||
def fetchBottomQuotes(instance):
|
||||
raise NotImplementedError
|
||||
|
||||
fetchQuoteData(178)
|
||||
|
|
Loading…
Add table
Reference in a new issue