如何让 Python-Telegram_bot 在没有命令的情况下发送消息? [英] How to make Python-Telegram_bot send a message without getting a commad?

查看:39
本文介绍了如何让 Python-Telegram_bot 在没有命令的情况下发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python-Telegram-bot 制作电报机器人.我想让它向一个特定用户(在这种情况下是我自己)发送一条消息来选择一个选项.之后,它应该将该选项作为命令并照常工作.但是 30 分钟后……它应该向我发送相同的消息,让我像以前一样选择一个选项.我怎样才能让我工作?

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()

我希望它在不获取/start 命令(更新)的情况下运行.但在此之后......会有正常的功能会得到更新,30分钟后我想运行这个开始"无需更新即可再次运行.

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.

推荐答案

您可以从更新程序或调度程序获取 bot 对象:

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!')

这篇关于如何让 Python-Telegram_bot 在没有命令的情况下发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