How to check if the current event is the last event in the series of events sent to webhook?

119 views
Skip to first unread message

Minh Phu Lo

unread,
Apr 5, 2023, 10:53:17 AM4/5/23
to Google Apps Script Community
Hi Team,
I am coding a google apps script webapp to react events from telegram api with a chat bot. I've already set webhook to google webhook to collect events, so I can't use **logger.log** to get log. Therefore I use **sendMessage** method of telegram api to get log of events. I use **doPost** to send HTTP request to telegram api whenever an event comes.
I aim to make chat bot resend a group of photos (as an album form) uploaded to the chat group but I am stucking at sendMediaGroup method of telegram api. This method requires 2 parameters **chat_id** and **media** where **media** is an Array of InputMediaAudio, InputMediaDocument, InputMediaPhoto and InputMediaVideo. In my case, **media** is an array of InputMediaPhoto. The problem is when an album of photos sent to the chat group, the telegram api responds a number of events (each one contains the same media_group_id and each of photo's property) as the number of photos in the album instead of an event that contains a media group composing all photos. Therefore I must collect InputMediaPhoto from each event then push them to **media** one by one. But I don't know how to help the bot identifing the final event to stop pushing thend do sendMediaGroup. The shape of my code as belows:

```
// Declare bot token, set webhook, declare myID as bot ID
// Declare functions such as sendMessage sendMediaGroup etc
// main body runs doPost to response events

var cache = CacheService.getScriptCache();

var msgIDstring = cache.get("msgID");

var mediastring = cache.get("mymedia");
if (mediastring == null){
  var media = []
}
else{
  var media = JSON.parse(mediastring);
}

function doPost(e) {
 
  let update = JSON.parse(e.postData.contents);
 
  let chat_id = update.message.chat.id; // group chat id

  sendMessage(myID, JSON.stringify(update), 'HTML');

if (update.message.media_group_id == null) {

// do other methods

}

  else {
   
    if (msgIDstring == null){
      cache.put("msgID",String(update.message.message_id));
      let photo_id = String(update.message.photo[0].file_id);
      let InputMediaPhoto = {
      'type': "photo",
      'media': photo_id
    }
   
    media.push(InputMediaPhoto);
    cache.put("mymedia",JSON.stringify(media));
    }  
   
    else {
      let i = update.message.message_id - Number(msgIDstring);
       
      while (i==1) {
       
        let photo_id = String(update.message.photo[0].file_id);
        let InputMediaPhoto = {
        'type': "photo",
        'media': photo_id
        };    
        media.push(InputMediaPhoto);
        sendMessage(myID, JSON.stringify(media), 'HTML');      
        cache.put("mymedia",JSON.stringify(media));        
        cache.put("msgID",String(update.message.message_id));

        if (i==0){
          break;
        }
      }
             
      sendMessage(myID, String(i), 'HTML'); // **It stops here with "0" sent to the chat bot**
      sendMessage(myID, cache.get("mymedia"), 'HTML');
      sendMediaGroup(chat_id,media);  
                     
    }
   
  }  
  //sendMessage(myID, JSON.stringify(media), 'HTML');
}


```
Any help will be much appreciated!! 

Nerio Villalobos

unread,
Apr 6, 2023, 2:41:11 PM4/6/23
to Google Apps Script Community
It seems that you are trying to send an album of photos using the sendMediaGroup method of the Telegram API, but you are facing some difficulties in identifying the final event to stop pushing the photos to the media array.

One approach you can try is to use a flag variable to indicate when all the photos in the album have been collected. You can initialize this variable to false at the beginning of the doPost function, and set it to true when you encounter the last photo in the album. Here is an example implementation:

// Declare bot token, set webhook, declare myID as bot ID
// Declare functions such as sendMessage sendMediaGroup etc
// main body runs doPost to response events

var cache = CacheService.getScriptCache();

var msgIDstring = cache.get("msgID");

var mediastring = cache.get("mymedia");
if (mediastring == null){
  var media = []
}
else{
  var media = JSON.parse(mediastring);
}

var allPhotosCollected = false; // initialize flag variable
          allPhotosCollected = true; // set flag variable to true when last photo is encountered
          break;
        }
      }
             
      if (allPhotosCollected) { // check flag variable
        sendMessage(myID, "All photos collected", 'HTML');

        sendMessage(myID, cache.get("mymedia"), 'HTML');
        sendMediaGroup(chat_id,media);
      } else {
        sendMessage(myID, "More photos to come", 'HTML');
      }
                     
    }
   
  }  
  //sendMessage(myID, JSON.stringify(media), 'HTML');
}

With this implementation, the while loop will continue collecting photos and pushing them to the media array until it encounters the last photo in the album. When the last photo is encountered, the flag variable allPhotosCollected is set to true, which signals that all the photos in the album have been collected. The if statement after the while loop checks the flag variable, and if it is true, it sends a message indicating that all photos have been collected, and then calls the sendMediaGroup method to send the album. If the flag variable is still false, it sends a message indicating that more photos are expected.
Reply all
Reply to author
Forward
0 new messages