aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjantuomi <jans.tuomi@gmail.com>2016-07-15 12:23:07 +0300
committerjantuomi <jans.tuomi@gmail.com>2016-07-15 12:23:07 +0300
commite4ec27720b022a45d69d4422b68b433d582c3563 (patch)
tree2dc775f675649d5028668a19d7690c41e2b65ad4
parent23bb8cfbf650d11f20a6ce7141e8cce1a6a5f74d (diff)
Add start and stop, rudimentary API request placeholder
-rw-r--r--botmanager.py49
-rw-r--r--radiodiodibot.py23
2 files changed, 52 insertions, 20 deletions
diff --git a/botmanager.py b/botmanager.py
index e704056..eeab4ea 100644
--- a/botmanager.py
+++ b/botmanager.py
@@ -1,17 +1,19 @@
import random
import sys
+import requests
import telepot
import traceback
import time
import logging
+
class BotManager(object):
-
- started = False
+ active_chat_ids = []
+ shoutbox_api_url = "http://localhost:8080"
def __init__(self, token):
self.token = token
-
+
# Attempt to create a bot with telepot
try:
logging.info("Creating bot listener with token {}...".format(token))
@@ -23,6 +25,8 @@ class BotManager(object):
sys.exit(1)
def start(self):
+ self.started = True
+
# Try fetching bot information from Telegram to check connection
try:
logging.info("Bot info: {}".format(self.bot.getMe()))
@@ -33,25 +37,50 @@ class BotManager(object):
self.bot.message_loop(self.handle)
logging.info("Listening for messages...")
while True:
+ self.get_messages_from_shoutbox()
time.sleep(10)
- def default_action(self, msg):
- content_type, chat_type, chat_id = telepot.glance(msg)
+ def get_messages_from_shoutbox(self):
+ try:
+ r = requests.get(self.shoutbox_api_url)
+ logging.info("Response from API OK.")
+ except:
+ logging.warning("Failed to get response from API!")
+ return
+
+ def default_action(self, chat_id):
self.bot.sendMessage(chat_id, "Radio palaa keväällä 2017!")
# Placeholder action for testing commands
- def now_playing(self, msg):
- content_type, chat_type, chat_id = telepot.glance(msg)
+ 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.")
+
# Determine which action to take
def parse_message(self, msg):
t = msg["text"]
+ content_type, chat_type, chat_id = telepot.glance(msg)
if "/nowplaying" in t:
- self.now_playing(msg)
+ self.now_playing(chat_id)
+ elif "/start" in t:
+ self.start_transmission(chat_id)
+ elif "/stop" in t:
+ self.stop_transmission(chat_id)
else:
- self.default_action(msg)
+ self.default_action(chat_id)
# Start parsing the incoming message if it is a text message
def handle(self, msg):
@@ -59,4 +88,4 @@ class BotManager(object):
logging.info(content_type, chat_type, chat_id)
if content_type == 'text':
- self.parse_message(msg) \ No newline at end of file
+ self.parse_message(msg)
diff --git a/radiodiodibot.py b/radiodiodibot.py
index ce8abf5..edc10dc 100644
--- a/radiodiodibot.py
+++ b/radiodiodibot.py
@@ -4,13 +4,7 @@ import sys, argparse
import botmanager
import logging
-logging.basicConfig(level=logging.INFO)
-
-# Make a globally accessible bot instance
-bot = None
-
-# Boolean to track whether the bot has been /started or /stopped
-started = False
+logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
# Try to import telepot
# If not installed, inform user and fail gracefully
@@ -19,20 +13,29 @@ try:
except:
print("radiodiodibot needs the telepot module to communicate with Telegram.")
print("Please install telepot before using radiodiodibot.")
- sys.exit()
+ sys.exit(1)
+
# Entry point
def main():
-
# Get the bot token as an argument from the user
parser = argparse.ArgumentParser(description="Telegram bot for the Radiodiodi Student Radio broadcast.")
- parser.add_argument("telegram_bot_token")
+ parser.add_argument("telegram_bot_token", help="Telegram Bot API token")
+ parser.add_argument("-U", "--shoutbox-api-url", help="Shoutbox API URL")
args = parser.parse_args()
token = args.telegram_bot_token
+ shoutbox_api_url = args.shoutbox_api_url
manager = botmanager.BotManager(token)
+
+ if (shoutbox_api_url != None):
+ manager.shoutbox_api_url = shoutbox_api_url
+
+ logging.info("Using Shoutbox API URL: {}".format(manager.shoutbox_api_url))
+
manager.start()
+
if __name__ == "__main__":
main()