From d954ed175de1dab5bda8c198bc8d7e0b2c552d01 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 18 Jul 2016 19:20:19 +0300 Subject: Add buffer to avoid forwarding duplicate messages from shoutbox --- .gitignore | 5 +++++ botmanager.py | 27 ++++++++++++++++++++++++--- jsonfactory.py | 5 ++++- mockshoutbox.py | 16 +++++++++------- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 72364f9..bd41bb7 100644 --- a/.gitignore +++ b/.gitignore @@ -87,3 +87,8 @@ ENV/ # Rope project settings .ropeproject + +# Vim swap files +*.swp + +.gitignore \ No newline at end of file diff --git a/botmanager.py b/botmanager.py index 48fad3c..e9b65a2 100644 --- a/botmanager.py +++ b/botmanager.py @@ -10,12 +10,16 @@ import logging from jsonfactory import JSONFactory from shoutboxapicommunicator import Communicator +class ObsoleteMessageException(Exception): + pass class BotManager(object): shoutbox_api_url = "http://localhost:8000" telegram_chat_id = "default_id" api_call_interval = 10 + last_message_timestamps = {} + def __init__(self, token): self.token = token @@ -44,14 +48,31 @@ class BotManager(object): self.forward_to_telegram(Communicator.fetch(self.shoutbox_api_url)) time.sleep(self.api_call_interval) + new_message_dict = {} + + for msg_id in self.last_message_timestamps: + + # if the message is older than 2 * call interval, + # pop it from the buffer because there is no way + # it can be a duplicate anymore + timestamp = round(float(self.last_message_timestamps[msg_id])) + if not round(time.time()) - timestamp < 2 * self.api_call_interval: + new_message_dict[msg_id] = self.last_message_timestamps[msg_id] + else: + logging.info("Popped message from buffer.") + + self.last_message_timestamps = new_message_dict + def forward_to_telegram(self, messages): try: for message in messages: - self.bot.sendMessage(self.telegram_chat_id, - "{}: {}".format(message["user"], message["text"])) + if message["id"] not in self.last_message_timestamps: + self.bot.sendMessage(self.telegram_chat_id, + "{}: {}".format(message["user"], message["text"])) + self.last_message_timestamps[message["id"]] = message["timestamp"] + except: logging.warning("Failed to send message to Telegram!") - traceback.print_exc() def default_action(self, chat_id): self.bot.sendMessage(chat_id, "Radio palaa keväällä 2017!") diff --git a/jsonfactory.py b/jsonfactory.py index be70996..6703ef2 100644 --- a/jsonfactory.py +++ b/jsonfactory.py @@ -2,15 +2,18 @@ import json class JSONFactory(object): + running_id = 1 + @staticmethod def make_object(text, user, timestamp, ip): message = json.dumps({ + "id": JSONFactory.running_id, "text": text, "user": user, "timestamp": timestamp, "ip": ip }) - + JSONFactory.running_id += 1 return message @staticmethod diff --git a/mockshoutbox.py b/mockshoutbox.py index 6e6326e..da8e48a 100644 --- a/mockshoutbox.py +++ b/mockshoutbox.py @@ -6,6 +6,8 @@ import traceback from http.server import SimpleHTTPRequestHandler import json +import time + ''' Mock HTTP server to test shoutbox api requests. @@ -23,13 +25,13 @@ class MyHandler(SimpleHTTPRequestHandler): self.send_header("Content-type", "application/json") self.end_headers() - body = """[ - { - \"text\":\"test message\", - \"user\":\"test user\" - } - ]""" - + body = json.dumps([{ + "user": "test user", + "text": "test message", + "timestamp": str(time.time()), + "id": time.time(), + "ip": "1.2.3.4" + }]) self.wfile.write(body.encode()) def do_POST(self): -- cgit v1.3