CLEO Help Discord WebHook

CLEO related

Saam

Active member
Joined
Apr 18, 2021
Messages
107
Reaction score
61
Location
Argentina
Hi, I'm trying to create requests with the discord API, the main snippet I want to "rewrite" is this (.LUA)
JavaScript:
local encoding = require 'encoding' -- подключаем для корректной отправки русских букв
encoding.default = 'CP1251'
u8 = encoding.UTF8
local sampev = require 'lib.samp.events' -- подключаем для хука отправки ответа на диалог
local effil = require 'effil' -- для ассинхронных запросов

local url = 'URL'
local data = {
   ['content'] = '', -- текст (меняется через команду, так что можно оставить пустым)
   ['username'] = 'Sended from .lua script!', -- ник отправителя
   ['avatar_url'] = 'https://c.tenor.com/Z9mXH7-MlcsAAAAS/sexy-black-man-thirst-trap.gif', -- ссылка на аватарку (можно убрать, будет дефолтная)
   ['tts'] = false, -- tts - text to speech - читалка сообщений (true/false)
   -- так же можно сделать еще много чего, подробнее тут: https://discord.com/developers/docs/resources/webhook
}

function main()
   while not isSampAvailable() do wait(0) end
   sampRegisterChatCommand('ds.msg', function(arg)
      data['username'] = sampGetPlayerNickname(select(2, sampGetPlayerIdByCharHandle(PLAYER_PED))) -- ник отправителя = ник в игре
      data['content'] = arg -- делаем что бы текст сообщения был равен тексту который мы ввели после команды
      -- отправляем запрос
      asyncHttpRequest('POST', url, {headers = {['content-type'] = 'application/json'}, data = u8(encodeJson(data))},
      function(response)
         print('[WebHook] [OK] отправлено!')
      end,
      function(err)
         print('[WebHook] [ERROR] error: '..err)
      end)
   end)
   wait(-1)
end

-- функция для отправки ассинхронных сообщений
function asyncHttpRequest(method, url, args, resolve, reject)
   local request_thread = effil.thread(function (method, url, args)
      local requests = require 'requests'
      local result, response = pcall(requests.request, method, url, args)
      if result then
         response.json, response.xml = nil, nil
         return true, response
      else
         return false, response
      end
   end)(method, url, args)
   -- Если запрос без функций обработки ответа и ошибок.
   if not resolve then resolve = function() end end
   if not reject then reject = function() end end
   -- Проверка выполнения потока
   lua_thread.create(function()
      local runner = request_thread
      while true do
         local status, err = runner:status()
         if not err then
            if status == 'completed' then
               local result, response = runner:get()
               if result then
                  resolve(response)
               else
                  reject(response)
               end
               return
            elseif status == 'canceled' then
               return reject(status)
            end
         else
            return reject(err)
         end
         wait(0)
      end
   end)
end
I tried to follow the example of this thread, but it doesn't work, what's wrong?
PHP:
{$CLEO}
0000: NOP

repeat
    wait 0
until 0AFA:

// host
alloc 0@ 30
0AD3: 0@ = format "discord.com"

// path
alloc 1@ 200
0AD3: 1@ = format "api/webhooks/1015806219421757530/a21OfargEvfX7D2B5irPX0Bf_sAqqQGBV8lrpbciUUB6TFE16_TSwLq0pVA3MD-COfF_"

// data
alloc 2@ 1000
//0AD3: 2@ = format "content=testing message"
0AD3: 2@ = format "content=testing message&username=custom_username"

// response buffer
alloc 3@ 1500

while true
    wait 0
    if
    0ADC: "X"
    then
        0AB1: call_scm_func @HTTP_POST 5 host 0@ file_path 1@ response_buffer 3@ data 2@ is_https true _ret_is_success 31@
        if
        31@ == true
        then
            chatmsg "Respuesta = %s" 0xFFFFFF 3@
        else
            chatmsg "Fail" -1
        end
    end
end


{$INCLUDE includes/http_requests.txt}
http_requests.txt
 

Juli4n

Active member
Joined
Mar 31, 2021
Messages
43
Solutions
1
Reaction score
19
Location
Argentina
I haven't edited my cleos with http request for a long time, but I think this is your mistake.

Code:
// path
alloc 1@ 200
0AD3: 1@ = format "api/webhooks/1015806219421757530/a21OfargEvfX7D2B5irPX0Bf_sAqqQGBV8lrpbciUUB6TFE16_TSwLq0pVA3MD-COfF_"
 

Saam

Active member
Joined
Apr 18, 2021
Messages
107
Reaction score
61
Location
Argentina
I haven't edited my cleos with http request for a long time, but I think this is your mistake.

Code:
// path
alloc 1@ 200
0AD3: 1@ = format "api/webhooks/1015806219421757530/a21OfargEvfX7D2B5irPX0Bf_sAqqQGBV8lrpbciUUB6TFE16_TSwLq0pVA3MD-COfF_"
I can't find what's wrong
 

Juli4n

Active member
Joined
Mar 31, 2021
Messages
43
Solutions
1
Reaction score
19
Location
Argentina
I can't find what's wrong
I don't remember the reason, I haven't done cleos like this for a long time, but I had to use the "call" function to make it work correctly.

C#:
:ConstructPath
// 0AC8: 31@ = allocate_memory_size 260
// 0AB1: call_scm_func @ConstructPath 1 31@
0AC8: 30@ = allocate_memory_size 260
0AC8: 31@ = allocate_memory_size 260
0AD3: 30@ = format "" // ID
0AD3: 31@ = format "" // Token
0AD3: 0@ = format "api/webhooks/%s/%s" 30@ 31@
0AC9: free_allocated_memory 30@
0AC9: free_allocated_memory 31@
ret 0
 

Juli4n

Active member
Joined
Mar 31, 2021
Messages
43
Solutions
1
Reaction score
19
Location
Argentina
I don't remember the reason, I haven't done cleos like this for a long time, but I had to use the "call" function to make it work correctly.

C#:
:ConstructPath
// 0AC8: 31@ = allocate_memory_size 260
// 0AB1: call_scm_func @ConstructPath 1 31@
0AC8: 30@ = allocate_memory_size 260
0AC8: 31@ = allocate_memory_size 260
0AD3: 30@ = format "" // ID
0AD3: 31@ = format "" // Token
0AD3: 0@ = format "api/webhooks/%s/%s" 30@ 31@
0AC9: free_allocated_memory 30@
0AC9: free_allocated_memory 31@
ret 0
That's how @monday posted it, idk
 
Top