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!!