aboutsummaryrefslogtreecommitdiffstats
path: root/botmanager.py
blob: dc91a3b1cb7bf6a8f1be2d9ffff84ad7e2127975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import json
import random
import sys
import requests
import telepot
import traceback
import time
import logging


class BotManager(object):
    shoutbox_api_url = "http://localhost:8000"
    telegram_chat_id = "default_id"

    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):
        self.started = True

        # 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:
            self.get_messages_from_shoutbox()
            time.sleep(10)

    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!")
            traceback.print_exc(2)
            return

        content = json.loads(r.text)
        logging.info("Messages:")
        for msg in content:
            logging.info("{}: {}".format(msg["user"], msg["text"]))

    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 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):
        t = msg["text"]
        content_type, chat_type, chat_id = telepot.glance(msg)
        if "/nowplaying" in t:
            self.now_playing(chat_id)
        elif "/start" in t:
            self.not_supported(chat_id)
        elif "/stop" in t:
            self.not_supported(chat_id)
        else:
            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)

        if content_type == 'text':
            self.parse_message(msg)