python-telegram-bot - 发送消息
python-telegram-bot - sending message
我试图在不等待用户响应的情况下使用 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()
在 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
.
的维护者
我试图在不等待用户响应的情况下使用 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()
在 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
.