Currently, I am in the process of coding a Google Apps Script web app that will react to events from the Telegram API using a chat bot. The first step was setting up a webhook to collect these events through the Google webhook. Since logger.log
is unavailable for logging purposes, I have resorted to using the sendMessage
method provided by the Telegram API to track these events. By utilizing the doPost
function, HTTP requests are sent to the Telegram API whenever an event occurs.
The main goal is to create a chat bot feature that can resend a collection of photos uploaded as an album in a chat group. However, my progress has hit a roadblock when attempting to implement the sendMediaGroup
method offered by the Telegram API. This specific method requires two key parameters: chat_id
and media
. The media
parameter should consist of an array containing InputMediaAudio
, InputMediaDocument
, InputMediaPhoto
, and InputMediaVideo
.
In my current scenario, the array I'm working with is focused on InputMediaPhoto
. The issue arises when sending an album of photos to the chat group, resulting in multiple events being generated by the Telegram API. Each event contains duplicate information such as the
media_group_id</code) and properties of each photo, equating to the number of photos within the album instead of consolidating them into one media group event. To address this challenge, I need to extract <code>InputMediaPhoto
objects from each event and manually add them to the media
array individually. However, determining the final event to trigger the sendMediaGroup
function remains unresolved. Below is a glimpse of my code structure:
// Initialization including bot token, webhook setup, and declaring bot ID (myID)
// Functions declaration like sendMessage, sendMediaGroup, etc.
// Main body executing doPost to respond to 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) {
// Implement 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'); // **The execution halts at this point with “0” sent to the chat bot**
sendMessage(myID, cache.get("mymedia"), 'HTML');
sendMediaGroup(chat_id, media);
}
}
//sendMessage(myID, JSON.stringify(media), 'HTML');
}