Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Protected function _update_state #2880

Closed
rejedai opened this issue Feb 1, 2022 · 2 comments
Closed

[QUESTION] Protected function _update_state #2880

rejedai opened this issue Feb 1, 2022 · 2 comments

Comments

@rejedai
Copy link

rejedai commented Feb 1, 2022

Issue I am facing

Why is the _update_state function from ConversationHandler protected?

In my context, there is a certain task that should be performed once a week, and send some participants a "pass" for further dialogue.

At the moment I have to make extra queries to the database in order to save the "pass" and check if there is one every time a person writes something.

However, using _update_state after receiving the object via dispatcher greatly simplifies this task.

Are there any problems related to using this function, will it become invalid in future updates and will I have to rewrite this part of the logic?

Traceback to the issue

No response

Related part of your code

def send_pass(context: CallbackContext, users):
    for user in users:
        dispatcher: Dispatcher = context.dispatcher
        conv: ConversationHandler = dispatcher.handlers[0][0]
        conv._update_state(NextStep.NEXT, (user.id, user.id))

Operating System

Linux

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 13.10 (0.3-1-g9c62773)
Bot API 5.6
certifi 2021.10.08
Python 3.9.9 (main, Dec 28 2021, 07:54:03)  [GCC 11.1.0]
@rejedai rejedai changed the title [QUESTION] [QUESTION] Protected function _update_state Feb 1, 2022
@Bibo-Joshi
Copy link
Member

Why is the _update_state function from ConversationHandler protected?

Because it's part of the internal logic of ConversationHandler and calling it manually has a high potential of breaking currently running conversations - e.g. if you manually update the state while a handler inside that conversation is currently running.

Are there any problems related to using this function, will it become invalid in future updates and will I have to rewrite this part of the logic?

Not unlikely, yes. Overhauling ConversationHandler is one of the bigger todos for after the asyncio switch. See e.g. #2770 .

In my context, there is a certain task that should be performed once a week, and send some participants a "pass" for further dialogue.

At the moment I have to make extra queries to the database in order to save the "pass" and check if there is one every time a person writes something.

You could do the following instead:

  • after making the query, save a flag (e.g. in context.bot_data) for every user where the query was successful/gave the required result
  • In you conversation callbacks, add a check for that flag and depending on that change which state you return from the callback

@rejedai
Copy link
Author

rejedai commented Feb 1, 2022

You could do the following instead:

  • after making the query, save a flag (e.g. in context.bot_data) for every user where the query was successful/gave the required result
  • In you conversation callbacks, add a check for that flag and depending on that change which state you return from the callback

Thank you for the prompt response.

I found another solution, writing the state directly to user_data, via context.dispatcher.user_data[user.id]

@stale stale bot added the stale label Feb 5, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants