I wrote a code in Google Apps Script that sends a message in my Telegram bot.
The problem is that the message I sent by using apps script is visible only to me in the bot and not to other people who started the bot.
How can I fix this problem?
function sendMessageToTelegram(chatId, message) {
// Replace 'YOUR_BOT_TOKEN' with your Telegram Bot API token
var botToken = 'YOUR_BOT_TOKEN';
var apiUrl = 'https://api.telegram.org/bot' + botToken + '/sendMessage';
// Log the message to check its value
Logger.log('Message received: ' + message);
// Check if the message is empty or undefined
if (!message) {
Logger.log('Error: Message text is empty or undefined.'); // Log the error message
return; // Exit the function if message is empty
}
// Construct the payload
var payload = {
'chat_id': chatId,
'text': message
};
// Send the message
var options = {
'method': 'post',
'payload': payload
};
// Send message to Telegram
var response = UrlFetchApp.fetch(apiUrl, options);
// Check if the message was sent successfully
var responseData = JSON.parse(response.getContentText());
if (response.getResponseCode() === 200 && responseData.ok) {
Logger.log('Message sent to Telegram: ' + message); // Log successful message sending
} else {
throw new Error('Failed to send message to Telegram. Response: ' + JSON.stringify(responseData));
}
}
// Define the message to send
var messageToSend = 'Hello from Google Apps Script!';
// Log the message before sending
Logger.log('Message to send: ' + messageToSend);
// Call sendMessageToTelegram with the message
sendMessageToTelegram('my_chat_ID', messageToSend);
}