aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-07-16 12:23:19 +0300
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-07-16 12:23:19 +0300
commit46380f5907c6e8e9e0e655d064dececb0978a3ce (patch)
tree6abf32f1248d82bd6ca9d4750320aab911f8e790
parent68e8ef4f35d8145dd8277e65cb2473e865ace60c (diff)
Add option for chat_id, refine logging
-rw-r--r--botmanager.py31
-rwxr-xr-xradiodiodibot.py15
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()