From 50779b5ecc55f906bcfd46e54dad70b2c74d3a98 Mon Sep 17 00:00:00 2001 From: jantuomi Date: Tue, 19 Jul 2016 14:20:36 +0300 Subject: Code cleanup --- telegramapicommunicator.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 telegramapicommunicator.py (limited to 'telegramapicommunicator.py') diff --git a/telegramapicommunicator.py b/telegramapicommunicator.py new file mode 100644 index 0000000..169a1ce --- /dev/null +++ b/telegramapicommunicator.py @@ -0,0 +1,47 @@ +import logging + +import sys +import telepot + +from basecommunicator import BaseCommunicator + + +class TelegramCommunicator(BaseCommunicator): + """Class for communicating with the Telegram API""" + + chat_id = "default_id" + bot = None + token = "DEFAULT_TOKEN_123" + + @staticmethod + def spawn_bot(token): + TelegramCommunicator.bot = telepot.Bot(token) + + @staticmethod + def fetch(): + raise Exception("""Attempted to fetch messages from Telegram manually. + Please use telepot bot functionality for listening to messages.""") + + @staticmethod + def send(data): + TelegramCommunicator.send_raw("{}: {}".format(data["user"], data["text"])) + + @staticmethod + def send_raw(message): + try: + TelegramCommunicator.bot.sendMessage(TelegramCommunicator.chat_id, + message) + except: + logging.warning("Could not send message to Telegram chat id {}!".format(TelegramCommunicator.chat_id)) + + @staticmethod + def start_listening(handle): + try: + logging.info("Bot info: {}".format(TelegramCommunicator.bot.getMe())) + except: + logging.error("Could not fetch bot info. Check your token and connectivity to Telegram!") + sys.exit(1) + + TelegramCommunicator.bot.message_loop(handle) + + logging.info("Listening for messages...") -- cgit v1.3