commit 156ffd5e89de31d1d972c5c6776d88bf26005e11 Author: Giulio Date: Sun May 30 22:09:07 2021 +0200 Added old php bot diff --git a/old/scusette.php b/old/scusette.php new file mode 100644 index 0000000..63f0753 --- /dev/null +++ b/old/scusette.php @@ -0,0 +1,154 @@ +'); +define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); +define('CHANNEL', ''); +define('ADMIN_CHAT', ''); +define('ADMIN_ID', ''); + +$mysql_user = ''; +$mysql_pass = ''; +$mysql_db = 'scusettebot'; + +$db = new PDO('mysql:host=127.0.0.1;dbname=scusettebot;charset=utf8mb4', $mysql_user, $mysql_pass); + +$ingiurie = array( "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 reply($chatid, $reply) { + return file_get_contents(API_URL."sendmessage?chat_id=".$chatid."&text=".urlencode($reply)."&parse_mode=HTML"); +} + +$content = file_get_contents("php://input"); +//file_put_contents('/tmp/debug.txt', $content); +$update = json_decode($content, true); +$chatid = $update["message"]["chat"]["id"]; + +$fromid = $update["message"]["from"]["id"]; +$fromusername = $update["message"]["from"]["username"]; +$command = explode("\n", $update['message']['text'])[0]; + +switch($command) { + case '/start': + $reply = $help; + break; + case '/invia': + $exp = explode("\n", $update['message']['text']); + $title = $exp[1]; + $body = $exp[2]; + $stmt = $db->prepare("INSERT INTO submissions (title, body, fromid, fromusername, timestamp) VALUES (?, ?, ?, ?, ?)"); + $stmt->execute(array($title, $body, $fromid, $fromusername, time())); + reply(ADMIN_CHAT, "Nuova scusetta in coda, controlla con /list"); + $reply = "Scusetta inviata, se verra' approvata la vedrai pubblicata su scusette.it"; + break; + case '/list': + if ($fromid === ADMIN_ID) { + $reply = ""; + $stmt = $db->prepare("SELECT * FROM submissions"); + $stmt->execute(); + $scusette = $stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($scusette as $scusetta) { + $reply = $reply."".$scusetta['id']." by @".$scusetta['fromusername']."\n".$scusetta['title']."\n".$scusetta['body']."\n\n"; + } + } else { + $reply = "Non ho capito"; + } + break; + case '/approve': + if ($fromid === ADMIN_ID) { + $exp = explode("\n", $update['message']['text']); + $id = $exp[1]; + $stmt = $db->prepare("SELECT * FROM submissions WHERE id = ?"); + $stmt->execute(array($id)); + $scusetta = $stmt->fetch(PDO::FETCH_ASSOC); + $slug = strtolower(preg_replace('/[^\p{L}0-9]+/u', '-', trim($scusetta['title']))); + $stmt = $db->prepare("INSERT INTO anchor.anchor_posts (title, slug, markdown, html, created, updated, author, category, status) VALUES (?, ?, ?, ?, now(), now(), ?, ?, 'published')"); + $stmt->execute(array($scusetta['title'], $slug, htmlentities($scusetta['body']), "

".htmlentities($scusetta['body'])."

", 6, 1)); + $stmt = $db->prepare("DELETE FROM submissions WHERE id = ?"); + $stmt->execute(array($id)); + reply(CHANNEL, html_entity_decode("".htmlentities($scusetta["title"])."\n".htmlentities($scusetta["body"]))); + $reply = "https://scusette.it/blog/index.php/posts/".$slug; + + } else { + $reply = "Non ho capito"; + } + + break; + case '/delete': + if ($fromid === ADMIN_ID) { + $exp = explode("\n", $update['message']['text']); + $id = $exp[1]; + $stmt = $db->prepare("DELETE FROM submissions WHERE id = ?"); + $stmt->execute(array($id)); + $reply = "Scusetta ".$id." eliminata"; + } else { + $reply = "Non ho capito"; + } + break; + case '/random': + $stmt = $db->prepare("SELECT title, markdown FROM anchor.anchor_posts ORDER BY RAND() LIMIT 1"); + $stmt->execute(); + $scusetta = $stmt->fetch(PDO::FETCH_ASSOC); + $reply = html_entity_decode("".$scusetta["title"]."\n".$scusetta["markdown"]); + break; + case '/query': + $search = "%".str_replace('%', '', explode("\n", $update['message']['text'])[1])."%"; + $stmt = $db->prepare("SELECT title, markdown FROM anchor.anchor_posts WHERE markdown LIKE ? OR title LIKE ? ORDER BY RAND() LIMIT 1"); + $stmt->execute(array($search, $search)); + $scusetta = $stmt->fetch(PDO::FETCH_ASSOC); + if (!empty($scusetta)) { + $reply = html_entity_decode("".$scusetta["title"]."\n".$scusetta["markdown"]); + } else { + $reply = "Nope!"; + } + + break; + default: + if ($fromid === ADMIN_ID) { + $reply = "Ogni tuo desiderio e' un ordine padrone"; + } else { + $reply = $ingiurie[array_rand($ingiurie, 1)]; + } + //$reply = "Non ho capito"; + break; +} + +reply($chatid, $reply); + +?>