Exemplo n.º 1
0
    def cb_auth(self, bot, update, args):
        """
            Callback method used to authenticate to the bot
            Sends a telegramm message
        """
        chat_id = update.message.chat.id
        username = update.message.chat.username
        first_name = update.message.chat.first_name
        last_name = update.message.chat.last_name
        LOGGER.info(f"{chat_id} {username} : /auth {args}")
        if len(args) < 1:
            self.send_help(chat_id, "auth")
            return

        botpass = args[0]
        if chat_id in self.userdata:
            userinfo = self.userdata[chat_id]
        else:
            # these are the default setting for new users
            userinfo = {
                "authenticated":
                "no",
                "failed_auth":
                0,
                "disabled_ws_message":
                ["fcs", "fos", "os", "ws", "ps", "fls", "fcu"],
                "getbalance": ["iot", "usd", "btc"]
            }

        # ignore user if he keeps forcing /auth
        if userinfo['failed_auth'] > 20:
            return

        # notify user that there have been to many failed atempts
        if userinfo['failed_auth'] > 10:
            bot.send_message(chat_id, text="you are blocked")
            userinfo["failed_auth"] += 1
            self.userdata[chat_id] = userinfo
            utils.save_userdata(self.userdata)
            return

        if botpass == self.auth_pass:
            message = "<pre>authentication successfull </pre>"
            bot.send_message(chat_id, text=message, parse_mode='HTML')
            userinfo["authenticated"] = "yes"
            userinfo["failed_auth"] = 0
            userinfo["telegram_user"] = username
            userinfo["telegram_name"] = f"{first_name} {last_name}"
        else:
            message = "<pre>bad password</pre>"
            bot.send_message(chat_id, text=message, parse_mode='HTML')
            userinfo["authenticated"] = "no"
            userinfo["failed_auth"] += 1
            userinfo["telegram_user"] = username
            userinfo["telegram_name"] = f"{first_name} {last_name}"

        self.userdata[chat_id] = userinfo
        utils.save_userdata(self.userdata)
Exemplo n.º 2
0
    def _cb_set(self, bot, update, args):
        """
        set default options
        """
        LOGGER.info(f"{update.message.chat.username} : /set {args}")
        chat_id = update.message.chat.id

        if len(args) < 2:
            self.send_help(chat_id, "set")
            return

        name = args[0]
        value = args[1]
        valid_settings = [
            'defaultpair', 'graphtheme', 'calctype', "getbalance"
        ]
        if name not in valid_settings:
            str_settings = " ".join(valid_settings)
            formated_message = ("<pre>"
                                f"{name} is not a valid setting\n"
                                f"valid settings are {str_settings}\n"
                                "</pre>")
            bot.send_message(chat_id, text=formated_message, parse_mode='HTML')
            return

        if name == "defaultpair" and value not in self.btfx_symbols:
            msgtext = f"incorect symbol , available pairs are {self.btfx_symbols}"
            bot.send_message(chat_id, text=msgtext, parse_mode='HTML')
            return

        if name == "graphtheme" and value not in [
                'standard', 'colorblind', 'monochrome'
        ]:
            msgtext = f"incorect theme , available themes are standard, colorblind, monochrome"
            bot.send_message(chat_id, text=msgtext, parse_mode='HTML')
            return

        if name == "getbalance":
            curr_list = []
            for iterator in range(1, len(args)):
                if args[iterator] in self.currencies:
                    curr_list.append(args[iterator])
                else:
                    msgtext = f"{args[iterator]} is not a valid currency"
                    bot.send_message(chat_id, text=msgtext, parse_mode='HTML')
            value = curr_list

        self.userdata[chat_id][name] = value
        utils.save_userdata(self.userdata)

        message = f'<pre>{name} was set to {value}</pre>'
        bot.send_message(chat_id, text=message, parse_mode='HTML')
