package.path = package.path .. ";//?.lua" driver = require('luasql.sqlite3') BOT_TOKEN = '' API_URL = 'https://api.telegram.org/bot'..BOT_TOKEN..'/' CHANNEL = '' ADMINS_CHAT = {} ADMINS_ID = {} HTDOCS = '/htdocs/' SYNC = '/htdocs/scusette/' APP = '/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 = [[ Scusette.it bot Per ricevere una scusetta random
/random
Per cercare una scusetta
/query
keyword
Per inviare una scusetta invia un messaggio col seguente formato:
/invia
titolo scusetta
corpo scusetta
Linee guida: * 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

"..title.."

\n\t\t\n\t\t

"..body.."

\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 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))) 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("%s -
%s
by @%s\n%s\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("%s by @%s\n%s\n%s\n\n", id, author, title, body) resp = list end end elseif command == "/approve" then id = lines[2] if is_admin(fromid) and id then con:execute(string.format("update scusette set approved = 1 where id = '%s'", 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 = string.format("%s\n%s\n\n%s/#%s", title, body, 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(string.format("update scusette set rejected = 1 where id = '%s'", 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("%s\n%s\n\nhttps://scusette.it/#%s", title, body, 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("%s\n%s\n\nhttps://scusette.it/#%s", title, body, 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)