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] How to send a message to all the connected users #1836

Closed
floatingpurr opened this issue Mar 19, 2020 · 7 comments
Closed

[QUESTION] How to send a message to all the connected users #1836

floatingpurr opened this issue Mar 19, 2020 · 7 comments
Labels

Comments

@floatingpurr
Copy link

Is there a way to send a message to all the users that are using a bot, possibly, triggered by an external script?

@Bibo-Joshi
Copy link
Member

There is no way, to get all the users via a API method. However, if you're using persistence since lauching your bot, context.dispatcher.chat_data.keys() will be a list (well, actually a dictkeys object) with all the chat ids that triggered an update, that was handled by your bot and similarly for context.dispatcher.user_data.keys(). The important part here is, that users that only sent messages that your bot ignores, wont be listed. Also, there is no guarantee that a user hasn't blocked your bot in the mean time, so you should wrap each send_message in a try ... except, when iterating over all your users.

@floatingpurr
Copy link
Author

floatingpurr commented Mar 19, 2020

Thanx, great! Any caveat about the number of messages per time unit?

And is there a way to trigger those messages from an external script?

@Bibo-Joshi
Copy link
Member

Regarding flood limits, please have a look at our wiki.

What do you mean my "trigger those messages from an external script"?

@floatingpurr
Copy link
Author

Thanx for the pointer to the wiki. Regarding the trigger, I was wondering if there is a good design pattern to trigger the messages from an external script that runs outside of the bot.

@Bibo-Joshi
Copy link
Member

Oh okay. Well, the simples solution would be to add a handler to your bot such that every time you send a message, say "/forward my text", a message "my text" will be send to all users.

If you don't want to interrupt your script, you could create a second updater, that uses the same persistence files as the actual one. Just make sure, not to call updater.start_polling() in the "external" script, as

  1. you can't have two updaters fetching updates at the same time
  2. if you actually handle any of these updates in the external script, you will override the persistence file(s).

Having set up the second updater, you can access the ids via updater.dispatcher.chat_data.keys() and user updater.bot to send the messages. This has two caveats:

  1. Two scripts trying to read the same file(s). Could lead to errors.
  2. in the external script, you will only have the ids from when the persistence files were written last. Depending on your persistence setup (with PicklePersistence, the on_flush parameter is important), this may be either the last handling of an update or even the last time the bot was shut down.

@floatingpurr
Copy link
Author

Oh wow, great hint. I'll start from this : )

Thanks once again!

@Bibo-Joshi
Copy link
Member

Happy to help. I'll close the issue.

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