0

My intention is to develop a bot with pyTelegramBotAPI, in which the internal state 'state' allows or disallows access to certain commands.

The expected output in the fragment is the message only if the command is 'plan' and the 'state' is 0

My code:

import telebot

TOKEN = 'TOKEN'
tb = telebot.TeleBot(TOKEN)
state = 0


@tb.message_handler(commands=['plan'], func=lambda state: state == 0)
def planFunct(m):
    idCon = m.chat.id
    tb.send_message(idCon, 'El comando plan funciona.' + str(idCon))


tb.polling(none_stop=True)

The result of this fragment is no way out. And I am not able to recognize if the problem starts from a bad use of filters, scope of variables, or misuse of lambda function.

I appreciate the help, this is my first question in stack overflow.

Regards

API: https://github.com/eternnoir/pyTelegramBotAPI

1 Answer 1

1

A possible solution to the problem, is to use a common function through def, the problem lies in the difficulty to pass a parameter to the function when applying the syntax of the filter 'func'.

To use the parameter, we will assign its value inside the function, because it has scope to obtain it.

My Code:

import telebot

TOKEN = 'TOKEN'
tb = telebot.TeleBot(TOKEN)
satate = 0


def comprobarEstados(m):
    m = state
    if m == 0:
        return True


@tb.message_handler(commands=['plan'], func=comprobarEstados)
def planFunct(m):
    idCon = m.chat.id
    tb.send_message(idCon, "El comando plan funciona.")


tb.polling(none_stop=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.