add tell me more support with sqlite database
authorArmaan Bhojwani <code@armaanb.net>
Sun, 20 Dec 2020 21:31:49 +0000 (16:31 -0500)
committerArmaan Bhojwani <code@armaanb.net>
Sun, 20 Dec 2020 21:31:49 +0000 (16:31 -0500)
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

.gitignore
tallbot.py

index 67e8ef5fffeaa8e89e5e920ef4f4e6a17d89ed00..d95e4b07bf3b2057ae7067015343140f67e0e2d9 100644 (file)
@@ -1 +1,2 @@
+queries.db
 tall.bot
index e097ba78234fbc75146932e3f8af73d2ad11cc98..f17e04e5348def307940d2bf257618a16daf7ad5 100755 (executable)
@@ -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> " + 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()