skip to Main Content

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


  1. Assuming the above output object is in the variable obj, you could use the following snippet to log the details of each user:

     var results = obj.result;  
      if (results.length > 0) {
        for (var r in results) {
          var user = results[r].user;
          Logger.log([user.id, user.first_name, user.last_name])
        }
      }
    

    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 in user.

    Login or Signup to reply.
  2. 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 the response.

    Assume your request response is-

    let response = {"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}]};
    

    I don’t know but if it’s a JSON string then parse it so that the program not fails.

    if (typeof response === 'string') {
        response = JSON.parse(response);
    }
    

    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’s user object.

    response.result.map(result => {
        console.log(result.user.id, result.user.first_name, result.user.last_name);
    });
    

    I just console the values. You can do whatever you want.

    let response = {"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}]};
    
    if (typeof response === 'string') {
    	response = JSON.parse(response);
    }
    
    response.result.map(result => {
    	console.log(`id: ${result.user.id}`, `first_name: ${result.user.first_name}`, `last_name: ${result.user.last_name}`);
    });
    Login or Signup to reply.
  3. You can find id, first_name & last_name with help of map() function something like below snippet.

    var myArray = {"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}]}
    
    JSON.parse(JSON.stringify([myArray])).map(function(v,i) {
      v.result.map(function(v2, i2){
        console.log('ID:'+v2.user.id+' ==> First Name:'+v2.user.first_name+' ==> Last Name:'+v2.user.last_name)
      })
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search