Skip to content

[FEATURE] #2148

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

Closed
ImKewal opened this issue Oct 15, 2020 · 6 comments
Closed

[FEATURE] #2148

ImKewal opened this issue Oct 15, 2020 · 6 comments
Labels

Comments

@ImKewal
Copy link

ImKewal commented Oct 15, 2020

Is your feature request related to a problem? Please describe.

I made a global class which extends the functionality of sending message by saving the chat id and message id of message sent by bot. So object of this class will have the list of all the messages sent by bot and also have methods to delete duplicate bot messages. I want to add this object to pickle persistence of bot but there is no way to it. Pickle Persistence class of PTB library only give options to add chat_data, bot_data and user_data to persistence; No option to add user defined objects and variables to the persistence.

Describe the solution you'd like

I think it would be nice if you would add a feature to pass a dictionary as a parameter to the PicklePersistence() method which will add the global variables mentioned in the dictionary to the persistence, and after restarting bot, those variables will be reloaded.

Describe alternatives you've considered

I considered saving those objects to the bot_data of the Callback context while exiting bot and restoring that bot_data copy at every restart of bot; as bot_data get persisted with inbuilt persistence. But that didn't work, as callback context is only accessible inside any handler not in main. So I need to call a dump_handler once before exiting and call a load_handler after the restart. So there should be predefined functions through which user can call any function just after bot start and just before bot stop.
Also bot_data should be available in whole main function or there should be any other *_data free from callback context to which any object can be saved inside main function so it get persisted with default persistence.

@Bibo-Joshi
Copy link
Member

Hi.

A few thoughts:

  1. bot/chat/user_data are vailable in your main as updater.dispatcher.bot/chat/user_data. keep in mind, that updater.dispatcher.chat/user_data are dictionaries containing for each chat/user_id the data available as context.chat/user_data in callbacks for that chat/user.
  2. if you store your custom class as updater.dispatcher.bot_data['some_key'] = my_class_instance and update it in your callbacks, it will be persisted both after each update and on shutdown, so there is no need for you to persist anything manually
  3. we already have [FEATURE] Customization of context logic #2068 open, which will, when implemented, allow you to access your custom class e.g. as context.message_tracker

I think 1.+2. are a good way to achieve what you want for now.

@ImKewal
Copy link
Author

ImKewal commented Oct 15, 2020

  1. Also the predefined function I mentioned, to call any function just after start and just before exiting of bot, will add a great functionality to any bot just like constructor and destructor of any object. These can be used for other functionalities also other the Pickling.

@Bibo-Joshi
Copy link
Member

You can do whatever you want before calling updater.start_polling(), e.g. send a message via updater.bot.send_message. I'm not sure if I see a value in doing something on exit. Persistence is already updated on exit to make sure the data is stored, but the usual concept of bots is that they should not shut down (unless they need an update). If you have a specific use case in mind, please try to describe it :)

@ImKewal
Copy link
Author

ImKewal commented Oct 15, 2020

While exiting we can just add that custom class to updater.dispatcher.bot_data in one step only. No need to update that updater.dispatcher.bot_data class copy every time my object mutate. That will be helpful. Anyways, I will try using your solution and provide feedback.

@Bibo-Joshi
Copy link
Member

Bibo-Joshi commented Oct 15, 2020

Python passes objects by reference:

lst = [1,2]
context.bot_data['list'] = list
lst.append(3)
print(context.bot_data['list']) # this gives [1, 2, 3]

so no need to update the custom object before exit. its current state will be saved.

@ImKewal
Copy link
Author

ImKewal commented Oct 16, 2020

Thanks for help!

@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants