123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package.path = package.path .. ";/<include_path>/?.lua"
- driver = require('luasql.sqlite3')
- BOT_TOKEN = '<bot_token>'
- API_URL = 'https://api.telegram.org/bot'..BOT_TOKEN..'/'
- CHANNEL = '<channel_id>'
- ADMINS_CHAT = {<admin_chat>}
- ADMINS_ID = {<admin_id>}
- HTDOCS = '<path>/htdocs/'
- SYNC = '<path>/htdocs/scusette/'
- APP = '<path>/app/scusette/'
- DB = APP..'db/scusette.sqlite3'
- URL = 'https://scusette.it/'
- env = assert(driver.sqlite3())
- con = assert(env:connect(DB))
- ingiurie = { "Coglione!",
- "Oh ma ce la fai?",
- "Ti ripigli?",
- "Ma quanto sei ritardato?",
- "Sei proprio un @w00tw00t",
- "Messaggio ricevuto, sei ritardato",
- "Per caso lavori in Accenture?",
- "Senior Manager in Spike Reply vero?",
- "http://www.gtfo.org",
- "Hai l'aria di uno che fa Big Data per KPMG",
- "Mandato il CV a Deloitte?"
- }
- help = [[
- <strong>Scusette.it</strong> bot
- Per ricevere una scusetta random
- <pre>
- /random
- </pre>
- Per cercare una scusetta
- <pre>
- /query
- keyword
- </pre>
- Per inviare una scusetta invia un messaggio col seguente formato:
- <pre>
- /invia
- titolo scusetta
- corpo scusetta
- </pre>
- <em>Linee guida</em>:
- * Utilizza grammatica e capitalizzazione corrette
- * Il titolo e' il fatto e il corpo e' la scusetta in prima persona, prendi spunto da quelle gia' pubblicate
- * Valuta se anche se scontestualizzata la scusetta e' divertente lo stesso
- * La scusetta deve essere stata usata
- * Le scusette saranno pubblicate solo previa approvazione di un admin
- * Sta calmo e non piangere pls
- ]]
- function rows(connection, sql_statement)
- local cursor = assert(connection:execute(sql_statement))
- return function()
- return cursor:fetch()
- end
- end
- function generate(con)
- scusette = ""
- n = con:execute("select count(id) from scusette"):fetch()
- 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
- 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"
- end
- file = io.open(APP.."template/header.html", "r")
- io.input(file)
- header = io.read("*all")
- header = header:gsub("#scusettenumber#", tostring(n))
- io.close(file)
- file = io.open(APP.."template/footer.html", "r")
- io.input(file)
- footer = io.read("*all")
- io.input(file)
- file = io.open(HTDOCS.."index.html", "w")
- io.output(file)
- io.write(header..scusette..footer)
- io.close(file)
- file = io.open(SYNC.."scusette.html", "w")
- io.output(file)
- io.write(header..scusette..footer)
- io.close(file)
- end
- function is_admin(id)
- for _, admin_id in ipairs(ADMINS_ID) do
- if admin_id == id then
- return true
- else
- return false
- end
- end
- end
- function reply (chatid, reply)
- local reply = ngx.escape_uri(reply)
- local httpc = require("resty.http").new()
- local res, err = httpc:request_uri(API_URL.."sendmessage", {
- method = "POST",
- body = "chat_id="..chatid.."&text="..reply.."&parse_mode=HTML",
- headers = {
- ["Content-Type"] = "application/x-www-form-urlencoded",
- },
- })
- ngx.say(err)
- end
- ngx.req.read_body()
- content = ngx.req.get_body_data()
- if not content then
- ngx.exit(ngx.HTTP_BAD_REQUEST)
- end
- local json_local = require "json"
- update = json_local.decode(content)
- if not update["message"] or not update["message"]["chat"] or not update["message"]["from"] or not update["message"]["text"] then
- ngx.exit(ngx.HTTP_BAD_REQUEST)
- end
-
- chatid = update["message"]["chat"]["id"]
- fromid = update["message"]["from"]["id"]
- fromusername = update["message"]["from"]["username"];
- message = update["message"]["text"]
- lines = {}
- for l in message:gmatch("[^\n]+") do
- table.insert(lines, l)
- end
- command = lines[1]
- if command == "/start" then
- resp = help
- elseif command == "/invia" then
- title = lines[2]
- body = lines[3]
- if title and body then
- slug = lines[2]:gsub('%W','-'):lower()
- author = fromid
- --stmt = con:prepare("inserto into scusette (slug, title, body, author, approved, rejected) values(:slug, :title, :body, :author, 0, 0)")
- --stmt:bind({slug=slug, title=title, body=body, author=author}):exec()
- 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)")
- if r then
- resp = "Scusetta inviata, se verra' approvata riceverai una notifica e la vedrai pubblicata!"
- else
- resp = "Qualcosa e' andato storto, contatta un admin"
- end
- for _, admin_chat_id in ipairs(ADMINS_CHAT) do
- id = con:getlastautoid()
- scusetta = string.format("<em>%s</em> - <pre>%s</pre> by @%s\n<em>%s</em>\n%s\n\n", id, slug, author, title, body)
- reply(admin_chat_id, "Nuova scusetta in coda, controlla con /list\n\n"..scusetta)
- end
- else
- resp = "Scusetta non valida, controlla la sintassi con /start"
- end
- elseif command == "/list" then
- if is_admin(fromid) then
- list = ""
- for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where not approved and not rejected") do
- list = list..string.format("<strong>%s</strong> by @%s\n<em>%s</em>\n%s\n\n", id, author, title, body)
- resp = list
- end
- end
- --reply(chatid, "NON sei admin! "..fromid.." value: "..tostring(test).."0: "..tostring(type(ADMINS_ID[1])).." 1: "..tostring(type(fromid)))
- elseif command == "/approve" then
- id = lines[2]
- if is_admin(fromid) and id then
- con:execute("update scusette set approved = 1 where id = "..con:escape(tonumber(id)))
- generate(con)
- for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette where id = "..con:escape(tonumber(id))) do
- scusetta = "<strong>"..title.."</strong>\n"..body.."\n\n"..URL.."/#"..slug
- reply(author, "Scusetta approvata!")
- -- reply(CHANNEL, scusetta);
- end
- resp = "Operazione eseguita"
- else
- resp = "Nope"
- end
- elseif command == "/delete" then
- id = lines[2]
- if is_admin(fromid) and id then
- con:execute("update scusette set rejected = 1 where id = "..con:escape(tonumber(id)))
- resp = "Scusetta eliminata"
- else
- resp = "Nope"
- end
- elseif command == "/rebuild" then
- if is_admin(fromid) then
- generate(con)
- end
- elseif command == "/random" then
- for id, slug, title, body, author in rows (con, "select id, slug, title, body, author from scusette order by random() limit 1") do
- resp = string.format("<i><em>"..title.."</em></i>\n"..body.."\n\nhttps://scusette.it/#"..slug)
- end
- elseif command == "/query" then
- query = lines[2]
- 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
- resp = string.format("<i><em>"..title.."</em></i>\n"..body.."\n\nhttps://scusette.it/"..slug)
- end
- else
- if is_admin(fromid) then
- resp = "A rapporto!"
- else
- resp = ingiurie[math.random(#ingiurie)]
- end
- end
- con:commit()
- con:close()
- env:close()
- reply(chatid, resp)
|