Advertisement
DreamWolf

WolfTG

Dec 25th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. -- WolfTG.lua (библиотека)
  2. local component = require("component")
  3. local term = require("term")
  4. local internet = require("internet")
  5. local os = require("os")
  6. local JSON = require("json")
  7.  
  8. -- Таблица для хранения состояния
  9. local WolfTG = {}
  10.  
  11. -- Функция для инициализации бота
  12. function WolfTG.init(telegramToken, chatId)
  13. WolfTG.telegramToken = telegramToken
  14. WolfTG.chatId = chatId
  15. WolfTG.lastUpdateId = 0
  16. WolfTG.messageList = {}
  17. WolfTG.exitProgram = false
  18. end
  19.  
  20. -- Функция для получения нового сообщения и добавления в переменную
  21. function WolfTG.wolftg_receive(variableName)
  22. local url = string.format(
  23. "https://api.telegram.org/bot%s/getUpdates?offset=%d&timeout=60&allowed_updates=[\"message\"]",
  24. WolfTG.telegramToken, WolfTG.lastUpdateId + 1
  25. )
  26.  
  27. local result, response = pcall(function()
  28. local handle = internet.request(url)
  29. if not handle then
  30. return nil
  31. end
  32. local responseData = ""
  33. for chunk in handle do
  34. responseData = responseData .. chunk
  35. end
  36. return responseData
  37. end)
  38.  
  39. if not result or not response then
  40. return nil
  41. end
  42.  
  43. local decoded, err = JSON:decode(response)
  44. if err then
  45. return nil
  46. end
  47.  
  48. if decoded and decoded.ok and decoded.result then
  49. for _, update in ipairs(decoded.result) do
  50. if update.update_id > WolfTG.lastUpdateId then
  51. WolfTG.lastUpdateId = update.update_id
  52. if update.message.text then
  53. -- Добавляем сообщение в переданную переменную
  54. _G[variableName] = update.message.text
  55. end
  56. end
  57. end
  58. end
  59. end
  60.  
  61. -- Функция для отправки сообщения в Telegram с использованием параметров в URL
  62. function WolfTG.wolftg_receiving(variableName)
  63. local message = _G[variableName]
  64. if not message then
  65. return "Сообщение пустое!"
  66. end
  67.  
  68. -- Формируем правильный URL для отправки сообщения
  69. local url = string.format(
  70. "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
  71. WolfTG.telegramToken,
  72. WolfTG.chatId,
  73. internet.urlencode(message) -- Кодируем сообщение для URL
  74. )
  75.  
  76. -- Отправка GET-запроса
  77. local result, response = pcall(function()
  78. local handle = internet.request(url)
  79. if not handle then
  80. return nil
  81. end
  82. local responseData = ""
  83. for chunk in handle do
  84. responseData = responseData .. chunk
  85. end
  86. return responseData
  87. end)
  88.  
  89. if not result or not response then
  90. return "Ошибка отправки: " .. (response or "Неизвестная ошибка")
  91. end
  92.  
  93. -- Печатаем ответ от Telegram
  94. return response
  95. end
  96.  
  97. -- Функция для получения chat_id
  98. function WolfTG.wolftg_Registerid()
  99. return WolfTG.chatId
  100. end
  101.  
  102. -- Функция для получения API токена
  103. function WolfTG.wolftg_registerapi()
  104. return WolfTG.telegramToken
  105. end
  106.  
  107. -- Возвращаем библиотеку
  108. return WolfTG
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement