Man*_*ala 3 python telegram python-telegram-bot telegram-bot
我正在使用 Python-Telegram-bot 制作电报机器人。我想让它向一个特定用户(在这种情况下是我自己)发送一条消息来选择一个选项。之后,它应该将该选项作为命令并照常工作。但是 30 分钟后……它应该向我发送相同的消息,让我像以前一样选择一个选项。我怎样才能让我工作?
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()
Run Code Online (Sandbox Code Playgroud)
我希望它在不获取 /start 命令(更新)的情况下运行。但在此之后......会有正常的功能会得到更新,30分钟后我想再次运行这个“开始”功能而没有得到更新。
Fel*_*ler 10
自该版本以来v20.x,已经切换到asyncio. Updater已更改,它不再将机器人令牌作为参数。相反,使用构建器模式,ApplicationBuilder预计将用于创建您的电报机器人。
以下是向任意聊天发送消息的两种方法。
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!'))
Run Code Online (Sandbox Code Playgroud)
您可以bot从更新程序或调度程序获取对象:
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!')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1926 次 |
| 最近记录: |