我使用 python-telegram-bot 模塊開發了一個電報機器人。我嘗試運行一個函式而不使用它執行它,dispatcher.run_async(myfunction)但是我如何執行任務,例如從內部發送訊息dispatcher.run_async()?
我的資料庫中有所有用戶 ID。這是我的代碼片段。
import telegram
from telegram.ext import Updater
def myfunction():
# bot.send_message(text='Hello, World',chat_id=<user_id>)
def main():
"""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
dispatcher.run_async(myfunction)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
uj5u.com熱心網友回復:
不確定這是否是預期的方式,但您可以通過將函式傳遞給機器人來傳遞函式run_async:
dispatcher.run_async(myFunction, updater.bot)
def myfunction(bot):
bot.send_message(text='Hello, World',chat_id=123456789)
import telegram
from telegram.ext import Updater
def myfunction(bot):
bot.send_message(text='Hello, World',chat_id=123456789)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater("<MY-BOT-TOKEN>")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
dispatcher.run_async(myfunction, updater.bot)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442724.html
