scusette.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. --stmt = con:prepare("inserto into scusette (slug, title, body, author, approved, rejected) values(:slug, :title, :body, :author, 0, 0)")
  131. --stmt:bind({slug=slug, title=title, body=body, author=author}):exec()
  132. r, e = con:execute("insert into scusette (slug, title, body, author, date, approved, rejected) values ('"..con:escape(slug).."', '"..con:escape(title).."', '"..con:escape(body).."', '"..con:escape(author).."', CURRENT_TIMESTAMP, 0, 0)")
  133. if r then
  134. resp = "Scusetta inviata, se verra' approvata riceverai una notifica e la vedrai pubblicata!"
  135. else
  136. resp = "Qualcosa e' andato storto, contatta un admin"
  137. end
  138. for _, admin_chat_id in ipairs(ADMINS_CHAT) do
  139. id = con:getlastautoid()
  140. scusetta = string.format("<em>%s</em> - <pre>%s</pre> by @%s\n<em>%s</em>\n%s\n\n", id, slug, author, title, body)
  141. reply(admin_chat_id, "Nuova scusetta in coda, controlla con /list\n\n"..scusetta)
  142. end
  143. else
  144. resp = "Scusetta non valida, controlla la sintassi con /start"
  145. end
  146. elseif command == "/list" then
  147. if is_admin(fromid) then
  148. list = ""
  149. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where not approved and not rejected") do
  150. list = list..string.format("<strong>%s</strong> by @%s\n<em>%s</em>\n%s\n\n", id, author, title, body)
  151. resp = list
  152. end
  153. end
  154. --reply(chatid, "NON sei admin! "..fromid.." value: "..tostring(test).."0: "..tostring(type(ADMINS_ID[1])).." 1: "..tostring(type(fromid)))
  155. elseif command == "/approve" then
  156. id = lines[2]
  157. if is_admin(fromid) and id then
  158. con:execute("update scusette set approved = 1 where id = "..con:escape(tonumber(id)))
  159. generate(con)
  160. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where id = "..con:escape(tonumber(id))) do
  161. scusetta = "<strong>"..title.."</strong>\n"..body.."\n\n"..URL.."/#"..slug
  162. reply(author, "Scusetta approvata!")
  163. -- reply(CHANNEL, scusetta);
  164. end
  165. resp = "Operazione eseguita"
  166. else
  167. resp = "Nope"
  168. end
  169. elseif command == "/delete" then
  170. id = lines[2]
  171. if is_admin(fromid) and id then
  172. con:execute("update scusette set rejected = 1 where id = "..con:escape(tonumber(id)))
  173. resp = "Scusetta eliminata"
  174. else
  175. resp = "Nope"
  176. end
  177. elseif command == "/rebuild" then
  178. if is_admin(fromid) then
  179. generate(con)
  180. end
  181. elseif command == "/random" then
  182. for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette order by random() limit 1") do
  183. resp = string.format("<i><em>"..title.."</em></i>\n"..body.."\n\nhttps://scusette.it/#"..slug)
  184. end
  185. elseif command == "/query" then
  186. query = lines[2]
  187. 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
  188. resp = string.format("<i><em>"..title.."</em></i>\n"..body.."\n\nhttps://scusette.it/"..slug)
  189. end
  190. else
  191. if is_admin(fromid) then
  192. resp = "A rapporto!"
  193. else
  194. resp = ingiurie[math.random(#ingiurie)]
  195. end
  196. end
  197. con:commit()
  198. con:close()
  199. env:close()
  200. reply(chatid, resp)