The object i got from telegram api is like this:
{
'ok': True,
'result': [
{
'update_id': 633425135,
'message':
{
'message_id': 2,
'from':
{
'id': 5060166011,
'is_bot': False,
'first_name': 'Mosquito',
'username': 'TheMosquitoo',
'language_code': 'en'
},
'chat':
{
'id': 5060166011,
'first_name': 'Mosquito',
'username': 'TheMosquitoo',
'type': 'private'
},
'date': 1641723768,
'text': 'asdfasdf'
}
},
{
'update_id': 633425136,
'message':
{
'message_id': 4,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723894,
'text': '/start',
'entities': [
{
'offset': 0,
'length': 6,
'type': 'bot_command'
}]
}
},
{
'update_id': 633425137,
'message':
{
'message_id': 5,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723910,
'text': '😂'
}
},
{
'update_id': 633425138,
'message':
{
'message_id': 7,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723957,
'text': '*kumaraswamy'
}
}]
}
i wanted to map this object such that i only get messages, first_name and last_name…
please help me.
>Solution :
Try this (obj is your object, i.e. the object returned by the API call):
result = [
{'message_text': msg['message']['text'],
'username': msg['message']['from']['username'],
'first_name': msg['message']['from']['first_name']}
for msg in obj['result']]
This is the result:
[
{
'message_text': 'asdfasdf',
'username': 'TheMosquitoo',
'first_name': 'Mosquito',
},
{
'message_text': '/start',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
{
'message_text': '😂',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
{
'message_text': '*kumaraswamy',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
]