簡體   English   中英

python-telegram-bot 向特定 ID 發送消息

[英]Send a message to a specific ID python-telegram-bot

我想在初始化時向機器人的維護者(特定 ID)發送一條消息。 所有腳本都是用處理程序制作的,我不能讓它同時使用兩種發送消息的方法:

def main() -> None:
    """Start the bot."""
    # Create the Application and pass it your bot's token.
    application = Application.builder().token("TOKEN").build()
    
    application.bot.send_message("SOME ID","Hello! I am working.") # Problematic line

    # Handlers
    application.add_handler(CommandHandler("command", command_function))
    ...
    
    # on non command i.e message - echo the message on Telegram
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, default_command))
    
    # Run the bot until the user presses Ctrl-C
    application.run_polling()

如果我嘗試運行之前的腳本,我會收到以下警告:

RuntimeWarning: coroutine 'ExtBot.send_message' was never awaited
  application.bot.send_message("SOME ID","Hello! I am working.") # Problematic line

我猜想問題出在兩種發送消息的方法之間。 但是有什么辦法可以同時使用兩者嗎? (或者至少一次,僅當機器人初始化時)

要在啟動時運行異步代碼, Application(Builder) class 提供了post_init掛鈎。 該文檔還包括一個示例,說明如何使用此掛鈎在啟動時調用機器人方法。


免責聲明:我目前是python-telegram-bot的維護者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM