dearn44
dearn44

Reputation: 3422

Send updates to specific channel without any form of trigger

While events on my algorithm occur, I want to dispatch updates to a specific channel. My code looks something like this

def _create_telegram_dispatcher():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    updater = Updater("TOKEN")

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on non command i.e message - echo the message on Telegram
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

def _run():
    broker = BROKER()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(_client_thread(api))

if __name__ == "__main__":
    self._create_telegram_dispatcher()
    self._run()

So while the broker is running, if a condition is satisfied I would like my bot to send an update on the channel, without needing someone to talk to it first. What is the way to get access to the telegram bot from my initialized Broker class, and have it send a message to a specific channel?

Upvotes: 0

Views: 573

Answers (1)

CallMeStag
CallMeStag

Reputation: 7050

Assuming that you implemented the BROKER class yourself, you could add an argument bot to it and pass updater.bot to it. This would require you to rework _create_telegram_dispatcher a bit, i.e. don't directly call updater.idle() inside the function.

Alternatively, you can just instantiate a new Bot instance inside your broker.

Edit: Once you have a bot instance available, sending messages is as easy as (self.)bot.send_message(…) - see here for more details.

Upvotes: 1

Related Questions