简体   繁体   English

如何获取我通过 Python 使用 Telegram 机器人发送的消息的 ID?

[英]How get id of the message I sent with my Telegram bot via Python?

To send the message on my Telegram channel, I use the following template that works perfectly:为了在我的 Telegram 频道上发送消息,我使用了以下完美运行的模板:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
    }

chat_telegram = ['XXXXXXXXXXXXX']

textalert = f'Test'
botalert = 'YYYYYYYYYYYYYYYYYYYYY'
urlalert = f'https://api.telegram.org/bot{botalert}/sendMessage'
params = {'text':textalert, 'chat_id':chat_telegram, 'parse_mode':'HTML'}
return_request = requests.get(urlalert, headers=headers, params=params)

I've tried using all four of the following ways to try to retrieve the message ID that was sent:我尝试使用以下所有四种方法来尝试检索已发送的消息 ID:

print(return_request['update']['message']['message_id'])
print(return_request['message']['message_id'])
print(return_request.update.message.message_id)
print(return_request.message.message_id)

But none returned positively, how should I proceed to recover the value I need?但是没有一个正面返回,我应该如何继续恢复我需要的价值?

Here is the API map是 API map

The message id is stored in the response.text part of the response, you can retrieve it via the json method:消息 id 存储在响应的response.text部分,您可以通过json方法检索它:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
    }

chat_id = ['chat id']

textalert = f'Test'
botalert = 'some token'
urlalert = f'https://api.telegram.org/bot{botalert}/sendMessage'
params = {'text':textalert, 'chat_id':chat_id, 'parse_mode':'HTML'}

response = requests.get(urlalert, headers=headers, params=params)
json_data = response.json()

# get the message id
print('message id', json_data['result']['message_id'])

alternatively, you can retrieve it with the json module:或者,您可以使用json模块检索它:

import json
json_data = json.loads(response.text)
# json_data['result']['message_id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在电报(pytelegramBotAPI)中获取chat_id和message_id以更新电报bot(Python)中最后发送的消息 - how to get chat_id and message_id in telebot(pytelegramBotAPI) to update last sent message in telegram bot(Python) 获取我的机器人在 python 中发送的消息的消息 ID - Get message ID of the message sent by my bot in python Python Telegram Bot-如何更新我的机器人已发送的最后一条消息的文本 - Python Telegram Bot - How to update the text of the last message my bot has sent Python-telegram-bot:如何在电报 api 中通过机器人获取自定义编号的最新消息? - Python-telegram-bot: How can I get customized number latest message by bot in telegram api? 如何通过 python 在电报机器人中获取实时位置? - How can I get Live Location in telegram bot via python? 如何使用 Telegram 的 python bot API 编辑已发送的消息? - How do I edit a sent message using Telegram's python bot API? 如何从我的机器人获取消息 ID 电报频道并删除该消息? - How to get the message id telegram channel from my bot and remove that message? Telegram bot 获取发送消息的时间 - Telegram bot get a time of the sent message 电报机器人 Api / Python:试图通过我的电报机器人发送语音消息 - Telegram Bot Api / Python: Trying to send voice message via my telegram bot Python Telegram Bot 让机器人响应消息 - Python Telegram Bot get the bot to respond to message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM