diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-07-14 15:40:36 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-07-14 15:40:36 +0300 |
| commit | f1e665927354fa526e6f339d44abd766eaec98cd (patch) | |
| tree | f9412f05ceab340ca3c8c51a23e67a5e94095c22 | |
| parent | 15a3bdebec1ea0f071e581ac8482b56bed13e362 (diff) | |
Add basic functionality and a placeholder response
| -rw-r--r-- | radiodiodibot.py | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/radiodiodibot.py b/radiodiodibot.py index 99a0ef5..5282ae9 100644 --- a/radiodiodibot.py +++ b/radiodiodibot.py @@ -1,23 +1,61 @@ #!/usr/env python -import sys +import sys, argparse, traceback +import time -def get_token_from_argv(): - try: - return sys.argv[1] - except: - print("Please provide the telegram bot token as a command line argument.") - sys.exit() +# Make a globally accessible bot instance +bot = None + +# Try to import telepot +# If not installed, inform user and fail gracefully +try: + import telepot +except: + print("radiodiodibot needs the telepot module to communicate with Telegram.") + print("Please install telepot before using radiodiodibot.") + sys.exit() +def handle(msg): + content_type, chat_type, chat_id = telepot.glance(msg) + print(content_type, chat_type, chat_id) + + if content_type == 'text': + bot.sendMessage(chat_id, "Radio palaa keväällä 2017!") + +# 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: - import telepot + print("Creating bot listener with token {}...".format(token)) + bot = telepot.Bot(token) + print("Bot succesfully created.") except: - print("radiodiodibot needs the telepot module to communicate with Telegram.") - print("Please install telepot before using radiodiodibot.") + traceback.print_exc() + print("Error creating bot listener. radiodiodibot will now exit...") return - token = get_token_from_argv() + # 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) + if __name__ == "__main__": main() |
