scusette.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package.path = package.path .. ";/<include_path>/?.lua"
  2. driver = require('luasql.sqlite3')
  3. BOT_TOKEN = '<bot_token>'
  4. API_URL = 'https://api.telegram.org/bot'..BOT_TOKEN..'/'
  5. CHANNEL = '<channel_id>'
  6. ADMINS_CHAT = {<admin_chat>}
  7. ADMINS_ID = {<admin_id>}
  8. HTDOCS = '<path>/htdocs/'
  9. SYNC = '<path>/htdocs/scusette/'
  10. APP = '<path>/app/scusette/'
  11. DB = APP..'db/scusette.sqlite3'
  12. URL = 'https://scusette.it/'
  13. env = assert(driver.sqlite3())
  14. con = assert(env:connect(DB))
  15. ingiurie = { "Coglione!",
  16. "Oh ma ce la fai?",
  17. "Ti ripigli?",
  18. "Ma quanto sei ritardato?",
  19. "Sei proprio un @w00tw00t",
  20. "Messaggio ricevuto, sei ritardato",
  21. "Per caso lavori in Accenture?",
  22. "Senior Manager in Spike Reply vero?",
  23. "http://www.gtfo.org",
  24. "Hai l'aria di uno che fa Big Data per KPMG",
  25. "Mandato il CV a Deloitte?"
  26. }
  27. help = [[
  28. <strong>Scusette.it</strong> bot
  29. Per ricevere una scusetta random
  30. <pre>
  31. /random
  32. </pre>
  33. Per cercare una scusetta
  34. <pre>
  35. /query
  36. keyword
  37. </pre>
  38. Per inviare una scusetta invia un messaggio col seguente formato:
  39. <pre>
  40. /invia
  41. titolo scusetta
  42. corpo scusetta
  43. </pre>
  44. <em>Linee guida</em>:
  45. * Utilizza grammatica e capitalizzazione corrette
  46. * Il titolo e' il fatto e il corpo e' la scusetta in prima persona, prendi spunto da quelle gia' pubblicate
  47. * Valuta se anche se scontestualizzata la scusetta e' divertente lo stesso
  48. * La scusetta deve essere stata usata
  49. * Le scusette saranno pubblicate solo previa approvazione di un admin
  50. * Sta calmo e non piangere pls
  51. ]]
  52. function rows(connection, sql_statement)
  53. local cursor = assert(connection:execute(sql_statement))
  54. return function()
  55. return cursor:fetch()
  56. end
  57. end
  58. function generate(con)
  59. scusette = ""
  60. n = con:execute("select count(id) from scusette"):fetch()
  61. for id, slug, title, body, author, date in rows(con, "select id, slug, title, body, author, date from scusette where approved order by id desc") do
  62. scusette = scusette.."\t\t<hr><h3 id=\""..slug.."\"><a href=\""..URL.."#"..slug.."\">"..title.."</a></h3>\n\t\t<aside><small>"..date..", scusettebot</small></aside>\n\t\t<p>"..body.."</p>\n"
  63. end
  64. file = io.open(APP.."template/header.html", "r")
  65. io.input(file)
  66. header = io.read("*all")
  67. header = header:gsub("#scusettenumber#", tostring(n))
  68. io.close(file)
  69. file = io.open(APP.."template/footer.html", "r")
  70. io.input(file)
  71. footer = io.read("*all")
  72. io.input(file)
  73. file = io.open(HTDOCS.."index.html", "w")
  74. io.output(file)
  75. io.write(header..scusette..footer)
  76. io.close(file)
  77. file = io.open(SYNC.."scusette.html", "w")
  78. io.output(file)
  79. io.write(header..scusette..footer)
  80. io.close(file)
  81. end
  82. function is_admin(id)
  83. for _, admin_id in ipairs(ADMINS_ID) do
  84. if admin_id == id then
  85. return true
  86. else
  87. return false
  88. end
  89. end
  90. end
  91. function reply (chatid, reply)
  92. local reply = ngx.escape_uri(reply)
  93. local httpc = require("resty.http").new()
  94. local res, err = httpc:request_uri(API_URL.."sendmessage", {
  95. method = "POST",
  96. body = "chat_id="..chatid.."&text="..reply.."&parse_mode=HTML",
  97. headers = {
  98. ["Content-Type"] = "application/x-www-form-urlencoded",
  99. },
  100. })
  101. ngx.say(err)
  102. end
  103. ngx.req.read_body()
  104. content = ngx.req.get_body_data()
  105. if not content then
  106. ngx.exit(ngx.HTTP_BAD_REQUEST)
  107. end
  108. local json_local = require "json"
  109. update = json_local.decode(content)
  110. if not update["message"] or not update["message"]["chat"] or not update["message"]["from"] or not update["message"]["text"] then
  111. ngx.exit(ngx.HTTP_BAD_REQUEST)
  112. end
  113. chatid = update["message"]["chat"]["id"]
  114. fromid = update["message"]["from"]["id"]
  115. fromusername = update["message"]["from"]["username"];
  116. message = update["message"]["text"]
  117. lines = {}
  118. for l in message:gmatch("[^\n]+") do
  119. table.insert(lines, l)
  120. end
  121. command = lines[1]
  122. if command == "/start" then
  123. resp = help
  124. elseif command == "/invia" then
  125. title = lines[2]
  126. body = lines[3]
  127. if title and body then
  128. slug = lines[2]:gsub('%W','-'):lower()
  129. author = fromid
  130. r, e = con:execute(string.format("insert into scusette (slug, title, body, author, date, approved, rejected) values ('%s', '%s', '%s', '%s', CURRENT_TIMESTAMP, 0, 0)", con:escape(slug), con:escape(title), con:escape(body), con:escape(author)))
  131. if r then
  132. resp = "Scusetta inviata, se verra' approvata riceverai una notifica e la vedrai pubblicata!"
  133. else
  134. resp = "Qualcosa e' andato storto, contatta un admin"
  135. end
  136. for _, admin_chat_id in ipairs(ADMINS_CHAT) do
  137. id = con:getlastautoid()
  138. scusetta = string.format("<em>%s</em> - <pre>%s</pre> by @%s\n<em>%s</em>\n%s\n\n", id, slug, author, title, body)
  139. reply(admin_chat_id, "Nuova scusetta in coda, controlla con /list\n\n"..scusetta)
  140. end
  141. else
  142. resp = "Scusetta non valida, controlla la sintassi con /start"
  143. end
  144. elseif command == "/list" then
  145. if is_admin(fromid) then
  146. list = ""
  147. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where not approved and not rejected") do
  148. list = list..string.format("<strong>%s</strong> by @%s\n<em>%s</em>\n%s\n\n", id, author, title, body)
  149. resp = list
  150. end
  151. end
  152. elseif command == "/approve" then
  153. id = lines[2]
  154. if is_admin(fromid) and id then
  155. con:execute(string.format("update scusette set approved = 1 where id = '%s'", con:escape(tonumber(id))))
  156. generate(con)
  157. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where id = "..con:escape(tonumber(id))) do
  158. scusetta = string.format("<strong>%s</strong>\n%s\n\n%s/#%s", title, body, URL, slug)
  159. reply(author, "Scusetta approvata!")
  160. reply(CHANNEL, scusetta);
  161. end
  162. resp = "Operazione eseguita"
  163. else
  164. resp = "Nope"
  165. end
  166. elseif command == "/delete" then
  167. id = lines[2]
  168. if is_admin(fromid) and id then
  169. con:execute(string.format("update scusette set rejected = 1 where id = '%s'", con:escape(tonumber(id))))
  170. resp = "Scusetta eliminata"
  171. else
  172. resp = "Nope"
  173. end
  174. elseif command == "/rebuild" then
  175. if is_admin(fromid) then
  176. generate(con)
  177. end
  178. elseif command == "/random" then
  179. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette order by random() limit 1") do
  180. resp = string.format("<i><em>%s</em></i>\n%s\n\nhttps://scusette.it/#%s", title, body, slug)
  181. end
  182. elseif command == "/query" then
  183. query = lines[2]
  184. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where title like '%"..con:escape(query).."%' or '%"..con:escape(query).."%' order by random() limit 1") do
  185. resp = string.format("<i><em>%s</em></i>\n%s\n\nhttps://scusette.it/#%s", title, body, slug)
  186. end
  187. else
  188. if is_admin(fromid) then
  189. resp = "A rapporto!"
  190. else
  191. resp = ingiurie[math.random(#ingiurie)]
  192. end
  193. end
  194. con:commit()
  195. con:close()
  196. env:close()
  197. reply(chatid, resp)