【问题标题】:Parsing telegram bot api response解析电报机器人 api 响应
【发布时间】:2021-05-13 18:36:03
【问题描述】:

我正在尝试使用editMessageText 在电报机器人上编辑消息,但它需要一个 message_id 整数,所以当我发送消息时我需要以某种方式解析电报响应

https://api.telegram.org/bot12345:abcdefghijk-lmnopqrstuvwxyz/sendMessage?text=Some%20Text&chat_id=123456789

它会这样回应:

{"ok":true,"result":{"message_id":213557,"from":{"id":bot_id,"is_bot":true,"first_name":"BotName","username":"SpaceTheBot"},"chat":{"id":123456789,"title":"A Group","type":"supergroup"},"date":1612928163,"text":"Some text"}}

所以我想解析 message_id,以便稍后进行编辑。

【问题讨论】:

  • 试试这个。虽然这是用于删除消息,但您可以申请编辑消息。 stackoverflow.com/questions/65761031/…
  • 嘿!感谢您的回复,但我没有使用长轮询方法,感谢您的帮助。

标签: python aws-lambda telegram telegram-bot python-telegram-bot


【解决方案1】:

您需要反序列化来自 Telegram 的响应。之后它将是一个 Python 对象。在这种情况下,因为它是一个 JSON 对象,所以它将被转换为一个 dict 并且可以这样访问。

import json

response = '{"ok":true,"result":{"message_id":213557,"from":{"id":"bot_id","is_bot":true,"first_name":"BotName","username":"SpaceTheBot"},"chat":{"id":123456789,"title":"A Group","type":"supergroup"},"date":1612928163,"text":"Some text"}}'
result = json.loads(response)

print(result["result"]["message_id"])
>>> 213557

如果你使用请求,它有自己的JSON encorder/decoders

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2017-08-18
    • 2017-02-14
    • 2017-10-24
    • 2015-07-21
    • 2016-03-22
    • 2020-09-26
    相关资源
    最近更新 更多