How to get invite URL from Telegram bot createChatInviteLink response

I’ve successfully implemented the createChatInviteLink method in my Telegram bot, but I’m struggling to retrieve the actual invitation URL from the response to share it with users.

function generateInviteUrl(groupId){
  groupId = 'group_id';
  userLimit = 1;
  var apiCall = botUrl + '/createChatInviteLink?chat_id=' + groupId + '&member_limit=' + userLimit;
  return UrlFetchApp.fetch(apiCall);
}

The function runs without errors, but I can’t figure out how to parse the returned data to get the invite link. What’s the proper way to extract the generated invitation URL from this API response?

Your function’s returning the raw response instead of extracting the URL. The invite link is buried inside the result property - you need to parse the JSON first, then grab result.invite_link. Telegram wraps the actual data in a result object, which isn’t super obvious from their docs. The response also has expire_date and member_limit if you need those later. I ran into the same thing when I started with Telegram’s API. Oh, and make sure your bot has admin privileges or the call might fail silently.

yeah, you’re missing the json parsing step. add JSON.parse(response.getContentText()).result.invite_link after your fetch call - that’s where telegram puts the actual url you need.

You’re not parsing the JSON response from the API call. The createChatInviteLink method returns an object with the invite link details, but your function just fetches the data without extracting the actual URL. Here’s how to fix it:

function generateInviteUrl(groupId){
  groupId = 'group_id';
  userLimit = 1;
  var apiCall = botUrl + '/createChatInviteLink?chat_id=' + groupId + '&member_limit=' + userLimit;
  var response = UrlFetchApp.fetch(apiCall);
  var jsonResponse = JSON.parse(response.getContentText());
  
  if(jsonResponse.ok) {
    return jsonResponse.result.invite_link;
  } else {
    console.log('Error:', jsonResponse.description);
    return null;
  }
}

The invite URL lives in result.invite_link. Make sure to check the ok field first to confirm the response worked before grabbing the result data.