var token = '6781969859:AAGDG27aG3-L1Uu8QiMlUmSxUailFZbr2Q'; // only need to run once…
- User
var token = '6781969859:AAGDG27aG3-L1Uu8QiMlUmSx_UailFZbr2Q';
// only need to run once function setWebhook() { // only to create permission let drive = DriveApp.getRootFolder();
let Bot = new TelegramBot(); let result = Bot.request('setWebhook', { url: 'https://script.google.com/macros/s/AKfycbzlWvi9uj64WXn87LHz8Fw1ZD9Jh8QKXNOVdg_rVFZmgRDETf5HRVpLFvFcvU4O_aOI/exec' }); Logger.log(result); }
// Global variable to store post data from telegram var TelegramData;
function doPost(e) { // check valid request if (!validRequest_(e)) return;
let Bot = new TelegramBot(); let chatId = TelegramData.message.from.id; let Cmd = TelegramData.message.text;
// Start process request if (Cmd == '/start') { Bot.sendMessage(chatId, "Congratulations! It works!"); } /else if (Cmd == '/whoami') { let text = 'Your ID :\n' + TelegramData.message.from.id + '\n\n' + 'Your Name :\n' + TelegramData.message.from.first_name + '\n\n' + '⚠ Note :\nYour id is like your phone number. keep it a secret' ; Bot.sendMessage(text, 'HTML'); }/ else { let bot = new TelegramBot(); let chatIdOrigine = TelegramData.message.chat.id; // L’ID dell’utente che ha inviato il messaggio al bot let messageId = TelegramData.message.message_id; // L’ID del messaggio da inoltrare
bot.forwardMessage(625347711, chatIdOrigine, messageId); // inoltro del messaggio all'admin Bot.sendMessage(625347711, "/reply_" + chatId); // invio di un messaggio all'admin tempMessage(chatId);} }
function tempMessage(chatId) { // Invia il messaggio temporaneo con le istruzioni let tempMessage = Bot.sendMessage(chatId, "Messaggio inviato ✅", 'HTML');
// Memorizza l'ID del messaggio temporaneo let tempMessageId = tempMessage.result.message_id;
// Dopo un certo periodo di tempo, rimuovi il messaggio temporaneo Utilities.sleep(2000); // Attendi 3 secondi (puoi regolare il tempo a tuo piacimento)
// Rimuovi il messaggio temporaneo utilizzando l'ID memorizzato Bot.request('deleteMessage', { 'chat_id': chatId, 'message_id': tempMessageId }); }
function validRequest_(e) { // Only response if type is text message try { if (e.postData.type == 'application/json') { TelegramData = JSON.parse(e.postData.contents); return typeof TelegramData.message.text != 'undefined'; } else return false; } catch (e) { return false; } }
/* Telegram Bot function */
class TelegramBot { request(method, data) { let options = { 'method': 'post', 'contentType': 'application/json', 'payload': JSON.stringify(data) }; let response = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/' + method, options); if (response.getResponseCode() == 200) { return JSON.parse(response.getContentText()); } return false; } sendMessage(chatId, text, mode) { // mode (string): HTML, Markdown, MarkdownV2 // mode = mode || 'None'; return this.request('sendMessage', { 'chat_id': chatId, //TelegramData.message.from.id, 'text': text, 'parse_mode': mode }); } forwardMessage(chatId, fromChatId, messageId) { return this.request('forwardMessage', { 'chat_id': chatId, 'from_chat_id': fromChatId, 'message_id': messageId }); } }
- Assistant
La funzione tempMessage non funziona perché la variabile Bot non è definita all'interno di quella funzione. La variabile Bot è definita globalmente nella funzione doPost, ma non è accessibile all'interno della funzione tempMessage. Per Risolvere questo problema, è possibile passare l'istanza di TelegramBot come argomento alla funzione tempMessage, o definire globalmente la variabile Bot in modo che sia accessibile da tutte le funzioni all'interno dello script.
- User
mostrami come fare
- Assistant
Ecco come puoi modificare il codice per rendere la variabile Bot disponibile all'interno della funzione tempMessage:
- Definisci la variabile Bot globalmente all'inizio dello script, come segue:
var Bot = new TelegramBot();- Modifica la firma della funzione tempMessage per includere l'istanza di TelegramBot come argomento:
function tempMessage(chatId, Bot) {- Rimuovi la riga che dichiara nuovamente la variabile Bot all'interno della funzione tempMessage:
let bot = new TelegramBot();- Assicurati di passare l'istanza di TelegramBot quando chiami la funzione tempMessage dalla funzione doPost:
tempMessage(chatId, Bot);Con queste modifiche, la variabile Bot sarà disponibile all'interno della funzione tempMessage e la funzione dovrebbe funzionare correttamente.