added fuzzy string matching for commands
This commit is contained in:
		
							
								
								
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +1,10 @@ | |||||||
| # API-Key (keep secret at all times) | # API-Key (keep secret at all times) | ||||||
| key.py | key.py | ||||||
|  |  | ||||||
|  | # Cookies | ||||||
|  | .google-cookie | ||||||
|  |  | ||||||
|  |  | ||||||
| # Byte-compiled / optimized / DLL files | # Byte-compiled / optimized / DLL files | ||||||
| __pycache__/ | __pycache__/ | ||||||
| *.py[cod] | *.py[cod] | ||||||
|   | |||||||
| @@ -1,5 +0,0 @@ | |||||||
| #LWP-Cookies-2.0 |  | ||||||
| Set-Cookie3: 1P_JAR="2020-05-14-16"; path="/"; domain=".google.com"; path_spec; domain_dot; secure; expires="2020-06-13 16:24:54Z"; version=0 |  | ||||||
| Set-Cookie3: NID="204=n_wUpdR_TRFItRjyHxZ36B0EybjpBvLOa2ZsmpYnFPdVyLkmIaHc86rv9Ne06Ky5XEPRNNnsIHNzfmZc0NVnCf3HujqVbmYxN9OocwqASX0dYmH4B8Iy9UdLmvAUUfKC5FB3EkMMi_Q491lUiFSSbznjktAAMgcuHvJ7MBFAC7w"; path="/"; domain=".google.com"; path_spec; domain_dot; expires="2020-11-12 15:07:25Z"; HttpOnly=None; version=0 |  | ||||||
| Set-Cookie3: CGIC=""; path="/complete/search"; domain=".google.com"; path_spec; domain_dot; expires="2020-11-09 15:07:25Z"; HttpOnly=None; version=0 |  | ||||||
| Set-Cookie3: CGIC=""; path="/search"; domain=".google.com"; path_spec; domain_dot; expires="2020-11-09 15:07:25Z"; HttpOnly=None; version=0 |  | ||||||
							
								
								
									
										52
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								main.py
									
									
									
									
									
								
							| @@ -7,23 +7,25 @@ import json | |||||||
