Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: telegram bots command parameters

i am developing a telegram bot. there are commands and in my case it's /leaderboard

commands can have parameters (basically anything following the command).

i would like to add a parameter int for a page and an int param for category. they both are optional.

i was wondering to do something like:

/leaderboard {page} {category}

so i can do:

page, category = text.split(" ")

since both are optional my problem is: how can i get rid of the problem that i don't know if the first arg is referred to page or category (in case page is not specified and left as optional). Because if the user doesn't specify the page, the category takes the first place.

i would like to make it userfriendly.

i was thinking something like:

/leaderboard page={int} categ={int}

and then

for arg in text.split(" "):
    if arg.startswith("page"):
        page = arg.split("=")[1]
    elif arg.startswith("categ"):
        categ = arg.split("=")[1]

i just wrote the code here so it may be wrong, but i am more worried about the concept to use, not if in the code. So i ask if you have solutions better than these. Thanks in advance.

like image 441
91DarioDev Avatar asked Jun 19 '26 13:06

91DarioDev


1 Answers

Try something like this:

In my case i added two varibles and boolean category_only

In try section im checking if there is int in first arg (count = int(args[0]))

If not, in "except ValueError" i set category_only to true and handle this case. I think you should try parse second arg, and use additional variable if there is only one parameter.

@run_async
    def cats_command(self, bot, update, args):
        logging.info(str(update.message.from_user) + ' /cats')
        count, category = 3, ''
        category_only = False
        try:
            if int(args[0]) > 20:
                raise TooMuchCountError('too much')
            if int(args[0]) < 1:
                raise TooSmallCountError('too small')
            count = int(args[0])

        except TooMuchCountError:
            update.message.reply_text(
                TextMessages.too_much_count_error_message.format(count))
        except TooSmallCountError:
            update.message.reply_text(
                TextMessages.too_small_count_error_message.format(args[0]))
        except IndexError:
            update.message.reply_text(
                TextMessages.index_error_message.format(count))
        except ValueError:
            category_only = True

        try:
            if category_only:
                category = str(args[0])
            else:
                category = str(args[1])

            l = map(lambda x: x.name, self.categories)
            if category not in l:
                raise WrongCategoryError('not in list')

        except WrongCategoryError:
            update.message.reply_text(
                TextMessages.worng_category_error_message)
            category = ''
        except IndexError:
            pass

        logging.info(' catting for user {}'.format(update.message.from_user.first_name))
        images = Parser.get_images('xml', count, category)

So, your code:

args = text.split(" ")
category_only = False;
page, category = 0,0
try:
    category = int(args[0])
except ValueError:
    print('there is no good params at all!')
    /* catch this and return*/
try:
    page = int(args[1])
except ValueError:
    category_only = True

You can also avoid boolean variable using page=-1 or another value, and then check it like "if page > -1"

like image 182
FoxPro Avatar answered Jun 22 '26 23:06

FoxPro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!