Advertisement
AtomicScience

Telegram library

Jun 5th, 2020
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. local json = require("JSON")
  2. --local internet = require("internet")
  3. local internet = require("component").internet
  4. local serial = require("serialization")
  5. local fs = require("filesystem")
  6. local Promise = require("Promise")
  7. --local proxyUrl = "http://www.ipv6proxy.net/go.php?u="
  8.  
  9.  
  10. local function encode(data)
  11.     return data:gsub("([^%-%_%.%~])", function(char)
  12.         return string.format("%%%02X", string.byte(char))
  13.     end)
  14. end
  15.  
  16. local function getUrlForProxyificate(url)
  17.     if proxyUrl then
  18.         return proxyUrl .. encode(url)
  19.     else
  20.         return url
  21.     end
  22. end
  23.  
  24. function encodePOST(data)
  25.     local post = ""
  26.    
  27.     for k, v in pairs(data) do
  28.       post = post and (post .. "&") or ""
  29.       post = post .. tostring(k) .. "=" .. tostring(v)
  30.     end
  31.     return post
  32. end
  33.  
  34. function request(url,post,headers)
  35.     local handler = internet.request(url, encodePOST(post), headers)
  36.     if true then
  37.         local data = ""
  38.         local resCode,resName,resHeaders
  39.         while not resCode do
  40.             resCode, resName, resHeaders = handler:response()
  41.         end
  42.         repeat
  43.             local chunk, reason = handler.read(1024)
  44.             if chunk then
  45.                 data = data .. chunk
  46.             elseif reason then
  47.                 error(reason)
  48.             end
  49.             os.sleep(0)
  50.         until not chunk
  51.         return {
  52.             resCode = resCode,
  53.             resName = resName,
  54.             resHeaders = resHeaders,
  55.             payload = data,
  56.         }
  57.     else
  58.         error(handler)
  59.     end
  60. end
  61.  
  62. function sendRequest(token,method,POST)
  63.     local data = ""
  64.     local url = "https://vladcyb-bot.herokuapp.com/bot" .. token .. "/" .. method
  65.     data = request(getUrlForProxyificate(url),POST)
  66.     return json.decode(data.payload)
  67. end
  68.  
  69. local APIMetatable = {}
  70. APIMetatable.__index = function(self,key)
  71.     return function(post)
  72.         return Promise(function(resolve,reject)
  73.             local res = sendRequest(self.token,key,post)
  74.             if res.ok == true then
  75.                 resolve(res.result)
  76.             else
  77.                 reject(res.error_message)
  78.             end
  79.         end)
  80.     end
  81. end
  82.  
  83. function Bot(token)
  84.     local bot = {}
  85.     bot.API = {token=token}
  86.     setmetatable(bot.API,APIMetatable)
  87.     function bot.API.rawRequest(method,post)
  88.         return sendRequest(bot.API.token,method,post)
  89.     end
  90.     bot.longpoll = {callbacks={}}
  91.     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
  92.         bot.longpoll.callbacks[event] = bot.longpoll.callbacks[event] or {}
  93.         table.insert(bot.longpoll.callbacks[event],callback)
  94.     end
  95.     function bot.longpoll.start()
  96.         bot.longpoll.active = true
  97.         local errorCounter = 0
  98.         while bot.longpoll.active do
  99.             os.sleep(0)
  100.             local updates = sendRequest(token,"getUpdates",{
  101.                 offset = bot.longpoll.offset,
  102.                 --timeout = 1,
  103.             })
  104.            
  105.             if true then
  106.                 errorCounter = 0
  107.                 if updates.ok then
  108.                     local result = updates.res.result  
  109.                     for i = 1, #result do
  110.                         local update = result[i]
  111.                         for key, value in pairs(update) do
  112.                             local lngp = bot.longpoll.callbacks[key]
  113.                             if lngp then
  114.                                 for i = 1, #lngp do
  115.                                     lngp[i](update[key])
  116.                                 end
  117.                             end
  118.                         end
  119.                     end
  120.                     if #result > 0 then
  121.                         bot.longpoll.offset = result[#result].update_id+1
  122.                     end
  123.                 else
  124.                     print("error: ",updates.error_message)
  125.                 end
  126.             else
  127.                 if errorCounter > 3 then
  128.                     bot.longpoll.stop()
  129.                     print("Exiting: too many errors")
  130.                     error(updates)
  131.                 end
  132.                 errorCounter = errorCounter + 1
  133.                 print("internal error: ", updates, errorCounter)
  134.             end
  135.         end
  136.     end
  137.     function bot.longpoll.stop()
  138.         bot.longpoll.active = false
  139.     end
  140.     return bot
  141. end
  142. return Bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement