Commit fa273831 authored by Otthorn's avatar Otthorn 🤔

Added module de citaton

parent 8c04b813
base_quote_file = "citations.txt"
def write_to_db(author, quote, quote_file=base_quote_file):
f = open(quote_file, "a")
f.write(author + " " + quote + "\n")
f.close()
def load_db(quote_file=base_quote_file):
print("Loading quote database...")
f = open(quote_file, "r")
L = f.readlines()
f.close()
quote_dico = {}
for k in range(len(L)):
L[k] = L[k].strip("\n")
L[k] = L[k].split(" ")
author = L[k][0]
quote = " ".join(L[k][1:])
if author not in quote_dico.keys():
quote_dico[author] = []
quote_dico[author].append(quote)
print("Quote databse loaded")
return quote_dico
lisa_spooky Est-ce que je ressemble à un demi raton laveur ?
Fistine huhu :3
lisa_spooky !help linguistique
lisa_spooky 😍
otthorn Miaou
otthorn Je suis un chat
otthorn Aller, on range la Med ! (et on le fit vraiment :o)
Smile Bot :)
Smile Bot ...
......@@ -8,9 +8,14 @@ from giphy_api import gif_search
from weather_api import query_weather
from parse_env import parse_env
import citations
# Parse the global variables from the env
dico_env = parse_env("matrix.env")
# Load the quote database
quote_dico = citations.load_db()
USERNAME = dico_env['USERNAME'] # Bot's username
PASSWORD = dico_env['PASSWORD'] # Bot's password
SERVER = dico_env['SERVER'] # Matrix server URL
......@@ -108,7 +113,33 @@ def weather_callback(room, event):
summary = query_weather(query)
room.send_text(summary)
return
def quote_callback(room, event):
args = event['content']['body'].split()
author = args[1]
# author risque de changer si les gens changent de nom -> il vaut mieux mettre l'ID matrix
quote = " ".join(args[2:])
if author not in quote_dico.keys():
quote_dico[author] = []
quote_dico[author].append(quote)
citations.write_to_db(author, quote)
def list_quote_callback(room, event):
args = event['content']['body'].split()
author = args[1]
if author not in quote_dico.keys():
room.send_text("Désolé mais {} est introuvable dans la base de donnée".format(author))
return
else:
for quote in quote_dico[author]:
room.send_html("<blockquote>\n{} - {}\n</blockquote>\n".format(quote, author))
def main():
......@@ -136,6 +167,12 @@ def main():
weather_handler = MCommandHandler("weather", weather_callback)
bot.add_handler(weather_handler)
quote_handler = MCommandHandler("citation", quote_callback)
bot.add_handler(quote_handler)
list_quote_handler = MCommandHandler("liste_citations", list_quote_callback)
bot.add_handler(list_quote_handler)
# Start polling
bot.start_polling()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment