2016-05-29 19:08:39 +02:00
|
|
|
--[[
|
2016-08-23 06:16:32 +02:00
|
|
|
bindings.lua (rev. 2016/08/20)
|
2016-08-14 04:46:18 +02:00
|
|
|
otouto's bindings for the Telegram bot API.
|
|
|
|
https://core.telegram.org/bots/api
|
|
|
|
See the "Bindings" section of README.md for usage information.
|
2016-05-29 19:08:39 +02:00
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
Copyright 2016 topkecleon <drew@otou.to>
|
2016-10-04 20:16:20 +02:00
|
|
|
Modified by Brawl345 <https://github.com/Brawl345>
|
2016-10-04 16:07:15 +02:00
|
|
|
This code is licensed under the GNU AGPLv3. See /LICENSE for details.
|
2016-05-29 19:08:39 +02:00
|
|
|
]]--
|
2016-01-15 04:39:24 +01:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local bindings = {}
|
2016-01-15 04:39:24 +01:00
|
|
|
|
2016-08-27 05:25:55 +02:00
|
|
|
local https = require('ssl.https')
|
2016-09-08 00:28:20 +02:00
|
|
|
https.TIMEOUT = 10
|
2016-08-27 05:25:55 +02:00
|
|
|
local json = require('dkjson')
|
2016-05-29 19:08:39 +02:00
|
|
|
local ltn12 = require('ltn12')
|
2016-10-04 16:07:15 +02:00
|
|
|
local mp = require('multipart-post')
|
2016-05-29 19:08:39 +02:00
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
function bindings.init(token)
|
|
|
|
bindings.BASE_URL = 'https://api.telegram.org/bot' .. token .. '/'
|
|
|
|
return bindings
|
|
|
|
end
|
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
-- Build and send a request to the API.
|
|
|
|
-- Expecting self, method, and parameters, where method is a string indicating
|
|
|
|
-- the API method and parameters is a key/value table of parameters with their
|
|
|
|
-- values.
|
|
|
|
-- Returns the table response with success. Returns false and the table
|
|
|
|
-- response with failure. Returns false and false with a connection error.
|
|
|
|
-- To mimic old/normal behavior, it errs if used with an invalid method.
|
2016-08-23 06:16:32 +02:00
|
|
|
function bindings.request(method, parameters, file)
|
2016-05-29 19:08:39 +02:00
|
|
|
parameters = parameters or {}
|
|
|
|
for k,v in pairs(parameters) do
|
|
|
|
parameters[k] = tostring(v)
|
|
|
|
end
|
|
|
|
if file and next(file) ~= nil then
|
2016-07-03 01:20:04 +02:00
|
|
|
local file_type, file_name = next(file)
|
2016-07-04 01:29:51 +02:00
|
|
|
if not file_name then return false end
|
2016-07-03 01:20:04 +02:00
|
|
|
if string.match(file_name, '/tmp/') then
|
|
|
|
local file_file = io.open(file_name, 'r')
|
|
|
|
local file_data = {
|
|
|
|
filename = file_name,
|
2016-05-29 19:08:39 +02:00
|
|
|
data = file_file:read('*a')
|
2016-07-03 01:20:04 +02:00
|
|
|
}
|
|
|
|
file_file:close()
|
|
|
|
parameters[file_type] = file_data
|
|
|
|
else
|
|
|
|
local file_type, file_name = next(file)
|
|
|
|
parameters[file_type] = file_name
|
|
|
|
end
|
2016-05-29 19:08:39 +02:00
|
|
|
end
|
|
|
|
if next(parameters) == nil then
|
|
|
|
parameters = {''}
|
|
|
|
end
|
|
|
|
local response = {}
|
2016-10-04 20:16:20 +02:00
|
|
|
local body, boundary = mp.encode(parameters)
|
2016-08-27 16:12:46 +02:00
|
|
|
local success, code = https.request{
|
2016-08-24 15:38:29 +02:00
|
|
|
url = bindings.BASE_URL .. method,
|
2016-05-29 19:08:39 +02:00
|
|
|
method = 'POST',
|
|
|
|
headers = {
|
|
|
|
["Content-Type"] = "multipart/form-data; boundary=" .. boundary,
|
|
|
|
["Content-Length"] = #body,
|
|
|
|
},
|
|
|
|
source = ltn12.source.string(body),
|
|
|
|
sink = ltn12.sink.table(response)
|
|
|
|
}
|
|
|
|
local data = table.concat(response)
|
|
|
|
if not success then
|
2016-08-14 16:30:06 +02:00
|
|
|
print(method .. ': Connection error. [' .. code .. ']')
|
2016-05-29 19:08:39 +02:00
|
|
|
return false, false
|
2016-05-21 02:47:13 +02:00
|
|
|
else
|
2016-08-27 16:12:46 +02:00
|
|
|
local result = json.decode(data)
|
2016-05-29 19:08:39 +02:00
|
|
|
if not result then
|
|
|
|
return false, false
|
|
|
|
elseif result.ok then
|
|
|
|
return result
|
2016-10-04 20:16:20 +02:00
|
|
|
elseif result.description == 'Method not found' then
|
|
|
|
error(method .. ': Method not found.')
|
2016-08-14 04:46:18 +02:00
|
|
|
else
|
2016-05-29 19:08:39 +02:00
|
|
|
return false, result
|
|
|
|
end
|
2016-02-26 10:40:43 +01:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
function bindings.gen(_, key)
|
2016-08-24 15:38:29 +02:00
|
|
|
return function(params, file)
|
|
|
|
return bindings.request(key, params, file)
|
2016-02-26 10:40:43 +01:00
|
|
|
end
|
2015-07-19 22:46:06 +02:00
|
|
|
end
|
2016-05-29 19:08:39 +02:00
|
|
|
setmetatable(bindings, { __index = bindings.gen })
|
2016-04-08 23:12:02 +02:00
|
|
|
|
2016-08-14 16:34:25 +02:00
|
|
|
return bindings
|