Telegram Bot /getUpdates 并解析 JSON 响应?
Telegram Bot /getUpdates and parse JSON response?
我希望解析执行 Telegram API 调用的 JSON 响应:https://api.telegram.org/bot<token>/getUpdates
根据该回复,我想将所有 chat_IDs 存储在某个地方。我想遍历所有这些 ID,通过机器人将消息发送到每个群聊中。
import requests
def telegram_bot_sendtext(bot_message):
bot_token = ''
bot_chatID = ''
bot_message = ''
get_updates = 'https://api.telegram.org/bot' + bot_chatID + '/getUpdates'
response = requests.get(get_updates)
final = json.loads(response.text)
Dict = {final['result']['update_id']}
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
我知道我没有正确地根据响应创建这本词典。如何正确创建此字典或数组,然后循环遍历该对象以在每个组中发送 bot_message
?
result
是一个对象数组,可以用这种代码循环遍历
Dict = final['result']
for obj in Dict:
print(obj['update_id'])
我测试了这个,如果你想阅读message
那么
obj['message']['text']
我希望解析执行 Telegram API 调用的 JSON 响应:https://api.telegram.org/bot<token>/getUpdates
根据该回复,我想将所有 chat_IDs 存储在某个地方。我想遍历所有这些 ID,通过机器人将消息发送到每个群聊中。
import requests
def telegram_bot_sendtext(bot_message):
bot_token = ''
bot_chatID = ''
bot_message = ''
get_updates = 'https://api.telegram.org/bot' + bot_chatID + '/getUpdates'
response = requests.get(get_updates)
final = json.loads(response.text)
Dict = {final['result']['update_id']}
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
我知道我没有正确地根据响应创建这本词典。如何正确创建此字典或数组,然后循环遍历该对象以在每个组中发送 bot_message
?
result
是一个对象数组,可以用这种代码循环遍历
Dict = final['result']
for obj in Dict:
print(obj['update_id'])
我测试了这个,如果你想阅读message
那么
obj['message']['text']