aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjantuomi <jans.tuomi@gmail.com>2016-07-14 16:07:53 +0300
committerjantuomi <jans.tuomi@gmail.com>2016-07-14 16:07:53 +0300
commita9d28987688ec94b42f5da30a2930a47a1d8d631 (patch)
tree6aaec7add2148f18d530ef7d1155ff838fc0be69
parentf1e665927354fa526e6f339d44abd766eaec98cd (diff)
Add 'now playing' functionality for testing purposes
-rw-r--r--radiodiodibot.py40
1 files changed, 31 insertions, 9 deletions
diff --git a/radiodiodibot.py b/radiodiodibot.py
index 5282ae9..1e8b428 100644
--- a/radiodiodibot.py
+++ b/radiodiodibot.py
@@ -1,26 +1,48 @@
#!/usr/env python
import sys, argparse, traceback
-import time
+import time, random
# Make a globally accessible bot instance
bot = None
+# Boolean to track whether the bot has been /started or /stopped
+started = False
+
# Try to import telepot
# If not installed, inform user and fail gracefully
try:
- import telepot
+ import telepot
except:
- print("radiodiodibot needs the telepot module to communicate with Telegram.")
- print("Please install telepot before using radiodiodibot.")
- sys.exit()
+ print("radiodiodibot needs the telepot module to communicate with Telegram.")
+ 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':
- bot.sendMessage(chat_id, "Radio palaa keväällä 2017!")
+ parse_message(msg)
# Entry point
def main():
@@ -46,10 +68,10 @@ def main():
# Try fetching bot information from Telegram to check connection
try:
- print("Bot info: {}".format(bot.getMe()))
+ print("Bot info: {}".format(bot.getMe()))
except:
- print("Could not fetch bot info. Check your token and connectivity to Telegram!")
- return
+ print("Could not fetch bot info. Check your token and connectivity to Telegram!")
+ return
bot.message_loop(handle)
print("Listening for messages...")