Как сделать request запрос в тг

olol321

Участник
Автор темы
103
17
Версия MoonLoader
Другое
Не могу реализовать запрос в тг нужно request.get
делал так, ошибок нет, но не отправляет
requests.get("https://api.telegram.org/bot"..token_tg.."/sendMessage?chat_id="..chat_tg.."&text="..messageonjoin)
 

RedHolms

Известный
Проверенный
617
360
Lua:
local _thread_worker = function(url)
   local requests = require 'requests'
   local success, result = pcall(requests.request, 'GET', url, nil)
   if success then
      result.json, result.xml = nil, nil
      return { true, result }
   else
      return { false, result }
   end
end

makeRequest = function(url, params, successHandler, failHandler)
   -- format params
   if params and type(params) == 'table' and next(params) then
      url = url .. '?'
      local temp = ''
      for k, v in pairs(params) do
         url = url .. temp .. tostring(k) .. '=' .. tostring(v)
         temp = '&'
      end
   end

   local request_thread_runner = effil.thread(_thread_worker)
   lua_thread.create(function(url, successHandler, failHandler)
      local request_thread = request_thread_runner(url)

      local status, err
      while not err and status ~= 'completed' do
         status, err = request_thread:status()
      wait(0) end

      if err then
         return failHandler(0, err)
      else
         if status ~= 'completed' then
            return failHandler(1, status)
         end

         local retval = request_thread:get()
         local request_success, request_retval = retval[1], retval[2]

         if not request_success then
            return failHandler(2, request_retval)
         end

         return successHandler(request_retval)
      end
   end, url, successHandler, failHandler)
end

пример
Lua:
makeBotRequest = function(method, params)
   makeRequest("https://api.telegram.org/bot" .. BOT_TOKEN .. "/" .. method, params,
      function(result)
         -- check for http and telegram errors
         if result.status_code < 200 or result.status_code > 299 then
            if result.status_code == 400 and decodeJson(result.text) then goto telegram_error end

            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 10)")
            printChatError("Ошибка HTTP: %u", result.status_code)
            return
         end

         ::telegram_error::
         local json_response = decodeJson(result.text)
         if not json_response then
            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 11)")
            printChatError("Telegram вернул не JSON: %s", result.text)
            return
         end

         if not json_response.ok then
            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 12)")
            printChatError("Telegram вернул ошибку(Код: %u): %s", json_response.error_code, json_response.description)
            return
         end
      end,
      function(reason, info)
         printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: %u)", reason)
         if reason == 0 then
            printChatError("Ошибка effil: %s", tostring(info))
         elseif reason == 1 then
            printChatError("Поток не был завершён корректно: %s", tostring(info))
         elseif reason == 2 then
            printChatError("Ошибка запроса: %s", tostring(info))
         else
            printChatError("Неизвестная ошибка: %s", tostring(info))
         end
      end
   )
end
 

olol321

Участник
Автор темы
103
17
Lua:
local _thread_worker = function(url)
   local requests = require 'requests'
   local success, result = pcall(requests.request, 'GET', url, nil)
   if success then
      result.json, result.xml = nil, nil
      return { true, result }
   else
      return { false, result }
   end
end

makeRequest = function(url, params, successHandler, failHandler)
   -- format params
   if params and type(params) == 'table' and next(params) then
      url = url .. '?'
      local temp = ''
      for k, v in pairs(params) do
         url = url .. temp .. tostring(k) .. '=' .. tostring(v)
         temp = '&'
      end
   end

   local request_thread_runner = effil.thread(_thread_worker)
   lua_thread.create(function(url, successHandler, failHandler)
      local request_thread = request_thread_runner(url)

      local status, err
      while not err and status ~= 'completed' do
         status, err = request_thread:status()
      wait(0) end

      if err then
         return failHandler(0, err)
      else
         if status ~= 'completed' then
            return failHandler(1, status)
         end

         local retval = request_thread:get()
         local request_success, request_retval = retval[1], retval[2]

         if not request_success then
            return failHandler(2, request_retval)
         end

         return successHandler(request_retval)
      end
   end, url, successHandler, failHandler)
end

пример
Lua:
makeBotRequest = function(method, params)
   makeRequest("https://api.telegram.org/bot" .. BOT_TOKEN .. "/" .. method, params,
      function(result)
         -- check for http and telegram errors
         if result.status_code < 200 or result.status_code > 299 then
            if result.status_code == 400 and decodeJson(result.text) then goto telegram_error end

            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 10)")
            printChatError("Ошибка HTTP: %u", result.status_code)
            return
         end

         ::telegram_error::
         local json_response = decodeJson(result.text)
         if not json_response then
            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 11)")
            printChatError("Telegram вернул не JSON: %s", result.text)
            return
         end

         if not json_response.ok then
            printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: 12)")
            printChatError("Telegram вернул ошибку(Код: %u): %s", json_response.error_code, json_response.description)
            return
         end
      end,
      function(reason, info)
         printChatError("Ошибка при попытке выполнить запрос к Telegram! (Код Ошибки: %u)", reason)
         if reason == 0 then
            printChatError("Ошибка effil: %s", tostring(info))
         elseif reason == 1 then
            printChatError("Поток не был завершён корректно: %s", tostring(info))
         elseif reason == 2 then
            printChatError("Ошибка запроса: %s", tostring(info))
         else
            printChatError("Неизвестная ошибка: %s", tostring(info))
         end
      end
   )
end
я чет так подумал, а нахуя мне нужен этот телеграм?