diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-07-15 11:49:03 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-07-15 11:49:03 +0300 |
| commit | 23bb8cfbf650d11f20a6ce7141e8cce1a6a5f74d (patch) | |
| tree | 913d3320e3336a047cf39713c0826d6d25d48624 | |
| parent | a9d28987688ec94b42f5da30a2930a47a1d8d631 (diff) | |
Restructure code into classes
| -rw-r--r-- | botmanager.py | 62 | ||||
| -rw-r--r-- | radiodiodibot.py | 59 |
2 files changed, 69 insertions, 52 deletions
diff --git a/botmanager.py b/botmanager.py new file mode 100644 index 0000000..e704056 --- /dev/null +++ b/botmanager.py @@ -0,0 +1,62 @@ +import random +import sys +import telepot +import traceback +import time +import logging + +class BotManager(object): + + started = False + + def __init__(self, token): + self.token = token + + # Attempt to create a bot with telepot + try: + logging.info("Creating bot listener with token {}...".format(token)) + self.bot = telepot.Bot(token) + logging.info("Bot succesfully created.") + except: + traceback.print_exc() + logging.error("Error creating bot listener. radiodiodibot will now exit...") + sys.exit(1) + + def start(self): + # Try fetching bot information from Telegram to check connection + try: + logging.info("Bot info: {}".format(self.bot.getMe())) + except: + logging.error("Could not fetch bot info. Check your token and connectivity to Telegram!") + sys.exit(1) + + self.bot.message_loop(self.handle) + logging.info("Listening for messages...") + while True: + time.sleep(10) + + def default_action(self, msg): + content_type, chat_type, chat_id = telepot.glance(msg) + 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) + songs = ["Ace of Spades", "Mökkitie", "Alpha Russian XXL Night Mixtape", "teekkarihymni"] + self.bot.sendMessage(chat_id, "Radiossa soi {}!".format(random.choice(songs))) + + # Determine which action to take + def parse_message(self, msg): + t = msg["text"] + if "/nowplaying" in t: + self.now_playing(msg) + else: + self.default_action(msg) + + # 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)
\ No newline at end of file diff --git a/radiodiodibot.py b/radiodiodibot.py index 1e8b428..ce8abf5 100644 --- a/radiodiodibot.py +++ b/radiodiodibot.py @@ -1,7 +1,10 @@ #!/usr/env python -import sys, argparse, traceback -import time, random +import sys, argparse +import botmanager +import logging + +logging.basicConfig(level=logging.INFO) # Make a globally accessible bot instance bot = None @@ -18,66 +21,18 @@ except: print("Please install telepot before using radiodiodibot.") sys.exit() -def default_action(msg): - content_type, chat_type, chat_id = telepot.glance(msg) - bot.sendMessage(chat_id, "Radio palaa keväällä 2017!") - -# Placeholder action for testing commands -def now_playing(msg): - content_type, chat_type, chat_id = telepot.glance(msg) - songs = ["Ace of Spades", "Mökkitie", "Alpha Russian XXL Night Mixtape", "teekkarihymni"] - bot.sendMessage(chat_id, "Radiossa soi {}!".format(random.choice(songs))) - -# Determine which action to take -def parse_message(msg): - t = msg["text"] - if "/nowplaying" in t: - now_playing(msg) - else: - default_action(msg) - -# Start parsing the incoming message if it is a text message -def handle(msg): - content_type, chat_type, chat_id = telepot.glance(msg) - print(content_type, chat_type, chat_id) - - if content_type == 'text': - parse_message(msg) - # Entry point def main(): - global bot - # 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") args = parser.parse_args() token = args.telegram_bot_token - - # Attempt to create a bot with telepot - try: - print("Creating bot listener with token {}...".format(token)) - bot = telepot.Bot(token) - print("Bot succesfully created.") - except: - traceback.print_exc() - print("Error creating bot listener. radiodiodibot will now exit...") - return - - # Try fetching bot information from Telegram to check connection - try: - print("Bot info: {}".format(bot.getMe())) - except: - print("Could not fetch bot info. Check your token and connectivity to Telegram!") - return - - bot.message_loop(handle) - print("Listening for messages...") - while True: - time.sleep(10) + manager = botmanager.BotManager(token) + manager.start() if __name__ == "__main__": main() |
