如何使用 python-telegram-bot 发送消息

时间:2021-06-04 19:00:42

标签: python bots telegram

我是使用 python 和电报机器人的新手

当我收到一条没有使用的消息时,我试图找到一种使用 python-telegram-bot 发送消息的方法:

requests.post(
        'https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>&text=Hello World!')

流程:

消息到达通道 A,然后向通道 B 发送一条新消息(消息在通道 A 上接收)。

我只找到了reply_text。还有其他方法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

reply_text 将不起作用,因为它会将消息发送回通道 A。您需要 bot.send_message,您可以在其中使用 chat_id 设置目标通道。一个简单的例子:

def update_message_to_channel_B(update: object, context: CallbackContext) -> None:
    context.bot.send_message(chat_id=CHANNEL_B_ID, text='Message receveid on Channel A')

并添加处理程序:

dispatcher.add_handler(MessageHandler(Filters.text, update_message_to_channel_B))