Created
June 27, 2021 20:36
-
-
Save rogergcc/575a375e8cb14963ada9dae31855685f to your computer and use it in GitHub Desktop.
TelegramBot class for send .gs or js
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
class TelegramBot { | |
constructor(token) { | |
this.url = "https://api.telegram.org/bot" + token | |
//this.url = "https://api.telegram.org/bot" + token + "/" | |
} | |
getProp(key){ | |
return PropertiesService.getScriptProperties().getProperty(key) | |
} | |
setProp(key, value) { | |
PropertiesService.getScriptProperties().setProperty(key, value) | |
} | |
//1ero Register url | |
setWebhook() { | |
const urlWebhook = this.url + "/setWebhook?url=" + webAppUrl; | |
const response = UrlFetchApp.fetch(urlWebhook); | |
console.log(response); | |
Logger.log('telegram response status is ' + response.getResponseCode()); | |
} | |
//1.1 obtain webhook info | |
getWebHook() { | |
let response = UrlFetchApp.fetch(tgBotUrl + "/getWebhookInfo"); | |
if (response.getResponseCode() == 200) { | |
let data = JSON.parse(response.getContentText()) | |
Logger.log('current webhook ur is ' + data.result.url); | |
} else { | |
Logger.log('telegram response status is ' + response.getResponseCode()); | |
} | |
} | |
getMyUrl() { | |
return this.url; | |
} | |
get(url) { | |
try { | |
const response = UrlFetchApp.fetch(url) | |
return JSON.parse(response.getContentText()) | |
} catch (e) { | |
return { error: e.message } | |
} | |
} | |
//Send message to chat | |
//param {chat_id,text} | |
sendMessage(payload){ | |
//let payload = {chat_id: id_message,text: text} | |
let options = { | |
'method' : 'post', | |
'contentType': 'application/json', | |
'payload': JSON.stringify(payload) | |
} | |
return UrlFetchApp.fetch(this.getMyUrl() + "/sendMessage", options); | |
} | |
//optional - reply message | |
replyMessage(chat_id, message_id, message) { | |
const url = `${this.url}sendMessage?chat_id=${chat_id}&reply_to_message_id=${message_id}&text=${message}` | |
return this.get(url) | |
} | |
//Send text - Markdown values | |
sendText(chatId, text, keyBoard) { | |
const data = { | |
method: "post", | |
payload: { | |
method: "sendMessage", | |
chat_id: String(chatId), | |
text: text, | |
parse_mode: "HTML", | |
reply_markup: JSON.stringify(keyBoard) | |
} | |
}; | |
UrlFetchApp.fetch(this.getMyUrl() + '/', data); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment