【问题标题】:How to send message using python-telegram-bot如何使用 python-telegram-bot 发送消息
【发布时间】:2021-06-04 19:00:42
【问题描述】:

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

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

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

流程:

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

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

谢谢!

【问题讨论】:

    标签: python bots telegram


    【解决方案1】:

    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))
    

    【讨论】:

    • 您好,感谢您的回复!我发现机器人的上下文如下: context.bot.send_message
    • 正确,看我的例子。
    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多