diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-07-18 12:40:45 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-07-18 12:40:45 +0300 |
| commit | 1aac753ba2e95276af63bbaaedae9c2e89cb91c2 (patch) | |
| tree | f2b71ebffd789f157df9f429e08ba7474f2d14a0 | |
| parent | aa526d6d1b34113c2fdc6a97d6f46bc41b08713b (diff) | |
Create JSON Factory, http test server now dumps incoming telegram messages
| -rw-r--r-- | botmanager.py | 18 | ||||
| -rw-r--r-- | jsonfactory.py | 14 | ||||
| -rw-r--r-- | mockshoutbox.py | 27 | ||||
| -rw-r--r-- | shoutboxapicommunicator.py | 10 |
4 files changed, 53 insertions, 16 deletions
diff --git a/botmanager.py b/botmanager.py index 2002b7e..4d3fd86 100644 --- a/botmanager.py +++ b/botmanager.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- -import json import random import sys -import requests import telepot import traceback import time import logging -import shoutboxapicommunicator + +from jsonfactory import JSONFactory +from shoutboxapicommunicator import Communicator class BotManager(object): @@ -17,7 +17,6 @@ class BotManager(object): def __init__(self, token): self.token = token - self.api_comm = shoutboxapicommunicator.Communicator() # Attempt to create a bot with telepot try: @@ -41,7 +40,8 @@ class BotManager(object): logging.info("Listening for messages...") while True: - self.api_comm.fetch(self.shoutbox_api_url) + # TODO send to telegram + Communicator.fetch(self.shoutbox_api_url) time.sleep(10) def default_action(self, chat_id): @@ -66,9 +66,13 @@ class BotManager(object): self.not_supported(chat_id) elif "/stop" in t: self.not_supported(chat_id) - elif chat_id == self.telegram_chat_id: + elif str(chat_id) == self.telegram_chat_id.strip(): user_name = msg["from"]["first_name"] - self.api_comm.send(self.shoutbox_api_url, t, user_name) + data = JSONFactory.make(t, user_name, msg["date"], "null") + logging.info("Created JSON packet:\n{}".format(data)) + Communicator.send(self.shoutbox_api_url, data) + else: + logging.info("Got non-forwarded message from chat_id: {}".format(chat_id)) # Start parsing the incoming message if it is a text message def handle(self, msg): diff --git a/jsonfactory.py b/jsonfactory.py new file mode 100644 index 0000000..7cb316c --- /dev/null +++ b/jsonfactory.py @@ -0,0 +1,14 @@ +import json + + +class JSONFactory(object): + @staticmethod + def make(text, user, timestamp, ip): + message = json.dumps({ + "text": text, + "user": user, + "timestamp": timestamp, + "ip": ip + }) + + return message diff --git a/mockshoutbox.py b/mockshoutbox.py index 2399ba5..7ab1bbc 100644 --- a/mockshoutbox.py +++ b/mockshoutbox.py @@ -2,14 +2,13 @@ import sys import http.server +import traceback from http.server import SimpleHTTPRequestHandler +import json ''' Mock HTTP server to test shoutbox api requests. - -Copyright: linuxjournal.com -Source: http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python ''' class MyHandler(SimpleHTTPRequestHandler): @@ -33,9 +32,27 @@ class MyHandler(SimpleHTTPRequestHandler): self.wfile.write(body.encode()) + def do_POST(self): + content_len = int(self.headers.get('Content-Length')) + post_data = self.rfile.read(content_len) + + try: + message = json.loads(post_data.decode('utf-8')) + print("{}: {}".format(message["user"], message["text"])) + # Begin the response + self.send_response(200) + + except KeyError: + traceback.print_exc() + self.send_response(400) + + self.end_headers() + + return + HandlerClass = MyHandler -ServerClass = http.server.HTTPServer -Protocol = "HTTP/1.0" +ServerClass = http.server.HTTPServer +Protocol = "HTTP/1.0" if sys.argv[1:]: port = int(sys.argv[1]) diff --git a/shoutboxapicommunicator.py b/shoutboxapicommunicator.py index 57f6bc7..28a5b74 100644 --- a/shoutboxapicommunicator.py +++ b/shoutboxapicommunicator.py @@ -6,7 +6,8 @@ import logging class Communicator(object): - def fetch(self, url): + @staticmethod + def fetch(url): try: r = requests.get(url) logging.info("Response from API OK.") @@ -22,10 +23,11 @@ class Communicator(object): return content - def send(self, url, text, user): - data = {"text": text, "user": user} + @staticmethod + def send(url, data): try: requests.post(url, data) + user = json.loads(data)["user"] logging.info("Sent message from {} to shoutbox.".format(user)) except: - logging.warning("Failed to send message from {} to shoutbox API!".format(user))
\ No newline at end of file + logging.warning("Failed to send message to shoutbox API!")
\ No newline at end of file |
