How to make Python-Telegram_bot send a message without getting a commad?
Asked Answered
D

2

11

I'm Making a telegram bot using Python-Telegram-bot. I wanna make it send a message to one specific user (myself in this case) to select an option. after that, it should take that option as a command and work as usual. but after 30 min... it should send me the same message making me choose an option just like before. How can I make i work?

def start(update: Update, context: CallbackContext) -> None:
    user = update.message.from_user.username
    print(user)
    context.bot.send_message(chat_id=update.effective_chat.id, text= "Choose an option. ('/option1' , '/option 2', '/...')")


def main():

    updater = Updater("<MY-BOT-TOKEN>", use_context=True)

    updater.dispatcher.add_handler(CommandHandler('start', start))

    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

I want this to run without getting the /start command (the update). but after this... there will be normal functions that'll get updates and after 30 min I wanna run this "start" function again without getting an update.

Descendant answered 17/12, 2020 at 23:49 Comment(4)
You've placed your private BOT token in the question. I removed it, but it's still visible in the history. Consider renewing the token at @botFather to prevent other people abusing it!Yamen
I have answered something similar here https://mcmap.net/q/1017829/-telegram-bot-only-allowed-to-post-upon-events. You are on the right track using polling, you need to get and store the chat_id during the first interaction then you can trigger messages from your app (in a background thread -ie every 30 min)Cephalization
@BeppeC What if I want to run start() every 30 minutes? it asks for the missing parameters.Descendant
@BeppeC When you'll run it for the first time... save the update and context in a variable. then pass it as parameters every 30 min. make sure you save the update from the same chat.Descendant
L
19

You can get the bot object either from the updater or the dispatcher:

updater = Updater('<bot-token>')
updater.bot.sendMessage(chat_id='<user-id>', text='Hello there!')

# alternative:
updater.dispatcher.bot.sendMessage(chat_id='<user-id>', text='Hello there!')
Linton answered 30/12, 2020 at 18:54 Comment(0)
A
14

v20.x

Since version v20.x, there has been a switch to asyncio. Updater has been changed, it no longer takes the bot token as a parameter. Instead, using a builder pattern, the ApplicationBuilder is expected to be used to create your telegram bot.

Here are two approaches of sending messages to arbitrary chats.

import asyncio
import telegram
from telegram.ext import ApplicationBuilder

# using telegram.Bot
async def send(chat, msg):
    await telegram.Bot('<bot-token>').sendMessage(chat_id=chat, text=msg)

asyncio.run(send('<chat-id>', 'Hello there!'))

# using ApplicationBuilder
async def send_more(chat, msg):
    application = ApplicationBuilder().token('<bot-token>').build()
    await application.bot.sendMessage(chat_id=chat, text=msg)

asyncio.run(send_more('<chat-id>', 'Hello there!'))
Annabelleannabergite answered 25/10, 2022 at 14:10 Comment(2)
Worked for me, I'm using python-telegram-bot==20.7Cassimere
works but #75708105Sanctified

© 2022 - 2024 — McMap. All rights reserved.