JSON response samples for Telegram API developers. Based on Bot API 5.3. For details see the documentation.
Code.gs powered by Telegram Bot API v5.3 and Google Apps Script.
Each JSON response from the Telegram Bot API will be sent back by Google Apps Script as a message via Google web apps connected to the Telegram webhook. The results appear in the chat bot whenever a user submits certain content. You can change it to the Apps Script's Logger.log.
You can even more simplify the script with ES6 notation.
The JSON's media group is sent separately for each object. If in one media group there is one video, two images, and a caption text, it will be sent in four JSON modules of text, video, and two photos with the same media_group_id.
The JSON's Live Location will be sent according to the update range in live_period between every 60 and 86400 seconds with default every 900.
The JSON response from a Poll can only be captured when the Poll is created by the user. The JSON response from the Poll answer itself cannot be captured, but instead sent to the user who created it.
The JSON response from Poll answer can only be captured if the Poll is created by the Bot with the sendPoll method. The JSON response from the Poll itself cannot be captured.
Example of the sendPoll method with options property in a Array of String of JSON-serialized list of answer 2-10 strings 1-100 characters each:
let now = new Date();
now.setHours( now.getHours() + 1 );
let dataPoll = {
method: "post",
payload: {
method: "sendPoll",
chat_id: String( chatid ),
question: "Your favorite color",
options: JSON.stringify( [ "RED", "GREEN", "BLUE" ] ),
is_anonymous: "false",
type: "quiz",
allows_multiple_answers: "true",
correct_option_id: "2",
explanation: "Because <b>BLUE</b> is the color of <a href='https://telegram.org'>Telegram</a>.",
explanation_parse_mode: "HTML",
close_date: String( now.getTime() )
//in case need more complicated...
//close_date: String( Utilities.formatDate( now, Session.getScriptTimeZone(), "yyyy-MM-dd'T'HH:mm:ss'Z'" ) )
}
};
UrlFetchApp.fetch( telegramAPIURL + "/", dataPoll );
//let response = UrlFetchApp.fetch( telegramAPIURL + "/", dataPoll );
//Logger.log( response.getContentText() );
Add poll commands with @BotFather to generate a test poll:
start - Command test
poll000 - Poll anonymous regular single answer
poll001 - Poll anonymous regular multiple answer
poll010 - Poll anonymous quiz single answer
poll011 - Poll anonymous quiz multiple answer
poll100 - Poll non anonymous regular single answer
poll101 - Poll non anonymous regular multiple answer
poll110 - Poll non anonymous quiz single answer
poll111 - Poll non anonymous quiz multiple answer
Quiz cannot be mixed with Multiple-answer. poll011 and poll111 command is used to see Telegram's priority over these two properties when both forced to combine.
See POLL-JSON-Handler-Script.gs
JSON Response Bot @simrsjsonbot powered by Code.gs