require('moonloader')
local effil = require('effil')
local multipart = require('multipart-post')
local dkjson = require('dkjson')
local encoding = require('encoding')
encoding.default = 'CP1251'
u8 = encoding.UTF8
function main()
if (not isSampLoaded() or not isSampfuncsLoaded()) then
return
end
while (not isSampAvailable()) do
wait(0)
end
sampRegisterChatCommand('sendPhoto', function ()
--[[ ОБЯЗАТЕЛЬНО В ПОТОКЕ ]]--
lua_thread.create(function ()
--[[ Функция возвращает 1) Результат выполнения, 2) JSON ответ ]]
local result, response = telegramRequest(
'POST', --[[ https://en.wikipedia.org/wiki/POST_(HTTP) ]]--
'sendPhoto', --[[ https://core.telegram.org/bots/api#sendphoto ]]--
{ --[[ Аргументы, см. https://core.telegram.org/bots/api#sendphoto ]]--
['chat_id'] = '761579032', --[[ chat_id ]]--
['caption'] = u8:encode('Привет!') --[[ caption (Обязательно u8) ]]--
},
{ --[[ Сам файл, сюда можно передавать как PATH(Путь к файлу), так и FILE_ID(См. https://core.telegram.org/bots/) ]]--
['photo'] = string.format('%s\\smirk.png', getWorkingDirectory()) --[[ или же ==getWorkingDirectory() .. '\\smirk.png'== ]]--
},
'123123123213123' --[[ Токен Бота ]]
)
-- Caption: Привет! | MessageID: 12583 | ChatID: 761579032
print(string.format('Caption: %s | MessageID: %s | ChatID: %s', u8:decode(response.result.caption), response.result.message_id, response.result.chat.id))
end)
end)
wait(-1)
end
function telegramRequest(requestMethod, telegramMethod, requestParameters, requestFile, botToken, debugMode)
--[[ Arguments Part ]]--
--[[ Argument #1 (requestMethod) ]]--
local requestMethod = requestMethod or 'POST'
if (type(requestMethod) ~= 'string') then
error('[MoonGram Error] In Function "telegramRequest", Argument #1(requestMethod) Must Be String.')
end
if (requestMethod ~= 'POST' and requestMethod ~= 'GET' and requestMethod ~= 'PUT' and requestMethod ~= 'DETELE') then
error('[MoonGram Error] In Function "telegramRequest", Argument #1(requestMethod) Dont Have "%s" Request Method.', tostring(requestMethod))
end
--[[ Argument #2 (telegramMethod) ]]--
local telegramMethod = telegramMethod or nil
if (type(requestMethod) ~= 'string') then
error('[MoonGram Error] In Function "telegramRequest", Argument #2(telegramMethod) Must Be String.\nCheck: https://core.telegram.org/bots/api')
end
--[[ Argument #3 (requestParameters) ]]--
local requestParameters = requestParameters or {}
if (type(requestParameters) ~= 'table') then
error('[MoonGram Error] In Function "telegramRequest", Argument #3(requestParameters) Must Be Table.')
end
for key, value in ipairs(requestParameters) do
if (#requestParameters ~= 0) then
requestParameters[key] = tostring(value)
else
requestParameters = {''}
end
end
--[[ Argument #4 (botToken) ]]--
local botToken = botToken or nil
if (type(botToken) ~= 'string') then
error('[MoonGram Error] In Function "telegramRequest", Argument #4(botToken) Must Be String.')
end
--[[ Argument #5 (debugMode) ]]--
local debugMode = debugMode or false
if (type(debugMode) ~= 'boolean') then
error('[MoonGram Error] In Function "telegramRequest", Argument #5(debugMode) Must Be Boolean.')
end
if (requestFile and next(requestFile) ~= nil) then
local fileType, fileName = next(requestFile)
local file = io.open(fileName, 'rb')
if (file) then
lua_thread.create(function ()
requestParameters[fileType] = {
filename = fileName,
data = file:read('*a')
}
end)
file:close()
else
requestParameters[file_type] = fileName
end
end
local requestData = {
['method'] = tostring(requestMethod),
['url'] = string.format('https://api.telegram.org/bot%s/%s', tostring(botToken), tostring(telegramMethod))
}
local body, boundary = multipart.encode(requestParameters)
--[[ Request Part ]]--
local thread = effil.thread(function (requestData, body, boundary)
local response = {}
--[[ Include Libraries ]]--
local channel_library_requests = require('ssl.https')
local channel_library_ltn12 = require('ltn12')
--[[ Manipulations ]]--
local _, source = pcall(channel_library_ltn12.source.string, body)
local _, sink = pcall(channel_library_ltn12.sink.table, response)
--[[ Request ]]--
local result, _ = pcall(channel_library_requests.request, {
['url'] = requestData['url'],
['method'] = requestData['method'],
['headers'] = {
['Accept'] = '*/*',
['Accept-Encoding'] = 'gzip, deflate',
['Accept-Language'] = 'en-us',
['Content-Type'] = string.format('multipart/form-data; boundary=%s', tostring(boundary)),
['Content-Length'] = #body
},
['source'] = source,
['sink'] = sink
})
if (result) then
return { true, response }
else
return { false, response }
end
end)(requestData, body, boundary)
local result = thread:get(0)
while (not result) do
result = thread:get(0)
wait(0)
end
--[[ Running || Paused || Canceled || Completed || Failed ]]--
local status, error = thread:status()
if (not error) then
if (status == 'completed') then
local response = dkjson.decode(result[2][1])
--[[ result[1] = boolean ]]--
if (result[1]) then
return true, response
else
return false, response
end
elseif (status ~= 'running' and status ~= 'completed') then
return false, string.format('[TelegramLibrary] Error; Effil Thread Status was: %s', tostring(status))
end
else
return false, error
end
thread:cancel(0)
end