Trajano Roberto
Trajano Roberto

Reputation: 307

Amendment 01 Telegram bot button drive menu via Google Sheets Apps Script

have amended Google Web App based on JavaScript source code as per 20200403 sadly, still does not work

problem: buttons do not show up on Telegram group, related to the group chat_id, when I type: menu

JavaScript should construct and send back to Telegram group: InlineKeyboardMarkup object (This object represents an inline keyboard that appears right next to the message it belongs to.)

Google Web App JavaScript source code follows:

var vApiTokenTelegram = "????????????????????????"; // @MediaFlamengoBot API token
var vUrlTelegram = "https://api.telegram.org/bot" + vApiTokenTelegram;
var vWebAppUrl = "https://script.google.com/macros/s/?????????????????????/exec";

function sendReplyMarkupMessage( chat_id, text, oInlineKeyboard ) {
    var encodedText = encodeURIComponent(text);

    GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update", JSON.stringify(oInlineKeyboard, null, 4));    

    var req = new Request(vUrlTelegram +    "/sendMessage?chat_id=" + chat_id + "&text=" + text + "&reply_markup=" + oInlineKeyboard );
    fetch(req)
    .then(response => response.blob())
    .then(blob => {
        console.log('Response: ', blob)
    });
    //Logger.log(response.getContentText());  
}

function menu( chat_id ) {
    var oInlineKeyboard = JSON.stringify({
        inline_keyboard: [
            [{ text: 'Some button text 1', callback_data: '1' }],
            [{ text: 'Some button text 2', callback_data: '2' }],
            [{ text: 'Some button text 3', callback_data: '3' }],
            [{ text: 'Some button text 4', callback_data: '4' }],            
            [{ text: 'Some button text 5', callback_data: '5' }]
        ]
    });
    sendReplyMarkupMessage( chat_id, "test", oInlineKeyboard );
}

gmail logs the following, no JSON object: "{\"inline_keyboard\":[[{\"text\":\"Some button text 1\",\"callback_data\":\"1\"}],[{\"text\":\"Some button text 2\",\"callback_data\":\"2\"}],[{\"text\":\"Some button text 3\",\"callback_data\":\"3\"}],[{\"text\":\"Some button text 4\",\"callback_data\":\"4\"}],[{\"text\":\"Some button text 5\",\"callback_data\":\"5\"}]]}"

thanks in advance for any help

Trajano

Upvotes: 2

Views: 558

Answers (1)

0stone0
0stone0

Reputation: 44275

You are defining the reply_markup twice. Your using this "&reply_markup=" + reply_markup in the 'send' function. But the reply_markup is also defined in the options;

function menu( chat_id ) {
    var options = {
        reply_markup: JSON.stringify({
            inline_keyboard: [
            ...

Try removing the reply_markup and set the options as an array;

function sendReplyMarkupMessage( chat_id, text, reply_markup ) {
    var encodedText = encodeURIComponent(text);
    var response = UrlFetchApp.fetch(vUrlTelegram +    "/sendMessage?chat_id=" + chat_id + "&text=" + text + "&reply_markup=" + reply_markup );
    Logger.log(response.getContentText());  
}

function menu( chat_id ) {
    var options = JSON.stringify({
        inline_keyboard: [
            [{ text: 'Some button text 1', callback_data: '1' }],
            [{ text: 'Some button text 2', callback_data: '2' }],
            [{ text: 'Some button text 3', callback_data: '3' }]
        }
    ]);
    sendReplyMarkupMessage( chat_id, "test", options );
}   


Edit; I believe UrlFetchApp is a google script function.

Edit 2; You're right, the array in JSON.stringify should be an object!

I've created a JSFiddle bases on your code; Take a look at it here.

Output

After placing your own bot token + chat_id

enter image description here


Based on Google App Script, you should use something like this (Tested succesfully);

function myFunction() {
    let token = '123456788:AAdadadadbMTcMvY10SZGsbIJ2rdFXJiXmbFw';
    let url = "https://api.telegram.org/bot" + token + "/sendMessage";

    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify({
            'chat_id': 11111111,
            'text': 'fsdfdsfsdf',
            'reply_markup': {
                inline_keyboard: [
                    [{ text: 'Some button text 1', callback_data: '1' }],
                    [{ text: 'Some button text 2', callback_data: '2' }],
                    [{ text: 'Some button text 3', callback_data: '3' }]
                ]
            }
        })
    };
    var response = UrlFetchApp.fetch(url, options);  
    var res = UrlFetchApp.fetch(url);
    Logger.log(res);
}

Upvotes: 1

Related Questions