Fixed persistence, better bot commands.
Better bot structure
This commit is contained in:
		| @@ -1,23 +1,21 @@ | ||||
| import emoji | ||||
| import requests | ||||
| import Levenshtein as lev | ||||
| import datetime | ||||
|  | ||||
| import bot.api.keys | ||||
|  | ||||
|  | ||||
| class TelegramIO(): | ||||
|     def __init__(self, persistence, commands): | ||||
|     def __init__(self, persistence): | ||||
|         """Inits the Telegram-Interface | ||||
|         """ | ||||
|         self.base_url = "https://api.telegram.org/bot" + bot.api.keys.telegram_api + "/" | ||||
|         self.persistence = persistence | ||||
|         self.commands = commands | ||||
|         # Dynamic variables for answering | ||||
|         self.chat_id = "" | ||||
|         self.offset = 0 | ||||
|         self.message_id = "" | ||||
|  | ||||
|         self.message_queue = [] | ||||
|  | ||||
|  | ||||
|     def update_commands(self,commands): | ||||
| @@ -35,38 +33,37 @@ class TelegramIO(): | ||||
|         try: | ||||
|             result = requests.post(update_url,data=data) | ||||
|             result = result.json()["result"] | ||||
|             self.message_queue = result | ||||
|         except: | ||||
|             result = "" | ||||
|             result = [] | ||||
|  | ||||
|         return result | ||||
|         return len(result) | ||||
|  | ||||
|  | ||||
|     def handle_result(self, result): | ||||
|         """Inspects the message and reacts accordingly. Can easily be extended""" | ||||
|         message_data = result[0] | ||||
|     def process_message(self): | ||||
|         """Inspects the first message from self.message_queue and reacts accordingly.""" | ||||
|         message_data = self.message_queue.pop(0) | ||||
|  | ||||
|         self.persistence["bot"]["messages_read"] += 1 | ||||
|         self.offset = message_data["update_id"] + 1 | ||||
|  | ||||
|         if "edited_message" in message_data: | ||||
|             return "nothing", "happened" | ||||
|             return | ||||
|  | ||||
|         message = message_data["message"] | ||||
|         self.message_id = message["message_id"] | ||||
|         self.chat_id = message["chat"]["id"] | ||||
|         author = message["from"] | ||||
|  | ||||
|         chat_members = self.persistence["bot"]["chat_members"] | ||||
|         if str(author["id"]) not in chat_members: | ||||
|         if author["id"] not in self.persistence["bot"]["chat_members"]: | ||||
|             name = "" | ||||
|             if "first_name" in author: | ||||
|                 name += author["first_name"] + " " | ||||
|             if "last_name" in author: | ||||
|                 name += author["last_name"] | ||||
|             if len(name) == 0: | ||||
|                 name += "anonymous" | ||||
|             chat_members[author["id"]] = name | ||||
|             self.persistence["bot"]["chat_members"] =  chat_members | ||||
|                 name = "anonymous" | ||||
|             self.persistence["bot"]["chat_members"][author["id"]] = name | ||||
|             self.send_message("Welcome to this chat " + name + "!") | ||||
|  | ||||
|         if "text" in message: | ||||
| @@ -75,45 +72,12 @@ class TelegramIO(): | ||||
|             if "entities" in message: | ||||
|                 for entry in message["entities"]: | ||||
|                     if entry["type"] == "bot_command": | ||||
|                         return self.handle_command(message["text"][1:]) | ||||
|                         return message["text"] #self.handle_command(message["text"][1:]) | ||||
|  | ||||
|         elif "photo" in message: | ||||
|             print("Photo received, what do I do?") | ||||
|  | ||||
|         return "nothing", "happened" | ||||
|  | ||||
|  | ||||
|     def handle_command(self, command): | ||||
|         """Handles commands and stuff, using a bash-like syntax: | ||||
|         /[command] [argument 1] [argument 2] ... | ||||
|         """ | ||||
|         full = command.split(" ") | ||||
|         command = self.fuzzy_match_command(full[0]) | ||||
|         if len(command) != 1: | ||||
|             if command[0] == "EXACT": | ||||
|                 self.persistence["bot"]["commands_executed"] += 1 | ||||
|                 return 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: | ||||
|             self.send_message("Command <code>" + full[0] + "</code> not found. Please try again.") | ||||
|  | ||||
|         return "nothing", ["happened"] | ||||
|  | ||||
|  | ||||
|     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 | ||||
|         return | ||||
|  | ||||
|  | ||||
|     def send_thinking_note(self): | ||||
| @@ -154,7 +118,7 @@ class TelegramIO(): | ||||
|             out += " @ " + "telegram.send_message" | ||||
|             out += " --> " + "did not send:\n" + message | ||||
|             self.persistence["bot"]["log"] += [out] | ||||
|  | ||||
|              | ||||
|  | ||||
|     def send_photo(self, url, caption): | ||||
|         print("SENDING PHOTO: " + url) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Remy Moll
					Remy Moll