Skip to content

Instantly share code, notes, and snippets.

@sergiks
Created August 5, 2018 17:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiks/2849bc024edfaa15f79185d725fa9879 to your computer and use it in GitHub Desktop.
Save sergiks/2849bc024edfaa15f79185d725fa9879 to your computer and use it in GitHub Desktop.
Google Apps Script Telegram bot parts
function TelegramClient(token) {
var _token = token;
this.getURL = function() {
return "https://api.telegram.org/bot" + _token;
}
this.getResponse = function( method, data) {
var response = UrlFetchApp
.fetch(
this.getURL() + '/' + method
,{method: "post", payload: data}
)
;
// Logger.log( "Telegram: " + response.getResponseCode());
return response;
}
this.getMe = function() {
return this.getResponse("getMe").getContentText();
}
this.getFile = function(file_id) {
return this.getResponse("getFile", {file_id: file_id}).getContentText();
}
this.setWebhook = function(url) {
return this.getResponse("setWebhook", {url: url}).getContentText();
}
this.getWebhookInfo = function() {
return this.getResponse("getWebhookInfo").getContentText();
}
this.sendMessage = function(chatId, text, parse_mode) {
parse_mode = parse_mode || '';
return this.getResponse("sendMessage", {chat_id: ''+chatId, text: text, parse_mode: parse_mode}).getContentText();
}
this.sendMessageRaw = function(options) {
return this.getResponse("sendMessage", options).getContentText();
}
this.sendPhoto = function(chatId, photo, caption) {
return this.getResponse("sendPhoto", {chat_id: ''+chatId, photo: photo, caption: caption || ""}).getContentText();
}
this.sendDocument = function(chatId, document, caption) {
return this.getResponse("sendDocument", {chat_id: ''+chatId, document: document, caption: caption || ""}).getContentText();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment