scusette.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. define('BOT_TOKEN', '<bot_token>');
  3. define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
  4. define('CHANNEL', '<channel_id>');
  5. define('ADMIN_CHAT', '<admin_chat>');
  6. define('ADMIN_ID', '<admin_id>');
  7. $mysql_user = '<mysql_user>';
  8. $mysql_pass = '<mysql_pass>';
  9. $mysql_db = 'scusettebot';
  10. $db = new PDO('mysql:host=127.0.0.1;dbname=scusettebot;charset=utf8mb4', $mysql_user, $mysql_pass);
  11. $ingiurie = array( "Coglione!",
  12. "Oh ma ce la fai?",
  13. "Ti ripigli?",
  14. "Ma quanto sei ritardato?",
  15. "Sei proprio un @w00tw00t",
  16. "Messaggio ricevuto, sei ritardato",
  17. "Per caso lavori in Accenture?",
  18. "Senior Manager in Spike Reply vero?",
  19. "http://www.gtfo.org",
  20. "Hai l'aria di uno che fa Big Data per KPMG",
  21. "Mandato il CV a Deloitte?"
  22. );
  23. $help = "
  24. <strong>Scusette.it</strong> bot
  25. Per ricevere una scusetta random
  26. <pre>
  27. /random
  28. </pre>
  29. Per cercare una scusetta
  30. <pre>
  31. /query
  32. keyword
  33. </pre>
  34. Per inviare una scusetta invia un messaggio col seguente formato:
  35. <pre>
  36. /invia
  37. titolo scusetta
  38. corpo scusetta
  39. </pre>
  40. <em>Linee guida</em>:
  41. * Utilizza grammatica e capitalizzazione corrette
  42. * Il titolo e' il fatto e il corpo e' la scusetta in prima persona, prendi spunto da quelle gia' pubblicate
  43. * Valuta se anche se scontestualizzata la scusetta e' divertente lo stesso
  44. * La scusetta deve essere stata usata
  45. * Le scusette saranno pubblicate solo previa approvazione di un admin
  46. * Sta calmo e non piangere pls
  47. ";
  48. function reply($chatid, $reply) {
  49. return file_get_contents(API_URL."sendmessage?chat_id=".$chatid."&text=".urlencode($reply)."&parse_mode=HTML");
  50. }
  51. $content = file_get_contents("php://input");
  52. //file_put_contents('/tmp/debug.txt', $content);
  53. $update = json_decode($content, true);
  54. $chatid = $update["message"]["chat"]["id"];
  55. $fromid = $update["message"]["from"]["id"];
  56. $fromusername = $update["message"]["from"]["username"];
  57. $command = explode("\n", $update['message']['text'])[0];
  58. switch($command) {
  59. case '/start':
  60. $reply = $help;
  61. break;
  62. case '/invia':
  63. $exp = explode("\n", $update['message']['text']);
  64. $title = $exp[1];
  65. $body = $exp[2];
  66. $stmt = $db->prepare("INSERT INTO submissions (title, body, fromid, fromusername, timestamp) VALUES (?, ?, ?, ?, ?)");
  67. $stmt->execute(array($title, $body, $fromid, $fromusername, time()));
  68. reply(ADMIN_CHAT, "Nuova scusetta in coda, controlla con /list");
  69. $reply = "Scusetta inviata, se verra' approvata la vedrai pubblicata su scusette.it";
  70. break;
  71. case '/list':
  72. if ($fromid === ADMIN_ID) {
  73. $reply = "";
  74. $stmt = $db->prepare("SELECT * FROM submissions");
  75. $stmt->execute();
  76. $scusette = $stmt->fetchAll(PDO::FETCH_ASSOC);
  77. foreach ($scusette as $scusetta) {
  78. $reply = $reply."<strong>".$scusetta['id']."</strong> by @".$scusetta['fromusername']."\n<em>".$scusetta['title']."</em>\n".$scusetta['body']."\n\n";
  79. }
  80. } else {
  81. $reply = "Non ho capito";
  82. }
  83. break;
  84. case '/approve':
  85. if ($fromid === ADMIN_ID) {
  86. $exp = explode("\n", $update['message']['text']);
  87. $id = $exp[1];
  88. $stmt = $db->prepare("SELECT * FROM submissions WHERE id = ?");
  89. $stmt->execute(array($id));
  90. $scusetta = $stmt->fetch(PDO::FETCH_ASSOC);
  91. $slug = strtolower(preg_replace('/[^\p{L}0-9]+/u', '-', trim($scusetta['title'])));
  92. $stmt = $db->prepare("INSERT INTO anchor.anchor_posts (title, slug, markdown, html, created, updated, author, category, status) VALUES (?, ?, ?, ?, now(), now(), ?, ?, 'published')");
  93. $stmt->execute(array($scusetta['title'], $slug, htmlentities($scusetta['body']), "<p>".htmlentities($scusetta['body'])."</p>", 6, 1));
  94. $stmt = $db->prepare("DELETE FROM submissions WHERE id = ?");
  95. $stmt->execute(array($id));
  96. reply(CHANNEL, html_entity_decode("<strong>".htmlentities($scusetta["title"])."</strong>\n".htmlentities($scusetta["body"])));
  97. $reply = "https://scusette.it/blog/index.php/posts/".$slug;
  98. } else {
  99. $reply = "Non ho capito";
  100. }
  101. break;
  102. case '/delete':
  103. if ($fromid === ADMIN_ID) {
  104. $exp = explode("\n", $update['message']['text']);
  105. $id = $exp[1];
  106. $stmt = $db->prepare("DELETE FROM submissions WHERE id = ?");
  107. $stmt->execute(array($id));
  108. $reply = "Scusetta ".$id." eliminata";
  109. } else {
  110. $reply = "Non ho capito";
  111. }
  112. break;
  113. case '/random':
  114. $stmt = $db->prepare("SELECT title, markdown FROM anchor.anchor_posts ORDER BY RAND() LIMIT 1");
  115. $stmt->execute();
  116. $scusetta = $stmt->fetch(PDO::FETCH_ASSOC);
  117. $reply = html_entity_decode("<strong>".$scusetta["title"]."</strong>\n".$scusetta["markdown"]);
  118. break;
  119. case '/query':
  120. $search = "%".str_replace('%', '', explode("\n", $update['message']['text'])[1])."%";
  121. $stmt = $db->prepare("SELECT title, markdown FROM anchor.anchor_posts WHERE markdown LIKE ? OR title LIKE ? ORDER BY RAND() LIMIT 1");
  122. $stmt->execute(array($search, $search));
  123. $scusetta = $stmt->fetch(PDO::FETCH_ASSOC);
  124. if (!empty($scusetta)) {
  125. $reply = html_entity_decode("<strong>".$scusetta["title"]."</strong>\n".$scusetta["markdown"]);
  126. } else {
  127. $reply = "Nope!";
  128. }
  129. break;
  130. default:
  131. if ($fromid === ADMIN_ID) {
  132. $reply = "Ogni tuo desiderio e' un ordine padrone";
  133. } else {
  134. $reply = $ingiurie[array_rand($ingiurie, 1)];
  135. }
  136. //$reply = "Non ho capito";
  137. break;
  138. }
  139. reply($chatid, $reply);
  140. ?>