aboutsummaryrefslogtreecommitdiffstats
path: root/project
diff options
context:
space:
mode:
Diffstat (limited to 'project')
-rw-r--r--project/botmanager.py16
-rw-r--r--project/shoutboxapicommunicator.py28
2 files changed, 35 insertions, 9 deletions
diff --git a/project/botmanager.py b/project/botmanager.py
index 448dbb8..151e94d 100644
--- a/project/botmanager.py
+++ b/project/botmanager.py
@@ -27,6 +27,7 @@ class BotManager(object):
If not possible, crash gracefully
"""
TelegramCommunicator.token = token
+ self.running_message_id = 1
try:
logging.info("Creating bot listener with token {}...".format(token))
TelegramCommunicator.spawn_bot(token)
@@ -103,13 +104,22 @@ class BotManager(object):
logging.warning("Skipped sending message to shoutbox.")
return
- data = JSONFactory.make_object(text, user_name, date, "null")
- logging.info("Created JSON packet:\n{}".format(data))
- ShoutboxCommunicator.send(data)
+ message = {
+ "user": user_name,
+ "text": text,
+ "id": self.running_message_id,
+ "ip": "null",
+ "timestamp": date
+ }
+ self.running_message_id += 1
+ ShoutboxCommunicator.send(message)
def handle(self, msg):
"""Start parsing the incoming message if it is a text message"""
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'text':
+ logging.info("Got text message.")
self.parse_message(msg)
+ else:
+ logging.info("Got non-text message.")
diff --git a/project/shoutboxapicommunicator.py b/project/shoutboxapicommunicator.py
index 4c7e0c0..c30b408 100644
--- a/project/shoutboxapicommunicator.py
+++ b/project/shoutboxapicommunicator.py
@@ -13,17 +13,29 @@ class ShoutboxCommunicator(BaseCommunicator):
interval = 10
@staticmethod
+ def get_url():
+ return "{}/api/last?seconds={}".format(ShoutboxCommunicator.url, ShoutboxCommunicator.interval)
+
+ @staticmethod
+ def post_url():
+ return ShoutboxCommunicator.url + "/api/post"
+
+ @staticmethod
def fetch():
"""Get and return a list of new messages from the API"""
try:
- r = requests.get(ShoutboxCommunicator.url)
+ r = requests.get(ShoutboxCommunicator.get_url())
logging.info("Response from API OK.")
except:
- logging.warning("Failed to get response from API!")
+ logging.warning("Failed to get response from API! ({})".format(ShoutboxCommunicator.get_url()))
return
- content = json.loads(r.text)
+ try:
+ content = json.loads(r.text)
+ except:
+ logging.warning("Could not parse JSON from request!")
+ return
if len(content) > 0:
logging.info("Messages from shoutbox:")
@@ -34,10 +46,14 @@ class ShoutboxCommunicator(BaseCommunicator):
@staticmethod
def send(data):
- """Send a JSON message to the API"""
+ """Send a message to the API"""
+ post_params = "?user={}&text={}&id={}&ip={}&timestamp={}".format(
+ data["user"], data["text"], data["id"], data["ip"], data["timestamp"]
+ )
+
try:
- requests.post(ShoutboxCommunicator.url, data)
- user = json.loads(data)["user"]
+ requests.post(ShoutboxCommunicator.post_url() + post_params, "")
+ user = data["user"]
logging.info("Sent message from {} to shoutbox.".format(user))
except:
logging.warning("Failed to send message to shoutbox API!")