Exemplo n.º 3
0
    def cb_auth(self, bot, update, args):
        chat_id = update.message.chat.id
        username = update.message.chat.username
        first_name = update.message.chat.first_name
        last_name = update.message.chat.last_name
        LOGGER.info(f"{chat_id} {username} : /auth {args}")
        if len(args) < 1:
            self.send_help(chat_id, "auth")
            return

        botpass = args[0]
        if chat_id in self.userdata:
            userinfo = self.userdata[chat_id]
        else:
            userinfo = {"authenticated": "no", "failed_auth": 0}

        # ignore user if he keeps forcing /auth
        if userinfo['failed_auth'] > 20:
            return

        # notify user that there have been to many failed atempts
        if userinfo['failed_auth'] > 10:
            bot.send_message(chat_id, text="you are blocked")
            userinfo["failed_auth"] += 1
            self.userdata[chat_id] = userinfo
            utils.save_userdata(self.userdata)
            return

        if botpass == self.auth_pass:
            message = "<pre>authentication successfull </pre>"
            bot.send_message(chat_id, text=message, parse_mode='HTML')
            userinfo["authenticated"] = "yes"
            userinfo["failed_auth"] = 0
            userinfo["telegram_user"] = username
            userinfo["telegram_name"] = f"{first_name} {last_name}"
        else:
            message = "<pre>bad password</pre>"
            bot.send_message(chat_id, text=message, parse_mode='HTML')
            userinfo["authenticated"] = "no"
            userinfo["failed_auth"] += 1
            userinfo["telegram_user"] = username
            userinfo["telegram_name"] = f"{first_name} {last_name}"

        self.userdata[chat_id] = userinfo
        utils.save_userdata(self.userdata)
Exemplo n.º 4
0
    def cb_enable(self, bot, update, args):
        LOGGER.info(f"{update.message.chat.username} : /enable {args}")
        chat_id = update.message.chat.id
        userinfo = self.userdata[chat_id]
        if len(args) < 1:
            self.send_help(chat_id, "enable")
            return

        msg_type = args[0]
        if msg_type not in utils.WS_MSG_TYPES:
            types = " ".join(utils.WS_MSG_TYPES)
            message = f"<pre> msg_type is invalid, valid types are {types} </pre>"
            bot.send_message(chat_id, text=message, parse_mode='HTML')
            return
        userinfo["disabled_ws_message"].remove(msg_type)
        types = " ".join(userinfo["disabled_ws_message"])

        message = f"<pre>Done, disabled messages are : {types}</pre>"
        bot.send_message(chat_id, text=message, parse_mode='HTML')

        self.userdata[chat_id] = userinfo
        utils.save_userdata(self.userdata)
Exemplo n.º 5
0
    def cb_option(self, bot, update, args):
        LOGGER.info(f"{update.message.chat.username} : /option {args}")
        chat_id = update.message.chat.id

        if len(args) < 2:
            self.send_help(chat_id, "option")
            return

        optname = args[0]
        optvalue = args[1]
        valid_options = ['defaultpair', 'graphtheme', 'calctype']
        if optname not in valid_options:
            str_options = " ".join(valid_options)
            formated_message = ("<pre>"
                                f"{optname} is not a valid option\n"
                                f"valid options are {str_options}\n"
                                "</pre>")
            bot.send_message(chat_id, text=formated_message, parse_mode='HTML')
            return

        if optname == "defaultpair" and optvalue not in self.btfx_symbols:
            msgtext = f"incorect symbol , available pairs are {self.btfx_symbols}"
            bot.send_message(chat_id, text=msgtext, parse_mode='HTML')
            return

        if optname == "graphtheme" and optvalue not in [
                'standard', 'colorblind', 'monochrome'
        ]:
            msgtext = f"incorect theme , available themes are standard, colorblind, monochrome"
            bot.send_message(chat_id, text=msgtext, parse_mode='HTML')
            return

        self.userdata[chat_id][optname] = optvalue
        utils.save_userdata(self.userdata)

        message = f'<pre>option {optname} was set to {optvalue}</pre>'
        bot.send_message(chat_id, text=message, parse_mode='HTML')
Exemplo n.º 6
0
 def test_save_userdata(self):
     userdata = utils.read_userdata()
     self.assertIsNone(utils.save_userdata(userdata))
     self.assertTrue(glob.glob('data/usersdata.pickle'))
     self.assertTrue(glob.glob('data/usersdata.json'))