From 46380f5907c6e8e9e0e655d064dececb0978a3ce Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 16 Jul 2016 12:23:19 +0300 Subject: Add option for chat_id, refine logging --- botmanager.py | 31 +++++++++++++++---------------- radiodiodibot.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/botmanager.py b/botmanager.py index eeab4ea..c889e49 100644 --- a/botmanager.py +++ b/botmanager.py @@ -8,8 +8,8 @@ import logging class BotManager(object): - active_chat_ids = [] shoutbox_api_url = "http://localhost:8080" + telegram_chat_id = "default_id" def __init__(self, token): self.token = token @@ -51,23 +51,22 @@ class BotManager(object): def default_action(self, chat_id): self.bot.sendMessage(chat_id, "Radio palaa keväällä 2017!") + def send_to_shoutbox(self, chat_id, text, user): + data = {"message":text, "user":user} + try: + requests.post(self.shoutbox_api_url, data) + logging.info("Sent message from {} to shoutbox.".format(user)) + except: + logging.warning("Failed to send message from {} to shoutbox API!".format(user)) + # Placeholder action for testing commands def now_playing(self, chat_id): songs = ["Ace of Spades", "Mökkitie", "Alpha Russian XXL Night Mixtape", "teekkarihymni"] self.bot.sendMessage(chat_id, "Radiossa soi {}!".format(random.choice(songs))) - def start_transmission(self, chat_id): - self.active_chat_ids.append(chat_id) - - self.bot.sendMessage(chat_id, "OK! Radiodiodibot välittää nyt viestejä huutelulaatikon ja tämän keskustelun välillä.") - - def stop_transmission(self, chat_id): - if chat_id in self.active_chat_ids: - self.active_chat_ids.remove(chat_id) - self.bot.sendMessage(chat_id, "OK! Radiodiodibot lopettaa nyt viestien välittämisen.") - else: - self.bot.sendMessage(chat_id, "Radiodiodibot ei ole aktiivinen tässä keskustelussa.") + def not_supported(self, chat_id): + self.bot.sendMessage(chat_id, "Tätä toimintoa ei ole tuettu.") # Determine which action to take def parse_message(self, msg): @@ -76,16 +75,16 @@ class BotManager(object): if "/nowplaying" in t: self.now_playing(chat_id) elif "/start" in t: - self.start_transmission(chat_id) + self.not_supported(chat_id) elif "/stop" in t: - self.stop_transmission(chat_id) + self.not_supported(chat_id) else: - self.default_action(chat_id) + user_name = msg["from"]["first_name"] + self.send_to_shoutbox(chat_id, t, user_name) # Start parsing the incoming message if it is a text message def handle(self, msg): content_type, chat_type, chat_id = telepot.glance(msg) - logging.info(content_type, chat_type, chat_id) if content_type == 'text': self.parse_message(msg) diff --git a/radiodiodibot.py b/radiodiodibot.py index a9c8c68..6fe394e 100755 --- a/radiodiodibot.py +++ b/radiodiodibot.py @@ -4,6 +4,15 @@ import sys, argparse import botmanager import logging +try: # Python 2.7+ + from logging import NullHandler +except ImportError: + class NullHandler(logging.Handler): + def emit(self, record): + pass + +logging.getLogger(__name__).addHandler(NullHandler()) + logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") # Try to import telepot @@ -22,17 +31,23 @@ def main(): parser = argparse.ArgumentParser(description="Telegram bot for the Radiodiodi Student Radio broadcast.") parser.add_argument("telegram_bot_token", help="Telegram Bot API token") parser.add_argument("-U", "--shoutbox-api-url", help="Shoutbox API URL") + parser.add_argument("-C", "--telegram-chat-id", help="Telegram Chat ID") args = parser.parse_args() token = args.telegram_bot_token shoutbox_api_url = args.shoutbox_api_url + telegram_chat_id = args.telegram_chat_id manager = botmanager.BotManager(token) if (shoutbox_api_url != None): manager.shoutbox_api_url = shoutbox_api_url + if telegram_chat_id != None: + manager.telegram_chat_id = telegram_chat_id + logging.info("Using Shoutbox API URL: {}".format(manager.shoutbox_api_url)) + logging.info("Using Telegram Chat ID: {}".format(manager.telegram_chat_id)) manager.start() -- cgit v1.3