Last active
May 26, 2020 21:29
-
-
Save aNNiMON/18f1894447dfa72000a83011511a817c to your computer and use it in GitHub Desktop.
This file contains 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 { | |
def TelegramBot(token) { | |
this.token = token | |
} | |
def getUpdatesSync() = this.invokeJsonSync("getUpdates").result | |
def getFileSync(fileId) = this.invokeJsonSync("getFile", {"file_id": fileId}).result | |
def sendPhoto(chatId, photo, callback = 0) { | |
return this.invokeJson("sendPhoto", { | |
"chat_id": chatId, | |
"photo": photo | |
}, default(callback, def(r) {})) | |
} | |
def sendPhotoSync(chatId, photo) { | |
return this.invokeJsonSync("sendPhoto", { | |
"chat_id": chatId, | |
"photo": photo | |
}) | |
} | |
def sendMediaGroup(chatId, media, callback = 0) { | |
return this.invokeJson("sendMediaGroup", { | |
"chat_id": chatId, | |
"media": jsonencode(media) | |
}, default(callback, def(r) {})) | |
} | |
def sendMediaGroupSync(chatId, photo) { | |
return this.invokeJsonSync("sendMediaGroup", { | |
"chat_id": chatId, | |
"media": jsonencode(media) | |
}) | |
} | |
def createUrl(method, params) { | |
str = "https://api.telegram.org/bot" + this.token + "/" + method + "?" | |
params["access_token"] = this.token | |
for k, v : params | |
str += k + "=" + urlencode(v) + "&" | |
return str | |
} | |
def invokeJsonSync(method, params = {}) { | |
return sync(def(ret) = http(this.createUrl(method, params), combine(::jsondecode, ret))) | |
} | |
def toFileUrl(filePath) = "https://api.telegram.org/file/bot" + this.token + "/" + filePath | |
def invokeJson(method, params, callback) { | |
return http(this.createUrl(method, params), combine(::jsondecode, callback)) | |
} | |
def invoke(method, params, callback) { | |
return http(this.createUrl(method, params), callback) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment