Telegram Bot con Inline Keyboard in Google Sheets

Опубликовано: 07 Июль 2026
на канале: Google: Apps, Script, Pillole, Tips and Tricks
6,737
100

With a Telegram Bot it is possible to show the user a series of clickable buttons that perform an action.

In another video of mine on the subject I showed how to create a bot that asks a question to the user, waits for his answer and, based on the entered text, automatically provides a relevant answer: https://www.youtube .com / watch? v = cSK-RFiVMv0
On the same principle I want to show you in this video how to adapt the code so that the user can view, together with the bot message, a virtual keyboard with a series of buttons where he just needs to click on one of them to choose his answer. This feature is called inline keyboard.

If you start now with Telegram Bots I recommend my video on how to create one in 5 minutes which will serve as the basis for all your feature integrations:    • Creare un BOT di TELEGRAM in 5 Minuti  

The function that I called sendKeyboard does the same thing as sendMessage, i.e. it sends a message to the user, however having to manage more advanced parameters than a simple text message, such as the object that will contain the information of our virtual keyboard, uses the post method and its syntax is different.

This object is defined in the doPost (e) function, which I remember that in Google Apps Script is the first to be called every time the Web app is requested. In its structure, for each element that I want to be displayed in the form of a button, the text that will appear on the button and the name of the reference that will be passed to the Telegram Bot must be indicated to make it understand where the click occurred in order to act consequently by activating a callback function.

Since the Bot must be able to understand whether it is a generic user message or the click on the virtual keyboard that we have proposed, it becomes of central importance to insert a conditional instruction that verifies precisely whether it is a callback or a a normal insertion of a text message.

It is in this second case that the bot will show the user the clickable buttons. After retrieving the reference to the chat where to write and the user's name and surname information to be able to greet him and indicate what he should do, we pass everything to the sendKeyboard function that we have described with the JSON object of the keyboard.
At this point, what will happen in the Telegram chat will be to show the text message just seen and a series of clickable buttons containing the names of the aperitifs, or this result.
The bot will wait for the user's choices, if he clicks on one of the buttons the relative callback function will be activated.

All that remains is to deploy a new version of the application from 'Manage deployment'. If, on the other hand, it is the first time you publish the app you will first have to perform a function manually to start the authorization process and then carry out a 'Re-deployment'.

In the video you can see the live result, where I start with a greeting which, being a generic text message (and not a return action from clicking on a button that I have not yet viewed), I get in response the welcome and the invitation to choose the name of a drink from those selectable on the virtual keyboard that appears immediately after the message.

At the same time, my selections were saved in chronological order in Sheet1.

Useful links:
Article with the full code on my blog: https://www.appsscript.it/tutorial/te...
[IL LIBRO - ENG] Aim high with ... Google Apps Script: https://www.amazon.it/Punta-alto-Goog...
Official Telegram Bot API documentation: https://core.telegram.org/bots/api

#TelegramBot #GoogleAppsScript #GoogleSheets #JavaScript #BotTelegram