Created
June 8, 2020 18:47
-
-
Save VycktorStark/f85d56a99c6131bd17857a252e56a046 to your computer and use it in GitHub Desktop.
Telegram-bot-api in lua
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
local URL = require('socket.url') | |
local json = require('cjson') | |
local http = require('resty.http') | |
local api_errors = require('api_bad_requests') | |
local BASE_URL = 'https://api.telegram.org/bot' .. os.getenv("TG_TOKEN") | |
local api = {} | |
local function getCode(err) | |
local err = err:lower() | |
for k,v in pairs(api_errors) do | |
if err:match(v) then | |
return k | |
end | |
end | |
return 7 --if unknown | |
end | |
local function code2text(code) | |
--the default error description can't be sent as output, so a translation is needed | |
if code == 101 or code == 105 or code == 107 then | |
return ("I'm not an admin, I can't kick people") | |
elseif code == 102 or code == 104 then | |
return ("I can't kick or ban an admin") | |
elseif code == 103 then | |
return ("There is no need to unban in a normal group") | |
elseif code == 106 or code == 134 then | |
return ("This user is not a chat member") | |
elseif code == 7 then | |
return false | |
end | |
return false | |
end | |
local function log_error(method, code, extras, description) | |
if not method or not code then return end | |
local ignored_errors = {110, 111, 116, 118, 131, 150, 155, 403, 429} | |
for _, ignored_code in pairs(ignored_errors) do | |
if tonumber(code) == tonumber(ignored_code) then return end | |
end | |
local text = 'Type: #badrequest\nMethod: #'..method..'\nCode: #n'..code | |
if description then | |
text = text..'\nDesc: '..description | |
end | |
if extras then | |
if next(extras) then | |
for i, extra in pairs(extras) do | |
text = text..'\n#more'..i..': '..extra | |
end | |
else | |
text = text..'\n#more: empty' | |
end | |
else | |
text = text..'\n#more: nil' | |
end | |
api.sendLog(text) | |
end | |
----------------- INIT FUNCTION -------------- | |
local function Request(url, body) | |
-- Function request | |
local client = http.new() | |
local success, code, headers, status = client:request_uri(url, { | |
method = "POST", | |
headers = { | |
['Content-Type'] = 'application/x-www-form-urlencoded', | |
}, | |
body = ngx.encode_args(body) | |
}) | |
local r = { | |
success = success or "false", | |
code = code or "0", | |
headers = table.concat(headers or {"no headers"}), | |
status = status or "0", | |
body = table.concat(response or {"no response"}), | |
} | |
return r | |
end | |
function api.setWebhook(url, max_connections, allowed_updates) | |
-- Function SetWebhook | |
local body = {} | |
body.url = os.getenv("HOOK_URL") | |
body.max_connections = os.getenv("MAX_CONNECTIONS") or 40 | |
body.allowed_updates =json.encode({"message", "edited_message", "callback_query"}) | |
Request(BASE_URL..'/setWebhook', body) | |
end | |
function api.getMe() | |
-- get info bot | |
local url = BASE_URL.. '/getMe' | |
local body = {""} | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.sendMessage(chat_id, text, parse_mode, reply_markup, reply_to_message_id, disable_web_page_preview, disable_notification) | |
-- Function SendMenssage | |
local url = BASE_URL.. '/sendMessage' | |
if not chat_id then return nil, "No Local Chat ID" end | |
if not text then return nil, "No Text" end | |
if (parse_mode == true) then | |
parse_mode = 'Markdown' | |
end | |
local parse_mode = parse_mode:upper():gsub('ARKDOWN', 'arkdown') | |
local allowed_parse_mode = { | |
["Markdown"] = true, | |
["HTML"] = true | |
} | |
if (not allowed_parse_mode[parse_mode]) then parse_mode = "" end | |
local body = {} | |
body.chat_id = chat_id | |
body.text = tostring(text) | |
body.parse_mode = parse_mode | |
body.disable_web_page_preview = tostring(disable_web_page_preview) | |
body.disable_notification = tostring(disable_notification) | |
body.reply_to_message_id = tonumber(reply_to_message_id) | |
body.reply_markup = json.encode(reply_markup) or "" | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.sendReply(msg, text, markd, reply_markup, link_preview) | |
-- Function SendMessage to id message | |
return api.sendMessage(msg.chat.id, text, markd, reply_markup, msg.message_id, link_preview) | |
end | |
function api.sendAdmin(text, markdown) | |
-- Function SendMessage to id chat Admin | |
return api.sendMessage(json.decode(os.getenv("LOG")).admin, text, 'markdown') | |
end | |
function api.sendLog(text, markdown) | |
-- Function SendMessage to id chat Log | |
return api.sendMessage(json.decode(os.getenv("LOG")).chat or json.decode(os.getenv("LOG")).admin, text, 'markdown') | |
end | |
function api.forwardMessage(chat_id, from_chat_id, message_id, disable_notification) | |
-- Function forwardMessage to the id of a chat | |
local url = BASE_URL.. '/forwardMessage' | |
if not chat_id then return nil, "No Local Chat ID" end | |
if not from_chat_id then return nil, "No chat ID sending" end | |
if not message_id then return nil, "No message ID" end | |
local body = {""} | |
body.chat_id = chat_id | |
body.from_chat_id = from_chat_id | |
body.disable_notification = tostring(disable_notification) | |
body.message_id = tonumber(message_id) | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.getChatAdministrators(chat_id) | |
-- Function get Chat Administrators | |
local url = BASE_URL.. '/getChatAdministrators' | |
if not chat_id then return nil, "No Local Chat ID" end | |
local body = {} | |
body.chat_id = chat_id | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.unbanChatMember(chat_id, user_id) | |
local url = BASE_URL.. '/unbanChatMember' | |
if not chat_id then return nil, "No Local Chat ID" end | |
if not user_id then return nil, "No ID from Chat" end | |
local request_body = {} | |
request_body.chat_id = chat_id | |
request_body.user_id = tonumber(user_id) | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.getFile(file_id) | |
local url = BASE_URL.. '/getFile' | |
if not file_id then return nil, "file_id not specified" end | |
local request_body = {} | |
request_body.file_id = file_id | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
function api.kickChatMember(chat_id, user_id) | |
local url = BASE_URL.. '/kickChatMember' | |
if not chat_id then return nil, "No Local Chat ID" end | |
if not user_id then return nil, "No ID user" end | |
local body = {} | |
body.chat_id = chat_id | |
body.user_id = tonumber(user_id) | |
local response = Request(url,body) | |
if (response.success == 1) then | |
return json.decode(response.body) | |
else | |
return nil, "Fail" | |
end | |
end | |
return api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment