Issue
I'm a bit confused about this because Im new to python-telegram,-bot, and most examples online dont reflect the v20 changes for Updater and asyncio.
The relevant snippet of code is:
def main() -> None:
persistence_object = PicklePersistence(filepath=persistent_data_file_path)
application = (
ApplicationBuilder()
.token(bot_token)
.persistence(persistence=persistence_object)
.post_init(post_init_handler)
.post_stop(post_stop_handler)
.build()
)
application.run_polling()
async def post_init_handler(application: Application) -> None:
print("init")
bot_id=application.bot.bot.id
# Needed: Code snippet here (details below)
# It produces a list, which is sent as a raw text private message to a static defined user_id
if __name__ == "__main__":
main()
The missing code snippet obtains a list of all chats the bot is in (private, group, supergroup), with some key information about each (see below) and then send that info to a specific user_id as a private message. It doesnt enter polling until that's succeeded. The info it collates and sends is:
(chat id,
username/title of the chat,
chat type (private, group etc),
is the bot the owner of the chat? (if a group/channel),
bot's admin rights in the chat (if a group/channel and admin)
)
So a typical output sent would be:
123456,@fred99,private,(ignored),(ignored)
872981,@technogeek_chat,supergroup,True,(rights data)
(etc)
I cant figure out the correct way to get and send this data at bot startup.
CORRECT API CALL/S TO USE?
All the examples I can find, assume their actions are done reactively within some callback handler as a result of a polled received update, or if done initially, the example uses older Updater/dispatcher techniques not applicable in v20.
I * think * from the Transition guide to v20 thatapplication.create_task
could be the correct answer but if so I dont have a good example to rely on, how I'd use it for this.CORRECT PLACEMENT FOR API CALL/S?
I tried to move the code to betweenapplication...build()
and beforerun_polling()
, but the assignmentbot_id=application.bot.bot.id
failed and I couldnt get that to work. It wanted it initialized, I think, but I wasnt sure what was correct, or which was the more correct place of the two for this code snippet, and left it withinpost_init_handler()
which at least * seemed * to work?GETTING DATA FOR THE CALL/S?
I tried to find code to list the chats the bot is in. The only links were to the example "chatmemberbot.py", and that code doesnt actually detect what chats the bot is in. It relies on adding and removing from a manually maintained list based on updates. Which could potentially desync from actual chats it's in, in some situations (bugs, persist store deleted/corrupted/old store restored, shutdown prior to final flush in the past, ...) and the bot would never know its list wasn't correct.
What's the correct way to do this, please? Is it possible to have an actual example - simplified if it's easier - how it should be done, as I've scoured the docs and can't figure it from those?
Help much appreciated, thank you!!
Solution
CORRECT API CALL/S TO USE?
To simply make a request to Telegram, all you need is a Bot
instance. Please have a look at PTB Introduction for some examples. Note that you already have a Bot
instance available via application.bot
, so there is no need to build one on yowr own. You can simply use it via e.g.
async def post_init_handler(application):
await application.bot.send_message(text="Hello World", chat_id=123)
Application.create_task
is rather unrelated to making requests to Telegram but is a simple convenience function for asynchronous programming with PTB.
CORRECT PLACEMENT FOR API CALL/S?
post_init
is the correct place. This callback is there precisely to run custom logic before the polling of updates starts. This is also explained in the docs of Application.run_polling
GETTING DATA FOR THE CALL/S?
I tried to find code to list the chats the bot is in
This simply does not exist in the Bot API and keeping track manually is the only option. The chat member updates you already found are the most reliable way to do this.
Disclaimer: I'm currently the maintainer of python-telegram-bot
.
Answered By - CallMeStag
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.