Skip to content

Instantly share code, notes, and snippets.

@aNNiMON
Last active May 26, 2020 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aNNiMON/18f1894447dfa72000a83011511a817c to your computer and use it in GitHub Desktop.
Save aNNiMON/18f1894447dfa72000a83011511a817c to your computer and use it in GitHub Desktop.
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