Advertisement
HeroBrine1st

OpenComputers Telegram Bot API

Feb 3rd, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 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 request(url,post,headers)
  25. local success, handler = pcall(internet.request,url,post,headers)
  26. if success then
  27. local data = ""
  28. local resCode,resName,resHeaders
  29. while not resCode do
  30. resCode, resName, resHeaders = handler:responce()
  31. end
  32. repeat
  33. local chunk, reason = handler:read()
  34. if chunk then
  35. data = data .. chunk
  36. elseif reason then
  37. error(reason)
  38. end
  39. os.sleep(0)
  40. until not chunk
  41. return {
  42. resCode = resCode,
  43. resName = resName,
  44. resHeaders = resHeaders,
  45. payload = data,
  46. }
  47. else
  48. error(handler)
  49. end
  50. end
  51.  
  52. function sendRequest(token,method,POST)
  53. local data = ""
  54. local url = "https://api.telegram.org/bot" .. token .. "/" .. method
  55. data = request(getUrlForProxyificate(url),POST)
  56. --print(serialization.serialize(data.headers))
  57. return json.decode(data.payload)
  58. end
  59.  
  60. local APIMetatable = {}
  61. APIMetatable.__index = function(self,key)
  62. return function(post)
  63. return Promise(function(resolve,reject)
  64. local res = sendRequest(self.token,key,post)
  65. if res.ok == true then
  66. resolve(res.result)
  67. else
  68. reject(res.error_message)
  69. end
  70. end)
  71. end
  72. end
  73.  
  74. function Bot(token)
  75. local bot = {}
  76. bot.API = {token=token}
  77. setmetatable(bot.API,APIMetatable)
  78. function bot.API.rawRequest(method,post)
  79. return sendRequest(bot.API.token,method,post)
  80. end
  81. bot.longpoll = {callbacks={}}
  82. 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
  83. bot.longpoll.callbacks[event] = bot.longpoll.callbacks[event] or {}
  84. table.insert(bot.longpoll.callbacks[event],callback)
  85. end
  86. function bot.longpoll.start()
  87. bot.longpoll.active = true
  88. local errorCounter = 0
  89. while bot.longpoll.active do
  90. os.sleep(0)
  91. local success, updates = pcall(sendRequest,token,"getUpdates",{
  92. offset = bot.longpoll.offset,
  93. --timeout = 1,
  94. })
  95. if success then
  96. errorCounter = 0
  97. if updates.ok then
  98. local result = updates.result
  99. for i = 1, #result do
  100. local update = result[i]
  101. for key, value in pairs(update) do
  102. local lngp = bot.longpoll.callbacks[key]
  103. if lngp then
  104. for i = 1, #lngp do
  105. lngp[i](update[key])
  106. end
  107. end
  108. end
  109. end
  110. if #result > 0 then
  111. bot.longpoll.offset = result[#result].update_id+1
  112. end
  113. else
  114. print("error: ",updates.error_message)
  115. end
  116. else
  117. if errorCounter > 3 then
  118. bot.longpoll.stop()
  119. print("Exiting: too many errors")
  120. error(updates)
  121. end
  122. errorCounter = errorCounter + 1
  123. print("internal error: ", updates, errorCounter)
  124. end
  125. end
  126. end
  127. function bot.longpoll.stop()
  128. bot.longpoll.active = false
  129. end
  130. return bot
  131. end
  132. return Bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement