From: Armaan Bhojwani Date: Sun, 20 Dec 2020 21:31:49 +0000 (-0500) Subject: add tell me more support with sqlite database X-Git-Url: https://git.armaanb.net/?p=tall.bot.git;a=commitdiff_plain;h=b4a25c9ba64baa5d53546333dcf5ba89afffc48c add tell me more support with sqlite database This commit adds preliminary support for the "tell more more command" to return more sentences about the previous query. Utilizes a sqlite3 database to store people's last queries --- diff --git a/.gitignore b/.gitignore index 67e8ef5..d95e4b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +queries.db tall.bot diff --git a/tallbot.py b/tallbot.py index e097ba7..f17e04e 100755 --- a/tallbot.py +++ b/tallbot.py @@ -3,6 +3,7 @@ # Armaan Bhojwani 2020 import logging +import sqlite3 import sys import time import wikipedia @@ -10,16 +11,18 @@ import discord def wiki_sum(search, sen, pid): try: - logging.info(str(pid) + " | " + str(int(time.time())) + " | " + search) + logging.info(str(pid) + " | " + str(int(time.time())) + " | " + str(search)) + log_query(search, pid) return " " + wikipedia.summary(search, sentences=sen, auto_suggest=False) except Exception as e: - logging.debug(str(pid) + " | " + search + " | " + str(e)) + logging.debug(str(pid) + " | " + str(search) + " | " + str(e)) return str(e) def create_query(inp): - key1 = ["What", "what", "who", "Who"] + key1 = ["What", "what", "who", "Who", "what's", "What's", "whats", "Whats", + "Whatsa", "whatsa"] key2 = ["is", "are", "were", "was"] key3 = ["a", "an", "the"] @@ -33,6 +36,39 @@ def create_query(inp): return " ".join(x) +def tell_more(inp, pid): + def find_query(inp): + key1 = ["Tell me more", "tell me more"] + if inp[:12] in key1: + return inp[11:] + else: + return False + + def query_lookup(pid): + old = int(time.time()) - 150 + c.execute("SELECT query FROM queries WHERE pid = (?) AND time > (?)", + (str(pid), old)) + return c.fetchone() + + if find_query(inp): + return query_lookup(pid) + else: + return False + +def prep_database(): + c.execute("""CREATE TABLE IF NOT EXISTS queries( + time INTEGER, + pid TEXT, + query TEXT)""") + conn.commit() + +def log_query(search, pid): + c.execute("DELETE FROM queries WHERE pid = (?)", (str(pid),)) + c.execute("""INSERT INTO queries + (time, pid, query) VALUES(?, ?, ?)""", + (int(time.time()), str(pid), str(search))) + conn.commit() + class TallBot(discord.Client): async def on_ready(self): logging.info('Logged in as ', self.user) @@ -41,12 +77,18 @@ class TallBot(discord.Client): if message.author == self.user: return - key = create_query(message.content) - if key: - await message.channel.send(wiki_sum(key, 2, message.author)) + query = create_query(message.content) + more_info = tell_more(message.content, message.author) + if query: + await message.channel.send(wiki_sum(query, 2, message.author)) + elif more_info: + await message.channel.send(wiki_sum(more_info, 8, message.author)) def main(): + prep_database() TallBot().run(sys.argv[1]) if __name__ == "__main__": + conn = sqlite3.connect("queries.db") + c = conn.cursor() main()