| import datetime | import datetime | ||||||
| import googlesearch | import googlesearch | ||||||
| import emoji | import emoji | ||||||
|  | import Levenshtein as lev | ||||||
|  |  | ||||||
|  |  | ||||||
| from persistence import PersistentVars | from persistence import PersistentVars | ||||||
|  |  | ||||||
| class ChatBot(): | class ChatBot(): | ||||||
|  |  | ||||||
|     def __init__(self, name, api_key, version): |     def __init__(self, name, version): | ||||||
|         """Inits the Bot with a few conf. vars |         """Inits the Bot with a few conf. vars | ||||||
|         Args:   -> name:str - Name of the bot |         Args:   -> name:str - Name of the bot | ||||||
|                 -> api_key:str - t.me api-key |                 -> api_key:str - t.me api-key | ||||||
|                 -> version:str - Version number |                 -> version:str - Version number | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         self.base_url = "https://api.telegram.org/bot" + api_key + "/" |         self.base_url = "https://api.telegram.org/bot" + key.telegram_api + "/" | ||||||
|         self.version = version |         self.version = version | ||||||
|         self.name = name |         self.name = name | ||||||
|  |  | ||||||
|         # Persisten variable |         # Persistent variable | ||||||
|         self.persistence = PersistentVars("permanent_vars.json") |         self.persistence = PersistentVars("permanent_vars.json") | ||||||
|  |  | ||||||
|         # Uptime counter |         # Uptime counter | ||||||
| @@ -162,11 +164,31 @@ class ChatBot(): | |||||||
|         /[command] [argument 1] [argument 2] ... |         /[command] [argument 1] [argument 2] ... | ||||||
|         """ |         """ | ||||||
|         full = command.split(" ") |         full = command.split(" ") | ||||||
|         if full[0] in self.commands: |         command = self.fuzzy_match_command(full[0]) | ||||||
|             self.commands[full[0]](full[1:]) |         if len(command) != 1: | ||||||
|  |             if command[0] == "EXACT": | ||||||
|  |                 self.commands[command[1]](full[1:]) | ||||||
|  |             else: | ||||||
|  |                 send = "Did you mean <code>" + command[1] + "</code>" | ||||||
|  |                 for i in range(2,len(command)): | ||||||
|  |                     send += " or <code>" + command[1] + "</code>" | ||||||
|  |                 send += "?" | ||||||
|  |                 self.send_message(send) | ||||||
|         else: |         else: | ||||||
|             self.send_message("Command <code>" + full[0] + "</code> not found. Please try again.") |             self.send_message("Command <code>" + full[0] + "</code> not found. Please try again.") | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     def fuzzy_match_command(self, input): | ||||||
|  |         matches = ["not exact"] | ||||||
|  |         for command in self.commands.keys(): | ||||||
|  |             if lev.ratio(input.lower(),command) > 0.8: | ||||||
|  |                 matches.append(command) | ||||||
|  |                 if lev.ratio(input.lower(),command) == 1: | ||||||
|  |                     return ["EXACT", command] | ||||||
|  |  | ||||||
|  |         return matches | ||||||
|  |  | ||||||
|  |  | ||||||
|     def send_thinking_note(self): |     def send_thinking_note(self): | ||||||
|         data = { |         data = { | ||||||
|             "chat_id" : self.chat_id, |             "chat_id" : self.chat_id, | ||||||
| @@ -178,6 +200,7 @@ class ChatBot(): | |||||||
|         except: |         except: | ||||||
|             print("Could not show that I'm thinking =(") |             print("Could not show that I'm thinking =(") | ||||||
|  |  | ||||||
|  |  | ||||||
|     def send_message(self, message): |     def send_message(self, message): | ||||||
|         print("SENDING: " + emoji.demojize(message)) |         print("SENDING: " + emoji.demojize(message)) | ||||||
|  |  | ||||||
| @@ -223,9 +246,11 @@ class ChatBot(): | |||||||
|     def bot_print_status(self, params): |     def bot_print_status(self, params): | ||||||
|         """Prints the bots current status and relevant information""" |         """Prints the bots current status and relevant information""" | ||||||
|         delta = str(datetime.datetime.now() - self.start_time) |         delta = str(datetime.datetime.now() - self.start_time) | ||||||
|  |         ip = requests.get('https://api.ipify.org').text | ||||||
|         message = "<pre>Status: Running :green_circle:\n" |         message = "<pre>Status: Running :green_circle:\n" | ||||||
|         message += "Uptime: " + delta + "\n" |         message += "Uptime: " + delta[:delta.rfind(".")] + "\n" | ||||||
|         message += "Reboots: " + str(self.persistence.read("reboots")) + "\n" |         message += "Reboots: " + str(self.persistence.read("reboots")) + "\n" | ||||||
|  |         message += "IP-Adress: " + ip + "\n" | ||||||
|         message += "Messages read: " + str(self.persistence.read("message_read")) + "\n" |         message += "Messages read: " + str(self.persistence.read("message_read")) + "\n" | ||||||
|         message += "Messages sent: " + str(self.persistence.read("message_sent")) + "</pre>" |         message += "Messages sent: " + str(self.persistence.read("message_sent")) + "</pre>" | ||||||
|         self.send_message(message) |         self.send_message(message) | ||||||
| @@ -325,9 +350,10 @@ class ChatBot(): | |||||||
|  |  | ||||||
|     def bot_show_help(self, params): |     def bot_show_help(self, params): | ||||||
|         """Shows a list of all commands and their description""" |         """Shows a list of all commands and their description""" | ||||||
|         send_text = "Hello, this is " + self.name + ", V." + self.version + "\n" |         send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\n" | ||||||
|         send_text += "Here is a list of the available commands:\n" |         send_text += "Here is what I can do up to now: \n" | ||||||
|         for entry in self.commands: |         entries = sorted(list(self.commands.keys())) | ||||||
|  |         for entry in entries: | ||||||
|             send_text += "<b>" + entry + "</b> - " |             send_text += "<b>" + entry + "</b> - " | ||||||
|             send_text += "<code>" + self.commands[entry].__doc__ + "</code>\n\n" |             send_text += "<code>" + self.commands[entry].__doc__ + "</code>\n\n" | ||||||
|         self.send_message(send_text) |         self.send_message(send_text) | ||||||
| @@ -371,9 +397,9 @@ class ChatBot(): | |||||||
|  |  | ||||||
|     def bot_do_all(self,params): |     def bot_do_all(self,params): | ||||||
|         """Executes every single command with random params""" |         """Executes every single command with random params""" | ||||||
|         for key in self.commands: |         for command in self.commands: | ||||||
|             if key != "bot_do_all": |             if command != "bot_do_all": | ||||||
|                 	self.commands[key](["en","Freiburg"]) |                 	self.commands[command](["en","Freiburg"]) | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -418,4 +444,4 @@ class ChatBot(): | |||||||
|  |  | ||||||
|  |  | ||||||
| ####################################################################### | ####################################################################### | ||||||
| bot = ChatBot("ChatterBot", key.telegram_api, version="1.02") | bot = ChatBot("ChatterBot", version="1.03") | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| {"message_read": 0, "message_sent": 0, "log": [], "chat_members": {}, "reboots": 0} | {"message_read": 27, "message_sent": 17, "log": [], "chat_members": {"364520272": "Remy Moll"}, "reboots": 20} | ||||||
		Reference in New Issue
	
	Block a user
	 Remy Moll
					Remy Moll