Of course. Sending notifications from a Google Sheet to Telegram is an excellent and very popular alternative, especially since it doesn't have the same kind of administrator restrictions that Google Chat sometimes does.
Create a Telegram Bot and get its API Token.Get the Chat ID of the group or person you want to send messages to.Update your Google Apps Script to send the notification to Telegram's API.
Step 1: Create Your Telegram Bot and Get the API Token
Open Telegram (on your phone or desktop app).In the search bar, type BotFather and select the official one with the blue checkmark. Start a chat with BotFather and send the command: /newbot BotFather will ask you for a name for your bot (e.g., "My Sheet Notifier").Then, it will ask for a username for your bot. This must be unique and end in bot (e.g., MySheetNotifier_bot).If successful, BotFather will give you a message containing your API Token . It will look something like 1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefgh.
Step 2: Get Your Telegram Chat ID
A) For a Private Chat (to yourself):
Search for a bot called @userinfobot in Telegram and start a chat. It will immediately reply with your Chat ID . It's usually a 9 or 10-digit number.
B) For a Group Chat:
First, you must add your new bot (the one you created in Step 1) to the Telegram group.Then, add @userinfobot to the group. It will post a message with the group's Chat ID. The group Chat ID is a negative number (e.g., -100123456789). You can remove @userinfobot from the group after you have the ID.
Step 3: Update Your Google Apps Script
Open your Google Sheet and go to Extensions > Apps Script .Replace your existing code with the code below. Paste your Bot API Token and yourChat ID into the specified placeholders.
// This is the function that runs every time you edit the sheet
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
// 1. Check if the edit was in the correct cell and sheet
if (range.getA1Notation() === 'B64' && sheet.getName() === 'Dashboard') {
var value = range.getValue();
// 2. Check if the value contains the trigger words
if (value.includes("Long Entry") || value.includes("Short Entry")) {
var c60Value = sheet.getRange('C60').getValue();
// --- Send Notification to Telegram ---
sendTelegramNotification(c60Value);
// --- Email Notification (Optional) ---
// You can keep this or delete it if you only want Telegram alerts
var emailRecipient = "your.email@gmail.com"; // Replace with your email
var emailSubject = "Trading Signal Alert from Dashboard (B64)";
var emailBody = c60Value + "\n\nGenerated on: " + new Date().toLocaleString();
MailApp.sendEmail(emailRecipient, emailSubject, emailBody);
}
}
}
// This is the dedicated function to send the message to Telegram
function sendTelegramNotification(message) {
// REPLACE these two variables with your own details from Step 1 and 2
var botToken = "YOUR_TELEGRAM_BOT_TOKEN";
var chatId = "YOUR_CHAT_ID";
// This part prepares the message to be sent
// The 'parse_mode=HTML' allows you to use bold (<b>), italic (<i>), etc.
var url = "https://api.telegram.org/bot" + botToken + "/sendMessage?chat_id=" + chatId + "&text=" + encodeURIComponent(message) + "&parse_mode=HTML";
var options = {
'method': 'get' // Telegram API uses a GET request for sending messages this way
};
try {
// This line actually sends the message
UrlFetchApp.fetch(url, options);
} catch (e) {
// If there's an error, it will be logged for debugging
Logger.log("Error sending Telegram message: " + e.toString());
}
}
How to Use It
Save the script in the Apps Script editor.Go back to your Dashboard sheet.Edit cell B64 and type Test Long Entry.
Comments
Post a Comment