Skip to content
The Chat Archive
gpt-4.1-mini-2025-04-14· OpenAI WildChat-4.8M (AI2)

hi

  1. User

    hi

  2. Assistant

    Hello! How can I assist you today?

  3. User

    write me a code for automation google sheet data to telegram bot

  4. Assistant

    Certainly! To automate sending Google Sheets data to a Telegram bot, you can use Google Apps Script, which allows you to write JavaScript code directly within Google Sheets.

    Here's a step-by-step guide and example code to send data from a Google Sheet to a Telegram bot:


    Step 1: Create a Telegram Bot

    1. Open Telegram and search for @BotFather.
    2. Start a chat and send /newbot.
    3. Follow instructions to create your bot.
    4. After creation, you will get a bot token (something like 123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ).

    Step 2: Get your Chat ID

    You need your Telegram chat ID to send messages to yourself or a group:

    1. Start a chat with your bot or add the bot to a group.

    2. Open this URL in your browser (replace <YourBotToken> with your bot token):

      https://api.telegram.org/bot<YourBotToken>/getUpdates
      
    3. Send a message to the bot or in the group.

    4. Refresh the above URL, and look for "chat":{"id":YOUR_CHAT_ID"} in the JSON response.


    Step 3: Write Google Apps Script

    1. Open your Google Sheet.
    2. Click on Extensions > Apps Script.
    3. Paste the following code, replacing BOT_TOKEN and CHAT_ID with your own.
    function sendSheetDataToTelegram() {
      var botToken = "BOT_TOKEN";   // Replace with your Telegram bot token
      var chatId = "CHAT_ID";       // Replace with your chat ID
    
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var dataRange = sheet.getDataRange();
      var data = dataRange.getValues();
    
      // Example: Convert the data to a message string
      // This example sends the whole sheet content as formatted text
      var message = "Google Sheet Data:\n";
    
      for (var i = 0; i < data.length; i++) {
        message += data[i].join(" | ") + "\n";  // Join row columns by a pipe symbol
      }
    
      var url = "https://api.telegram.org/bot" + botToken + "/sendMessage";
    
      var payload = {
        method: "post",
        contentType: "application/json",
        payload: JSON.stringify({
          chat_id: chatId,
          text: message
        })
      };
    
      UrlFetchApp.fetch(url, payload);
    }
    

    Step 4: Run the Script

    1. Save the script.
    2. Click the Run ▶️ button.
    3. Google will ask for authorizations the first time – allow the permissions required.
    4. If everything is setup correctly, your Telegram should receive the message with the sheet data.

    Optional: Set up a trigger

    To automate sending data at intervals:

    • Go to Triggers (clock icon in the Apps Script editor).
    • Add a trigger to run sendSheetDataToTelegram function on a time-driven basis (e.g., daily, hourly).

    Let me know if you want help with more advanced formatting, sending specific rows, or other customizations!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.