pyTelegramBotAPI icon indicating copy to clipboard operation
pyTelegramBotAPI copied to clipboard

StatesV2: Support for topics, multibots, and business messages

Open coder2020official opened this issue 1 year ago • 2 comments

Only sync memorystorage working now

coder2020official avatar Jun 23 '24 10:06 coder2020official

@Badiboy I might need your opinion for further development. Have you used states before I ask you a question?

coder2020official avatar Jun 25 '24 07:06 coder2020official

@coder2020official I never used States :(

Badiboy avatar Jun 25 '24 14:06 Badiboy

Finished for sync - only left thing is to add a "bridge" between old and new formats of states to ease switching between these two.

coder2020official avatar Jul 12 '24 17:07 coder2020official

Checklist:

  • [x] Rewrite sync pickle storage to use decorators for locks to follow DRY
  • [x] Add a method for redis storage to migrate from old to new storage type
  • [x] test with old code example to ensure old code compatibility
  • [x] Compare old methods with new methods for all storages to check for return types
  • [x] Check typehints
  • [x] Add comments to needed functions & classes to provide an overview and usage
  • [x] state="*" should check for existing state and not return true in filters; BREAKING
  • [x] Update example to ensure better state experience
  • [x] Discuss the synchronous getMe call with @ badiboy for async
  • [x] Discuss the file structure implementation and file names(e.g states, aio & sync folder names, maybe better asyncio?)
  • [x] Code cleanup
  • [x] Should remove aioredis from optional dependencies: #2195

coder2020official avatar Jul 19 '24 16:07 coder2020official

Pickle storage is the worst out of all. I had some bugs when simultaneously testing my states code. I will investigate the issue and add a warning that pickle should NOT be used for production. Maybe for testing to a small audience.

coder2020official avatar Jul 19 '24 16:07 coder2020official

Breaking changes will be here

  1. state="*" filter will check for existing state, instead of returning True in StateFilter, which is useless;
  2. get_data returns an EMPTY dictionary instead of None if the key does not exist.
  3. State storage format completely changed: in order to avoid state loss, StateRedisStorage().migrate_format(bot_id) should be used (only for redis as it is supposed to be the only storage used for production)
  4. StateContext was renamed into StateDataContext; It should not impact users unless they deliberately use it for some reason(idk); Though in this case none of our examples featured this class directly, nor it was recommended to use

coder2020official avatar Jul 19 '24 16:07 coder2020official

@Badiboy i need your opinion on something I addressed before. Check the comment above(about sync call in async, I tagged you there)

coder2020official avatar Jul 21 '24 17:07 coder2020official

@Badiboy I am nearly done with this PR. I have provided all breaking changes which I believe are all reasonable; If they were not - I provided a way to fix backward compatibility(e.g migrate_format for Redis storage to avoid data loss)

Now, I would like you to review:

  • Folder & file names. I have moved states to a folder called states, with aio and sync folders. Do you think these names are fine? Maybe I should rename "aio" to "asyncio"?
  • the whole code :) (or at least the main files - init.py, async_telebot.py, etc to ensure backward compatibility, etc)

coder2020official avatar Jul 25 '24 13:07 coder2020official

All that is left is cleaning up the code, which I will do after you proceed with the review

coder2020official avatar Jul 25 '24 13:07 coder2020official

@badiboy I have just noticed some unexpected behaviour. It seems like the getMe call during init of the class is slowing down and resulting in connection timeout. Could you please test this out? Take echo bot code with this version of library installed, and try re-starting to see for any side effects.

I think placing a request in init is not a good idea after all. Do you have any ideas on where I can properly get the user object of the bot?((( Polling is one place, but to guarantee work of states(passing bot_id automatically), I need to ensure this object also exists for webhooks

coder2020official avatar Jul 25 '24 14:07 coder2020official

  • Changed token_check -> validate_token, which will just validate token by inspecting the string instead of calling getMe(wasn't a good idea after all); Drawbacks: subject to change in future. I am treating the first part of token as bot id, which is unspecified and may be changed in future, however, a number should still work for states.

coder2020official avatar Jul 26 '24 07:07 coder2020official

@Badiboy I am fully done, need your review

coder2020official avatar Jul 27 '24 10:07 coder2020official

@coder2020official I saw the request but I'm not sure when I'll be able to pass all of this:

image

Especially as far as I never used states.

Badiboy avatar Jul 27 '24 13:07 Badiboy

You can review only crucial files(not new ones, etc), e.g init, async_telebot.py, etc All new files or storages do not require a review

coder2020official avatar Jul 27 '24 13:07 coder2020official

Also I have addressed all breaking changes above

coder2020official avatar Jul 27 '24 14:07 coder2020official

@coder2020official I checked only main files. All related to states are on your responcibility )

Badiboy avatar Jul 28 '24 09:07 Badiboy

Anything else left?

coder2020official avatar Jul 28 '24 09:07 coder2020official

I don't understand your last commits.

image bot_id is available only for ones who used validate_token? Independently if token is valid or not? As I noted before, I propose still trying to get bot_id, but just handle the case when it cannot be extracted. It's not too hard.

Badiboy avatar Jul 28 '24 21:07 Badiboy

You totally removed get_me from sync: image And make it every time in async image

Instead of make it optional in both versions?

Badiboy avatar Jul 28 '24 21:07 Badiboy

I returned them to their original versions. Removing in sync will still cause a request to be done, as self.user will be empty, and it is a property.

Before, async would always call a getme request

coder2020official avatar Jul 29 '24 02:07 coder2020official

For 1), ok will fix it

coder2020official avatar Jul 29 '24 04:07 coder2020official

fixed 1), addressed 2)

coder2020official avatar Jul 30 '24 10:07 coder2020official

LGTM, let's go )

Badiboy avatar Jul 30 '24 22:07 Badiboy

merge?

coder2020official avatar Jul 31 '24 14:07 coder2020official

@badiboy bot api update was released just now. I propose first releasing bot API update, then merge this

coder2020official avatar Jul 31 '24 16:07 coder2020official

Need to change version to 4.23.0

coder2020official avatar Aug 05 '24 11:08 coder2020official

Have nothing against it. Push it here and merge.

Badiboy avatar Aug 05 '24 16:08 Badiboy

I am afk, so we will have to delay this a little No reason to rush anyways

coder2020official avatar Aug 05 '24 17:08 coder2020official

@badiboy done, merging since you approved. Next version will need to be 4.23.0

coder2020official avatar Aug 11 '24 11:08 coder2020official

We will also need a special changelog for next version with changes specifically for states.

coder2020official avatar Aug 11 '24 11:08 coder2020official