Skip to content

Instantly share code, notes, and snippets.

@aNNiMON

aNNiMON/TelegramBot.own

Last active May 26, 2020
Embed
What would you like to do?
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