I am developing a Telegram bot and need to ascertain who are the admins in a group
Have developed a Google Web App using JavaScript and calling the following method
getChatAdministrators
Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects that contains information about all chat administrators
this method is returning the following:
[20-03-14 17:14:23:403 AEDT]
{"ok":true,
"result":
[{"user":{"id":810784352,"is_bot":false,"first_name":"Rafael","last_name":"Vasconcelos"},"status":"administrator","can_be_edited":false,"can_change_info":true,"can_delete_messages":true,"can_invite_users":true,"can_restrict_members":true,"can_pin_messages":true,"can_promote_members":false},
{"user":{"id":1021450393,"is_bot":false,"first_name":"Mauro","last_name":"Ramires"},"status":"administrator","can_be_edited":false,"can_change_info":true,"can_delete_messages":true,"can_invite_users":true,"can_restrict_members":true,"can_pin_messages":true,"can_promote_members":false},
{"user":{"id":998081853,"is_bot":false,"first_name":"Filipe","last_name":"Lima"},"status":"administrator","can_be_edited":false,"can_change_info":true,"can_delete_messages":true,"can_invite_users":true,"can_restrict_members":true,"can_pin_messages":true,"can_promote_members":false},
{"user":{"id":962548471,"is_bot":false,"first_name":"Trajano","last_name":"Roberto","username":"TrajanoRoberto","language_code":"en"},"status":"creator"},
{"user":{"id":307271095,"is_bot":false,"first_name":"Leandro","last_name":"Silva","username":"Leandro_CRF"},"status":"administrator","can_be_edited":false,"can_change_info":true,"can_delete_messages":true,"can_invite_users":true,"can_restrict_members":true,"can_pin_messages":true,"can_promote_members":false}
]
}
question:
I am struggling to find correct JavaScript syntax to read:
id, first_name & last_name
for the response returned by the method
getChatAdministrators
thanks in advance for any help.
Trajano Roberto
3
Answers
Assuming the above output object is in the variable
obj
, you could use the following snippet to log the details of each user:To understand this, if you parse the object (in a tool like this), all the users are in an array in
result
. And each user’s details are inuser
.It seems you are getting a JSON response from the request and it’s an object. So for getting
id
,first_name
,last_name
you have to inspect theresponse
.Assume your request response is-
I don’t know but if it’s a JSON string then parse it so that the program not fails.
Here I check if the response is a string then parse the string as JSON.
Finally, I have to iterate through the response as the required information is stored inside the
result
array’suser
object.I just console the values. You can do whatever you want.
You can find
id
,first_name
&last_name
with help ofmap()
function something like below snippet.