155 lignes
		
	
	
		
			5.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			155 lignes
		
	
	
		
			5.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
define('BOT_TOKEN', '<bot_token>');
 | 
						|
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
 | 
						|
define('CHANNEL', '<channel_id>');
 | 
						|
define('ADMIN_CHAT', '<admin_chat>');
 | 
						|
define('ADMIN_ID', '<admin_id>');
 | 
						|
 | 
						|
$mysql_user	= '<mysql_user>';
 | 
						|
$mysql_pass	= '<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 = "
 | 
						|
<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 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."<strong>".$scusetta['id']."</strong> by @".$scusetta['fromusername']."\n<em>".$scusetta['title']."</em>\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']), "<p>".htmlentities($scusetta['body'])."</p>", 6, 1));
 | 
						|
			$stmt = $db->prepare("DELETE FROM submissions WHERE id = ?");
 | 
						|
                        $stmt->execute(array($id));
 | 
						|
			reply(CHANNEL, html_entity_decode("<strong>".htmlentities($scusetta["title"])."</strong>\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("<strong>".$scusetta["title"]."</strong>\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("<strong>".$scusetta["title"]."</strong>\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);
 | 
						|
	
 | 
						|
?>
 |