Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require("JSON")
- --local internet = require("internet")
- local internet = require("component").internet
- local serial = require("serialization")
- local fs = require("filesystem")
- local Promise = require("Promise")
- --local proxyUrl = "http://www.ipv6proxy.net/go.php?u="
- local function encode(data)
- return data:gsub("([^%-%_%.%~])", function(char)
- return string.format("%%%02X", string.byte(char))
- end)
- end
- local function getUrlForProxyificate(url)
- if proxyUrl then
- return proxyUrl .. encode(url)
- else
- return url
- end
- end
- function encodePOST(data)
- local post = ""
- for k, v in pairs(data) do
- post = post and (post .. "&") or ""
- post = post .. tostring(k) .. "=" .. tostring(v)
- end
- return post
- end
- function request(url,post,headers)
- local handler = internet.request(url, encodePOST(post), headers)
- if true then
- local data = ""
- local resCode,resName,resHeaders
- while not resCode do
- resCode, resName, resHeaders = handler:response()
- end
- repeat
- local chunk, reason = handler.read(1024)
- if chunk then
- data = data .. chunk
- elseif reason then
- error(reason)
- end
- os.sleep(0)
- until not chunk
- return {
- resCode = resCode,
- resName = resName,
- resHeaders = resHeaders,
- payload = data,
- }
- else
- error(handler)
- end
- end
- function sendRequest(token,method,POST)
- local data = ""
- local url = "https://vladcyb-bot.herokuapp.com/bot" .. token .. "/" .. method
- data = request(getUrlForProxyificate(url),POST)
- return json.decode(data.payload)
- end
- local APIMetatable = {}
- APIMetatable.__index = function(self,key)
- return function(post)
- return Promise(function(resolve,reject)
- local res = sendRequest(self.token,key,post)
- if res.ok == true then
- resolve(res.result)
- else
- reject(res.error_message)
- end
- end)
- end
- end
- function Bot(token)
- local bot = {}
- bot.API = {token=token}
- setmetatable(bot.API,APIMetatable)
- function bot.API.rawRequest(method,post)
- return sendRequest(bot.API.token,method,post)
- end
- bot.longpoll = {callbacks={}}
- function bot.longpoll.on(event,callback) --message, edited_message, channel_post, edited_channel_post, inline_query, chosen_inline_result, callback_query, shipping_query, pre_checkout_query
- bot.longpoll.callbacks[event] = bot.longpoll.callbacks[event] or {}
- table.insert(bot.longpoll.callbacks[event],callback)
- end
- function bot.longpoll.start()
- bot.longpoll.active = true
- local errorCounter = 0
- while bot.longpoll.active do
- os.sleep(0)
- local updates = sendRequest(token,"getUpdates",{
- offset = bot.longpoll.offset,
- --timeout = 1,
- })
- if true then
- errorCounter = 0
- if updates.ok then
- local result = updates.res.result
- for i = 1, #result do
- local update = result[i]
- for key, value in pairs(update) do
- local lngp = bot.longpoll.callbacks[key]
- if lngp then
- for i = 1, #lngp do
- lngp[i](update[key])
- end
- end
- end
- end
- if #result > 0 then
- bot.longpoll.offset = result[#result].update_id+1
- end
- else
- print("error: ",updates.error_message)
- end
- else
- if errorCounter > 3 then
- bot.longpoll.stop()
- print("Exiting: too many errors")
- error(updates)
- end
- errorCounter = errorCounter + 1
- print("internal error: ", updates, errorCounter)
- end
- end
- end
- function bot.longpoll.stop()
- bot.longpoll.active = false
- end
- return bot
- end
- return Bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement