Last active
September 11, 2024 20:33
-
-
Save aNNiMON/18f1894447dfa72000a83011511a817c to your computer and use it in GitHub Desktop.
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
use okhttp, http, json | |
class TelegramBot { | |
def TelegramBot(token) { | |
this.token = token | |
this.client = okhttp.newClient() | |
.callTimeout(6, "minutes") | |
.connectTimeout(5, "minutes") | |
.readTimeout(5, "minutes") | |
.build() | |
} | |
def getUpdatesSync(offset, limit = 100, timeout = 55) | |
= this.invokeJsonSync("getUpdates", { | |
"offset": offset, | |
"limit": limit, | |
"timeout": timeout | |
}).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 toFileUrl(filePath) = "https://api.telegram.org/file/bot" + this.token + "/" + filePath | |
def invokeJson(method, params, callback) { | |
return this.invoke(method, params, combine(::jsondecode, callback)) | |
} | |
def invokeJsonSync(method, params = {}, fallback = []) { | |
return jsondecode(this._apiCall(method, params))// : {"result": fallback} | |
} | |
def invoke(method, params, callback) { | |
return callback(this._apiCall(method, params)) | |
} | |
def _apiCall(method, params) = okhttp.request() | |
.url(this.createUrl(method, params)).get() | |
.newCall(this.client) | |
.execute().body().string() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment