Created
May 3, 2021 17:18
-
-
Save trecno/23b847e3a08a90a38adfce31c8fbf68d to your computer and use it in GitHub Desktop.
botDeTelegram.gs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var token = 'TOKEN DE TELEGRAM'; | |
var telegramUrl = 'https://api.telegram.org/bot' + token; | |
var spreadsheetId = 'ID DEL SPREADSHEET'; | |
function doGet(e) { | |
return HtmlService.createHtmlOutput('Hola'); | |
} | |
function sendText(id, answer) { | |
var url = telegramUrl + '/sendMessage?chat_id=' + id + '&text=' + answer; | |
UrlFetchApp.fetch(url); | |
} | |
function sendTextWithButtons(id, answer, keyboard) { | |
var data = { | |
method: 'post', | |
payload: { | |
method: 'sendMessage', | |
chat_id: String(id), | |
text: answer, | |
reply_markup: keyboard | |
} | |
} | |
UrlFetchApp.fetch(telegramUrl + '/', data); | |
} | |
function doPost(e) { | |
var contents = JSON.parse(e.postData.contents); | |
if (contents.message) { | |
var text = contents.message.text; | |
var id = contents.message.chat.id; | |
var name = contents.message.chat.first_name; | |
var spreadsheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName('Mensajes'); | |
spreadsheet.appendRow([new Date(), id, name, text]); | |
GmailApp.sendEmail('AQUÍ VA EL EMAIL QUE RECIBE EL CORREO', 'Bot de Telegram', JSON.stringify(contents)); | |
switch (text) { | |
case '/start': | |
var answer = 'Me has iniciado'; | |
sendText(id, answer); | |
break; | |
case '/info': | |
var answer = 'Esto es un bot de prueba'; | |
sendText(id, answer); | |
break; | |
case '/enlaces': | |
var keyboard = { | |
'inline_keyboard': [ | |
[{ | |
'text': 'Youtube', | |
'url': 'https://youtube.com' | |
},{ | |
'text': 'Google', | |
'url': 'https://google.com' | |
}] | |
] | |
}; | |
sendTextWithButtons(id, 'Algunos enlaces', JSON.stringify(keyboard)); | |
break; | |
case '/acciones': | |
var keyboard = { | |
'inline_keyboard': [ | |
[{ | |
'text': 'Acción 1', | |
'callback_data': '1' | |
}], | |
[{ | |
'text': 'Acción 2', | |
'callback_data': '2' | |
}] | |
] | |
}; | |
sendTextWithButtons(id, 'Algunas acciones', JSON.stringify(keyboard)); | |
break; | |
default: | |
var answer = 'No te he entendido'; | |
sendText(id, answer); | |
break; | |
} | |
} else if (contents.callback_query) { | |
var id = contents.callback_query.message.chat.id; | |
var data = contents.callback_query.data; | |
var answer = 'Has pulsado la acción número ' + data; | |
sendText(id, answer); | |
} | |
} |
Muchas gracias por tu explicación. Me ha servido para entenderlo mejor.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excelente trabajo!
Y muy bien explicado en tu canal de youtube!:
Video 1: https://www.youtube.com/watch?v=ixaRvBBNIzM
Video 2: https://www.youtube.com/watch?v=3wXyad6X2zo
Gracias!