Search code examples
javascriptgoogle-apps-scripttelegram-botstate-machine

Example 2 of Telegram bot menu access using Google Sheets Apps Script


I am developing a Google web app JavaScript application to communicate to a Telegram bot.

Telegram users type certain words or click buttons appearing on Telegram group via the usage of InlineKeyboardMarkup (InlineKeyboardButton);

I have implemented a Telegram Bot state machine to detect which commands have been entered, in order to ascertain respective action to be executed;

question: is there a more efficient approach to meet above?

sample of the Google web app JavaScript source code I am using:

function menu( chat_id ) {
    let url = vUrlTelegram + "/sendMessage";

    var keyboard = {
        'inline_keyboard' : 
        [
            [{'text' : 'admin',      'callback_data' : 'admin'}], // Row 1 
            [{'text' : 'squad',      'callback_data' : 'squad'}], // Row 2

            [{'text' : 'carioca',    'callback_data' : 'carioca'}], // Row 3
            [{'text' : 'brasileiro', 'callback_data' : 'brasileiro'}], // Row 4

            [{'text' : 'sponsors',   'callback_data' : 'sponsors'}], // Row 5       
            [{'text' : 'test',       'callback_data' : 'test'}] // Row 6       
        ]
    };  

    var data = {
        'chat_id': chat_id,
        'text': "main menu",
        'reply_markup': keyboard
    };   

    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify(data)
    };
    var response = UrlFetchApp.fetch(url, options);  
    Logger.log(response);
}


function telegramBotMachine( chat_id, text, name, data ) {
    var sUpperData = data.toUpperCase();
    var sResponse = " ";

//    var sContent = "chat_id: " + chat_id + " \ntext: " + text + " \nname: " + name + " \ndata: " + data + " \nsUpperData: " + sUpperData;
//    sendFormattedMessage( chat_id, sContent, "HTML", "false", "false" );


    if ( sUpperData.substr( 0, 4 ) === "MENU" ) {
        menu( chat_id );
    } else if ( sUpperData.substr( 0, 7 ) === "CARIOCA" && sUpperData.length === 7 ) {
        cariocaSubMenu( chat_id );
    } else if ( sUpperData.substr( 0, 13 ) === "CARIOCA JOGOS" && sUpperData.length > 7 ) {
        cariocaLeagueGameReport( chat_id );
    } else if ( sUpperData.substr( 0, 14 ) === "CARIOCA TABELA" && sUpperData.length > 7 ) {
        cariocaLeagueTableReport( chat_id );
    } else if ( sUpperData.substr( 0, 10 ) === "BRASILEIRO" && sUpperData.length === 10 ) {
        brasileiroSubMenu( chat_id );
    } else if ( sUpperData.substr( 0, 16 ) === "BRASILEIRO JOGOS" && sUpperData.length > 10 ) {
        brasileiroLeagueGameReport( chat_id );
    } else if ( sUpperData.substr( 0, 17 ) === "BRASILEIRO TABELA" && sUpperData.length > 10 ) {
        brasileiroLeagueTableReport( chat_id );
    } else if ( sUpperData.substr( 0, 5 ) === "ADMIN" ) {
        adminReport( chat_id )    
    } else if ( sUpperData.substr( 0, 5 ) === "SQUAD" ) {
        squadReport( chat_id );        
    } else if ( sUpperData.substr( 0, 8 ) === "SPONSORS" ) {
        sponsorsReport( chat_id );        
    } else if ( sUpperData.substr( 0, 4 ) === "TEST" ) {
        sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
    }
}  

function doPost(e) {
    var contents = JSON.parse(e.postData.contents);
    var chat_id = "";
    var text = "";
    var name = "";
    var data = "";
    var sResponse = "";

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

    if ( typeof( contents.message ) != "undefined" ) {
        //  block of code to be executed if condition1 is true
        chat_id = contents.message.chat.id;
        text = contents.message.text;
        name = contents.message.from.first_name + " " +  contents.message.from.last_name;
        data = text;
        sResponse = telegramBotMachine( chat_id, text, name, data );

    } else if ( typeof( contents.callback_query ) != "undefined" ) {
        //  block of code to be executed if the condition1 is false and condition2 is true
        chat_id = contents.callback_query.message.chat.id;
        text = contents.callback_query.message.text;
        name = contents.callback_query.message.from.first_name;
        data = contents.callback_query.data;
        sResponse = telegramBotMachine( chat_id, text, name, data );

    } else {
        //  block of code to be executed if the condition1 is false and condition2 is false

    }
} 

Solution

  • question: is there a more efficient approach to meet above?

    There is! Telegram Inlinekeyboardbutton provides the callback_data so you can easily tell apart the callbacks!

    Instead of using the callback text, lowering the chars and checking on the button value

    var text = contents.message.text;
    var name = contents.message.from.first_name + " " +  contents.message.from.last_name;
    var sResponse = telegramBotMachine( chat_id, text, name );
    if ( sUpperText.substr( 0, 4 ) === "MENU" ) {
        menu( chat_id );
    } else if
        ....
    

    You should use the callback_data which you are already providing in the keyboard function, but just not using! Something like this;

    function telegramBotMachine( chat_id, callbackData, name ) {
        switch (callbackData) {
            case 'menu':
                menu( chat_id );
                break;
    
            case 'admin':
                adminReport( chat_id );
                break;
    
            case 'squad': 
                squadReport( chat_id );
                break;
    
            // ...
    
            default: 
                sendFormattedMessage( chat_id, "Test", "HTML", "false", "false" );
                break;
        }
    }  
    
    function doPost(e) {
        var contents = JSON.parse(e.postData.contents);
        var chat_id = contents.message.chat.id;
    
        // Check for callback_query
        if (typeof contents.callback_query !== 'undefined') {
            var callbackData = contents.callback_query.data;   // admin, squad, ...
            var name = contents.message.from.first_name + " " +  contents.message.from.last_name;
            var sResponse = telegramBotMachine( chat_id, callbackData, name );
        }
    }  
    

    Used switch-case for readability!