Управление RakSampLite через TG

7 СМЕРТНЫХ ГРЕХОВ

Известный
Автор темы
515
159
Версия SA-MP
  1. Любая
Как сделать отправку сообщений из ТГ в игру через бота
 
Последнее редактирование:
Решение
Lua:
local encoding = require("encoding")
local json = require("cjson")
local effil = require("effil")
encoding.default = 'CP1251'
u8 = encoding.UTF8

local TOKEN = "123:ASD"
local CHAT_ID = "4542452"

local updateid

function sendTG(msg)
    msg = msg:gsub('{......}', '')
    msg = encodeUrl(msg)
    async_http_request('https://api.telegram.org/bot' .. TOKEN .. '/sendMessage?chat_id=' .. CHAT_ID .. '&text='..msg,'', function(result) end)
end

function encodeUrl(str)
    str = str:gsub(' ', '%+')
    str = str:gsub('\n', '%%0A')
    return u8(str)
end

function requestRunner()
    return effil.thread(function(u, a)
        local https = require 'ssl.https'
        local ok, result = pcall(https.request, u, a)
        if ok then...

Loocking

Известный
1,374
471
 

7 СМЕРТНЫХ ГРЕХОВ

Известный
Автор темы
515
159
Это увидомление в ТГ, а мне надо же из ТГ в игру отправка
 

atomlin

Известный
581
388
Lua:
local encoding = require("encoding")
local json = require("cjson")
local effil = require("effil")
encoding.default = 'CP1251'
u8 = encoding.UTF8

local TOKEN = "123:ASD"
local CHAT_ID = "4542452"

local updateid

function sendTG(msg)
    msg = msg:gsub('{......}', '')
    msg = encodeUrl(msg)
    async_http_request('https://api.telegram.org/bot' .. TOKEN .. '/sendMessage?chat_id=' .. CHAT_ID .. '&text='..msg,'', function(result) end)
end

function encodeUrl(str)
    str = str:gsub(' ', '%+')
    str = str:gsub('\n', '%%0A')
    return u8(str)
end

function requestRunner()
    return effil.thread(function(u, a)
        local https = require 'ssl.https'
        local ok, result = pcall(https.request, u, a)
        if ok then
            return {true, result}
        else
            return {false, result}
        end
    end)
end

function async_http_request(url, args, resolve, reject)
    local runner = requestRunner()
    if not reject then reject = function() end end
    newTask(function()
        threadHandle(runner, url, args, resolve, reject)
    end)
end

function threadHandle(runner, url, args, resolve, reject)
    local t = runner(url, args)
    local r = t:get(0)
    while not r do
        r = t:get(0)
        wait(0)
    end
    local status = t:status()
    if status == 'completed' then
        local ok, result = r[1], r[2]
        if ok then resolve(result) else reject(result) end
        elseif err then
            reject(err)
        elseif status == 'canceled' then
            reject(status)
    end
    t:cancel(0)
end

function get_telegram_updates()
    newTask(function()
        while not updateid do wait(1) end
        local runner = requestRunner()
        local reject = function() end
        while true do
            url = 'https://api.telegram.org/bot'..TOKEN..'/getUpdates?chat_id='..CHAT_ID..'&offset=-1'
            threadHandle(runner, url, "", processing_telegram_messages, reject)
            wait(0)
        end
    end)
end

function getLastUpdate()
    async_http_request('https://api.telegram.org/bot'..TOKEN..'/getUpdates?chat_id='..CHAT_ID..'&offset=-1','',function(result)
        if result then
            local proc_table = json.decode(result)
            if proc_table.ok then
                if #proc_table.result > 0 then
                    local res_table = proc_table.result[1]
                    if res_table then
                        updateid = res_table.update_id
                    end
                else
                    updateid = 1
                end
            end
        end
    end)
end

function onLoad()
    getLastUpdate()
    get_telegram_updates()
end

function processing_telegram_messages(result)
    if result then
        local proc_table = json.decode(result)
        if proc_table.ok then
            if #proc_table.result > 0 then
                local res_table = proc_table.result[1]
                if res_table then
                    if res_table.update_id ~= updateid then
                        updateid = res_table.update_id
                        local message_from_user = res_table.message.text
                        if message_from_user then
                            local text = u8:decode(message_from_user)
                            if text:match('^!test') then
                                sendTG('Привет!')
                                return
                                
                            elseif text:match("^!") then
                                sendTG('Неизвестная команда!')
                            end
                        end
                    end
                end
            end
        end
    end
end
 

Pahanchik

Участник
57
24
Lua:
local encoding = require("encoding")
local json = require("cjson")
local effil = require("effil")
encoding.default = 'CP1251'
u8 = encoding.UTF8

local TOKEN = "123:ASD"
local CHAT_ID = "4542452"

local updateid

function sendTG(msg)
    msg = msg:gsub('{......}', '')
    msg = encodeUrl(msg)
    async_http_request('https://api.telegram.org/bot' .. TOKEN .. '/sendMessage?chat_id=' .. CHAT_ID .. '&text='..msg,'', function(result) end)
end

function encodeUrl(str)
    str = str:gsub(' ', '%+')
    str = str:gsub('\n', '%%0A')
    return u8(str)
end

function requestRunner()
    return effil.thread(function(u, a)
        local https = require 'ssl.https'
        local ok, result = pcall(https.request, u, a)
        if ok then
            return {true, result}
        else
            return {false, result}
        end
    end)
end

function async_http_request(url, args, resolve, reject)
    local runner = requestRunner()
    if not reject then reject = function() end end
    newTask(function()
        threadHandle(runner, url, args, resolve, reject)
    end)
end

function threadHandle(runner, url, args, resolve, reject)
    local t = runner(url, args)
    local r = t:get(0)
    while not r do
        r = t:get(0)
        wait(0)
    end
    local status = t:status()
    if status == 'completed' then
        local ok, result = r[1], r[2]
        if ok then resolve(result) else reject(result) end
        elseif err then
            reject(err)
        elseif status == 'canceled' then
            reject(status)
    end
    t:cancel(0)
end

function get_telegram_updates()
    newTask(function()
        while not updateid do wait(1) end
        local runner = requestRunner()
        local reject = function() end
        while true do
            url = 'https://api.telegram.org/bot'..TOKEN..'/getUpdates?chat_id='..CHAT_ID..'&offset=-1'
            threadHandle(runner, url, "", processing_telegram_messages, reject)
            wait(0)
        end
    end)
end

function getLastUpdate()
    async_http_request('https://api.telegram.org/bot'..TOKEN..'/getUpdates?chat_id='..CHAT_ID..'&offset=-1','',function(result)
        if result then
            local proc_table = json.decode(result)
            if proc_table.ok then
                if #proc_table.result > 0 then
                    local res_table = proc_table.result[1]
                    if res_table then
                        updateid = res_table.update_id
                    end
                else
                    updateid = 1
                end
            end
        end
    end)
end

function onLoad()
    getLastUpdate()
    get_telegram_updates()
end

function processing_telegram_messages(result)
    if result then
        local proc_table = json.decode(result)
        if proc_table.ok then
            if #proc_table.result > 0 then
                local res_table = proc_table.result[1]
                if res_table then
                    if res_table.update_id ~= updateid then
                        updateid = res_table.update_id
                        local message_from_user = res_table.message.text
                        if message_from_user then
                            local text = u8:decode(message_from_user)
                            if text:match('^!test') then
                                sendTG('Привет!')
                                return
                               
                            elseif text:match("^!") then
                                sendTG('Неизвестная команда!')
                            end
                        end
                    end
                end
            end
        end
    end
end
помоги плиаз, у меня не воркает. токен и чатид вписал, но не хоче работат
 

владикс

Потрачен
537
182
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.

Pahanchik

Участник
57
24
Последнее редактирование:

7 СМЕРТНЫХ ГРЕХОВ

Известный
Автор темы
515
159
  • Нравится
Реакции: Pahanchik

Dewize

Известный
437
88
помоги плиаз, у меня не воркает. токен и чатид вписал, но не хоче работат
[12:34:13] [LUA] C:\Users\XE\Desktop\RakSamp Lite\scripts\Tolik.lua:41: attempt to call global 'newTask' (a nil value)
stack traceback:
C:\Users\XE\Desktop\RakSamp Lite\scripts\Tolik.lua:41: in function 'async_http_request'
C:\Users\XE\Desktop\RakSamp Lite\scripts\Tolik.lua:79: in function 'getLastUpdate'
C:\Users\XE\Desktop\RakSamp Lite\scripts\Tolik.lua:97: in function <C:\Users\XE\Desktop\RakSamp Lite\scripts\Tolik.lua:96>


Это связано с библиотекой или нет?