Fixed reddit paramater matching and upstream errors
This commit is contained in:
		| @@ -42,9 +42,14 @@ class TelegramIO(): | |||||||
|  |  | ||||||
|     def handle_result(self, result): |     def handle_result(self, result): | ||||||
|         """Inspects the message and reacts accordingly. Can easily be extended""" |         """Inspects the message and reacts accordingly. Can easily be extended""" | ||||||
|         for message_data in result: |         message_data = result[0] | ||||||
|  |  | ||||||
|         self.persistence.increment("messages_read") |         self.persistence.increment("messages_read") | ||||||
|         self.offset = message_data["update_id"] + 1 |         self.offset = message_data["update_id"] + 1 | ||||||
|  |  | ||||||
|  |         if "edited_message" in message_data: | ||||||
|  |             return "nothing", "happened" | ||||||
|  |  | ||||||
|         message = message_data["message"] |         message = message_data["message"] | ||||||
|         self.message_id = message["message_id"] |         self.message_id = message["message_id"] | ||||||
|         self.chat_id = message["chat"]["id"] |         self.chat_id = message["chat"]["id"] | ||||||
| @@ -98,6 +103,7 @@ class TelegramIO(): | |||||||
|  |  | ||||||
|         return "nothing", ["happened"] |         return "nothing", ["happened"] | ||||||
|  |  | ||||||
|  |  | ||||||
|     def fuzzy_match_command(self, input): |     def fuzzy_match_command(self, input): | ||||||
|         matches = ["not exact"] |         matches = ["not exact"] | ||||||
|         for command in self.commands.keys(): |         for command in self.commands.keys(): | ||||||
| @@ -122,6 +128,10 @@ class TelegramIO(): | |||||||
|  |  | ||||||
|  |  | ||||||
|     def send_message(self, message): |     def send_message(self, message): | ||||||
|  |  | ||||||
|  |         if message == "": | ||||||
|  |             return | ||||||
|  |  | ||||||
|         print("SENDING: " + emoji.demojize(message)) |         print("SENDING: " + emoji.demojize(message)) | ||||||
|  |  | ||||||
|         data = { |         data = { | ||||||
| @@ -130,15 +140,17 @@ class TelegramIO(): | |||||||
|             "parse_mode": "HTML", |             "parse_mode": "HTML", | ||||||
|             "reply_to_message_id" : self.message_id, |             "reply_to_message_id" : self.message_id, | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         send_url = self.base_url + "sendMessage" |         send_url = self.base_url + "sendMessage" | ||||||
|         try: |         try: | ||||||
|             r = requests.post(send_url, data=data) |             r = requests.post(send_url, data=data) | ||||||
|         except: |             print(r.status_code) | ||||||
|             log = self.persistence.read("log") |  | ||||||
|             log.append(str(datetime.datetime.now()) + " - did not send:\n" + message) |  | ||||||
|             self.persistence.write("log", log) |  | ||||||
|  |  | ||||||
|             self.persistence.increment("messages_sent") |             self.persistence.increment("messages_sent") | ||||||
|  |         except: | ||||||
|  |             out = datetime.datetime.now().strftime("%d.%m.%y - %H:%M") | ||||||
|  |             out += " @ " + "telegram.send_message" | ||||||
|  |             out += " --> " + "did not send:\n" + message | ||||||
|  |             self.persistence.append_list("log", out) | ||||||
|  |  | ||||||
|  |  | ||||||
|     def send_photo(self, url, caption): |     def send_photo(self, url, caption): | ||||||
| @@ -153,9 +165,9 @@ class TelegramIO(): | |||||||
|         send_url = self.base_url + "sendPhoto" |         send_url = self.base_url + "sendPhoto" | ||||||
|         try: |         try: | ||||||
|             r = requests.post(send_url, data=data) |             r = requests.post(send_url, data=data) | ||||||
|         except: |  | ||||||
|             log = self.persistence.read("log") |  | ||||||
|             log.append(str(datetime.datetime.now()) + " - did not send:\n" + url) |  | ||||||
|             self.persistence.write("log", log) |  | ||||||
|  |  | ||||||
|             self.persistence.increment("photos_sent") |             self.persistence.increment("photos_sent") | ||||||
|  |         except: | ||||||
|  |             out = datetime.datetime.now().strftime("%d.%m.%y - %H:%M") | ||||||
|  |             out += " @ " + "telegram.send_photo" | ||||||
|  |             out += " --> " + "did not send:\n" + url | ||||||
|  |             self.persistence.append_list("log", out) | ||||||
|   | |||||||
							
								
								
									
										97
									
								
								bot/main.py
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								bot/main.py
									
									
									
									
									
								
							| @@ -110,9 +110,19 @@ class ChatBot(): | |||||||
|                 string_emoji += letter |                 string_emoji += letter | ||||||
|         return string_emoji |         return string_emoji | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     def write_bot_log(self, function_name, error_message): | ||||||
|  |         """""" | ||||||
|  |         out = datetime.datetime.now().strftime("%d.%m.%y - %H:%M") | ||||||
|  |         out += " @ " + function_name | ||||||
|  |         out += " --> " + error_message | ||||||
|  |         self.persistence.append_list("log", out) | ||||||
|  |  | ||||||
|  |  | ||||||
|     ############################################################################ |     ############################################################################ | ||||||
|     """BOT-Commands: implementation""" |     """BOT-Commands: implementation""" | ||||||
|  |  | ||||||
|  |  | ||||||
|     def bot_print_lorem(self, *args): |     def bot_print_lorem(self, *args): | ||||||
|         """Prints a placeholder text.""" |         """Prints a placeholder text.""" | ||||||
|  |  | ||||||
| @@ -208,7 +218,7 @@ class ChatBot(): | |||||||
|     def bot_show_help(self, *args): |     def bot_show_help(self, *args): | ||||||
|         """Shows a list of all commands and their description""" |         """Shows a list of all commands and their description""" | ||||||
|         description = False |         description = False | ||||||
|         if "verbose" in args: |         if "full" in args: | ||||||
|             description = True |             description = True | ||||||
|  |  | ||||||
|         send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\n" |         send_text = "BeebBop, this is " + self.name + " (V." + self.version + ")\n" | ||||||
| @@ -226,7 +236,7 @@ class ChatBot(): | |||||||
|  |  | ||||||
|     def bot_print_log(self, *args): |     def bot_print_log(self, *args): | ||||||
|         """Shows an error-log, mostly of bad api-requests""" |         """Shows an error-log, mostly of bad api-requests""" | ||||||
|         if "clear" in params: |         if "clear" in args: | ||||||
|             self.persistence.write("log",[]) |             self.persistence.write("log",[]) | ||||||
|             self.telegram.send_message("Log cleared") |             self.telegram.send_message("Log cleared") | ||||||
|             return |             return | ||||||
| @@ -260,7 +270,7 @@ class ChatBot(): | |||||||
|  |  | ||||||
|     def bot_zvv(self, *args): |     def bot_zvv(self, *args): | ||||||
|         """Uses the swiss travel api to return the best route between a start- and endpoint. |         """Uses the swiss travel api to return the best route between a start- and endpoint. | ||||||
|         usage: <start> to <finish>""" |         usage: 'start' to 'finish'""" | ||||||
|         if len(args) <= 3: |         if len(args) <= 3: | ||||||
|             return "Please specify a start- and endpoint as well as a separator (the 'to')" |             return "Please specify a start- and endpoint as well as a separator (the 'to')" | ||||||
|  |  | ||||||
| @@ -303,42 +313,48 @@ class ChatBot(): | |||||||
|  |  | ||||||
|  |  | ||||||
|     def match_reddit_params(self, *args): |     def match_reddit_params(self, *args): | ||||||
|         """matches a list of two elements to one int and one string |         """ matches a list of two elements to a dict | ||||||
|         returns int, string or invalid, invalid |             returns: {"int": number, "str": name} | ||||||
|         """ |         """ | ||||||
|         args = list(args) |         r = {"int": 1, "str": "default"} | ||||||
|  |         print(args) | ||||||
|         if len(args) == 2: |         if len(args) == 2: | ||||||
|             p1 = args[0], p2 = args[1] |             p1, p2 = args[0], args[1] | ||||||
|             try: |             try: | ||||||
|                 try: |                 try: | ||||||
|                     r1 = int(p1) |                     r1 = int(p1) | ||||||
|                     r2 = p2 |                     r2 = p2 | ||||||
|                 except: |                 except: | ||||||
|                     r2 = int(p2) |                     r1 = int(p2) | ||||||
|                     r1 = p1 |                     r2 = p1 | ||||||
|             except: |  | ||||||
|                 return None |                 r["int"] = r1 | ||||||
|  |                 r["str"] = r2 | ||||||
|  |  | ||||||
|  |             except: | ||||||
|  |                 self.write_bot_log("match_reddit_params", "could not match given params to known pattern") | ||||||
|  |  | ||||||
|             return [r1, r2] |  | ||||||
|         elif len(args) == 1: |         elif len(args) == 1: | ||||||
|             try: |             try: | ||||||
|                 r1 = int(args[0]) |                 try: | ||||||
|  |                     r["int"] = int(args[0]) | ||||||
|                 except: |                 except: | ||||||
|                 return None |                     r["str"] = args[0] | ||||||
|             return [r1] |             except: | ||||||
|  |                 self.write_bot_log("match_reddit_params", "could not match given params to known pattern") | ||||||
|  |  | ||||||
|  |         return r | ||||||
|  |  | ||||||
|  |  | ||||||
|     def bot_tell_joke(self, *args): |     def bot_tell_joke(self, *args): | ||||||
|         """Tells you the top joke on r/jokes""" |         """Tells you the top joke on r/jokes""" | ||||||
|  |  | ||||||
|         number = 1 |  | ||||||
|         params_sorted = self.match_reddit_params(*args) |         params_sorted = self.match_reddit_params(*args) | ||||||
|         if params_sorted != None: |  | ||||||
|             if len(params_sorted) >= 1: |  | ||||||
|                 number = params_sorted[0] |  | ||||||
|             if len(params_sorted) > 1: |  | ||||||
|                 self.telegram.send_message("Please only specify one argument: the number of jokes") |  | ||||||
|  |  | ||||||
|  |         number = params_sorted["int"] | ||||||
|  |  | ||||||
|  |         if len(params_sorted) > 1: | ||||||
|  |             self.telegram.send_message("Ignored other params than number of jokes") | ||||||
|  |  | ||||||
|         joke = reddit.get_random_rising("jokes", number, "text") |         joke = reddit.get_random_rising("jokes", number, "text") | ||||||
|         return joke |         return joke | ||||||
| @@ -346,51 +362,52 @@ class ChatBot(): | |||||||
|  |  | ||||||
|     def bot_send_meme(self, *args): |     def bot_send_meme(self, *args): | ||||||
|         """Sends a meme from r/""" |         """Sends a meme from r/""" | ||||||
|         subreddit_name = "memes" |  | ||||||
|         subnames = { |         subnames = { | ||||||
|  |             "default" : "memes", # general case | ||||||
|             "physics" : "physicsmemes", |             "physics" : "physicsmemes", | ||||||
|             "dank" : "dankmemes", |             "dank" : "dankmemes", | ||||||
|             "biology" : "biologymemes", |             "biology" : "biologymemes", | ||||||
|             "math" : "mathmemes" |             "math" : "mathmemes" | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         number = 1 |  | ||||||
|         params_sorted = self.match_reddit_params(*args) |         params_sorted = self.match_reddit_params(*args) | ||||||
|         if params_sorted != None: |  | ||||||
|             if len(params_sorted) >= 1: |         number = params_sorted["int"] | ||||||
|                 number = params_sorted[0] |         if params_sorted["str"] in subnames: | ||||||
|             if len(params_sorted) >= 2: |             subreddit_name = subnames[params_sorted["str"]] | ||||||
|                 subreddit_name = params_sorted[1] |         else: | ||||||
|             if len(args) > 2: |             subreddit_name = subnames["default"] | ||||||
|                 self.telegram.send_message("Memes takes 2 parameters: the number of memes, and their topic.") |  | ||||||
|  |  | ||||||
|         urls = reddit.get_random_rising(subreddit_name, number, "photo") |         urls = reddit.get_random_rising(subreddit_name, number, "photo") | ||||||
|         for u in urls: |         for u in urls: | ||||||
|             try: |             try: | ||||||
|                 self.telegram.send_photo(u["image"], u["caption"]) |                 self.telegram.send_photo(u["image"], u["caption"]) | ||||||
|                 return "" |  | ||||||
|             except: |             except: | ||||||
|  |                 self.write_bot_log("bot_send_meme", "could not send image") | ||||||
|                 return "Meme won't yeet" |                 return "Meme won't yeet" | ||||||
|  |  | ||||||
|  |         return "" | ||||||
|  |  | ||||||
|  |  | ||||||
|     def bot_send_news(self, *args): |     def bot_send_news(self, *args): | ||||||
|         """Sends the first entries for new from r/""" |         """Sends the first entries for new from r/""" | ||||||
|         subnames = { |         subnames = { | ||||||
|  |             "default" : "worldnews", | ||||||
|             "germany" : "germannews", |             "germany" : "germannews", | ||||||
|             "france" : "francenews", |             "france" : "francenews", | ||||||
|             "europe" : "eunews", |             "europe" : "eunews", | ||||||
|             "usa" : "usanews" |             "usa" : "usanews" | ||||||
|         } |         } | ||||||
|         if len(params) == 0: |  | ||||||
|             subreddit_name = "worldnews" |  | ||||||
|         else: |  | ||||||
|         params_sorted = self.match_reddit_params(*args) |         params_sorted = self.match_reddit_params(*args) | ||||||
|             if params_sorted != None: |  | ||||||
|                 if len(params_sorted) >= 1: |  | ||||||
|                     number = params_sorted[0] |  | ||||||
|                 if len(params_sorted) > 1: |  | ||||||
|                     return "Please only specify one argument: the location" |  | ||||||
|  |  | ||||||
|  |         number = params_sorted["int"] | ||||||
|  |         if params_sorted["str"] in subnames: | ||||||
|  |             subreddit_name = subnames[params_sorted["str"]] | ||||||
|  |         else: | ||||||
|  |             subreddit_name = subnames["default"] | ||||||
|  |  | ||||||
|         text = reddit.get_top(subreddit_name, 10, "text") |         text = reddit.get_top(subreddit_name, number, "text") | ||||||
|         return text |         return text | ||||||
|   | |||||||
| @@ -41,7 +41,5 @@ class ModuleWrapper(): | |||||||
|  |  | ||||||
|     def react_hw_command(self, command, params): |     def react_hw_command(self, command, params): | ||||||
|         """""" |         """""" | ||||||
|         # Oh yeah, that needs to be changed |  | ||||||
|         # so params is a list, and so, to pass the commands, we need to unpack it: |         # so params is a list, and so, to pass the commands, we need to unpack it: | ||||||
|         # should work fine |  | ||||||
|         self.hw_commands[command](*params) |         self.hw_commands[command](*params) | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ class Variables(): | |||||||
|         self.savefile = {} |         self.savefile = {} | ||||||
|  |  | ||||||
|     def write(self, name, value): |     def write(self, name, value): | ||||||
|         pre = self.read() |         pre = self.read("") | ||||||
|         pre[self.module][name] = value |         pre[self.module][name] = value | ||||||
|         try: |         try: | ||||||
|             file = open(self.path,"w") |             file = open(self.path,"w") | ||||||
| @@ -26,7 +26,7 @@ class Variables(): | |||||||
|             print("Config not written - critical") |             print("Config not written - critical") | ||||||
|  |  | ||||||
|  |  | ||||||
|     def read(self, name=""): |     def read(self, name): | ||||||
|         if self.last_action == "read": |         if self.last_action == "read": | ||||||
|             if name == "": |             if name == "": | ||||||
|                 return self.savefile |                 return self.savefile | ||||||
| @@ -51,7 +51,13 @@ class Variables(): | |||||||
|         return vars |         return vars | ||||||
|  |  | ||||||
|  |  | ||||||
|     def increment(self, name=""): |     def increment(self, name, inc=1): | ||||||
|         pre = self.read(name) |         pre = self.read(name) | ||||||
|         plus1 = pre + 1 |         plus1 = pre + inc | ||||||
|         self.write(name, plus1) |         self.write(name, plus1) | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     def append_list(self, name, value): | ||||||
|  |         pre = self.read(name) | ||||||
|  |         pre.append(value) | ||||||
|  |         self.write(name, pre) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Remy Moll
					Remy Moll