我正在尝试使用 Python-Telegram-Bot 发送消息,而无需等待用户响应,但无法使其正常工作并且我没有收到任何错误。
Code:
def echo(context):
context.bot.send_message(chat_id=-516017547, text='test')
#chat_id is the group id I found by going to:
#https://api.telegram.org/bot<MY_API>/getUpdates
def main():
updater = Updater(API)
dp = updater.dispatcher
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dp.add_handler(echo_handler)
updater.start_polling()
updater.idle()
main()
答案 0 :(得分:1)
在 python-telegram-bot
中,处理程序用于处理传入的更新 - 仅此而已。但是,要调用 bot 方法,您只需要一个 telegram.Bot
实例。在您的 echo
函数中,它可用作 context.bot
。但是,它也可以在 main
中用作 updater.bot
或 updater.dispatcher.bot
。请注意,您也可以使用完全不带 Updater
的机器人实例:
from telegram import Bot
bot = Bot(TOKEN)
bot.send_message(...)
PS:如果您想使用 echo
作为 MessageHandler
的回调,它必须完全接受参数 update
和 context
。
免责声明:我目前是 python-telegram-bot
的维护者。