Creating a Telegram bot menu with Google Sheets Apps Script

Hey guys, I’ve been working on a Telegram bot that uses Google Sheets Apps Script. I wanted to set up a menu with buttons for users to interact with. Here’s what I came up with:

When a user types ‘menu’, they see a list of buttons on their Telegram screen. Each button triggers a different action. I used JavaScript to send JSON objects to the Telegram bot.

Here’s a snippet of my code:

function showMainMenu(chatId) {
  const botUrl = API_ENDPOINT + '/sendMessage';

  const menuButtons = {
    'inline_keyboard': [
      [{'text': 'Settings', 'callback_data': 'settings'}],
      [{'text': 'Team', 'callback_data': 'team'}],
      [{'text': 'Local', 'callback_data': 'local'}],
      [{'text': 'National', 'callback_data': 'national'}],
      [{'text': 'Partners', 'callback_data': 'partners'}],
      [{'text': 'Debug', 'callback_data': 'debug'}]
    ]
  };

  const payload = {
    'chat_id': chatId,
    'text': 'Please choose an option:',
    'reply_markup': menuButtons
  };

  const requestOptions = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(botUrl, requestOptions);
  console.log(response);
}

It’s working pretty well now, but I’d love to hear if anyone has suggestions for improvement or different approaches!

nice work emma! i’ve done similar stuff with telegram bots. one thing u might consider is adding some emojis to ur button texts. it makes the menu look more appealing and user-friendly. also, have u thought about grouping related options? like putting ‘team’ and ‘partners’ together? just a thought!

Your implementation looks solid, Emma. One suggestion to enhance functionality would be to implement a persistent menu using Telegram’s setMyCommands method. This allows users to access menu options directly from the chat interface, reducing the need to type ‘menu’ each time. Additionally, consider implementing error handling for API calls to gracefully manage potential issues. Lastly, you might want to explore using inline keyboards for sub-menus, providing a more intuitive navigation experience for complex bot structures. These improvements could significantly enhance user interaction with your bot.

I’ve been down this road before, and your approach is solid. One thing that really improved my bot’s usability was implementing a state machine to manage different conversation flows. It helped keep track of where users were in the interaction and made it easier to handle complex scenarios.

Also, consider caching frequently accessed data from Google Sheets in your Apps Script. This can significantly reduce API calls and improve response times, especially if you’re dealing with larger datasets.

Lastly, I found it helpful to implement a logging system that records user interactions and any errors. It’s been invaluable for debugging and understanding user behavior. You could use the Logger class in Apps Script or even write logs back to a separate sheet for easy analysis.

Keep iterating on your bot. The more you refine it based on user feedback, the better it becomes!