certalert.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. define('BOT_TOKEN', '');
  3. define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
  4. $mysql_user = '';
  5. $mysql_pass = '';
  6. $db = new PDO('mysql:host=127.0.0.1;dbname=certalertbot;charset=utf8mb4', $mysql_user, $mysql_pass);
  7. $help = "
  8. <strong>CertAlert</strong> bot
  9. This bot sends an alert when a certificate matching a certain rule is logged in the Certificate Trasparency.
  10. <pre>/list
  11. </pre>
  12. To list the current rules.
  13. <pre>/delete &lt;id&gt;
  14. </pre>
  15. To delete a rule.
  16. <pre>/add &lt;in/start/end&gt; &lt;string&gt;
  17. </pre>
  18. To add a rule.
  19. <i>in</i> mtaches the given substring in any postition, <i>start</i> at the beginning and <i>end</i> at the end
  20. For special characters use the IDNA encoding.
  21. ";
  22. function reply($chatid, $reply) {
  23. return file_get_contents(API_URL."sendmessage?chat_id=".$chatid."&text=".urlencode($reply)."&parse_mode=HTML");
  24. }
  25. $content = file_get_contents("php://input");
  26. //error_log($content);
  27. $update = json_decode($content, true);
  28. $chatid = $update["message"]["chat"]["id"];
  29. $fromid = $update["message"]["from"]["id"];
  30. $fromusername = $update["message"]["from"]["username"];
  31. $command = explode(" ", $update['message']['text'])[0];
  32. switch($command) {
  33. case '/start':
  34. $reply = $help;
  35. break;
  36. case '/list':
  37. $stmt = $db->prepare("SELECT id, type, value FROM rules where userid = ? ORDER BY timestamp ASC");
  38. $stmt->execute(array($fromid));
  39. $rules = $stmt->fetchAll(PDO::FETCH_ASSOC);
  40. if (!empty($rules)) {
  41. $reply = "<strong>ID\tType\tValue</strong>\n";
  42. foreach ($rules as $rule) {
  43. switch($rule['type']) {
  44. case 0:
  45. $type = "in";
  46. break;
  47. case 1:
  48. $type = "start";
  49. break;
  50. case 2:
  51. $type = "end";
  52. break;
  53. }
  54. $reply .= $rule['id']."\t<i>".$type."</i>\t".htmlentities($rule['value'])."\n";
  55. }
  56. } else {
  57. $reply = "There are no rules yet";
  58. }
  59. break;
  60. break;
  61. case '/add':
  62. $exp = explode(" ", $update['message']['text']);
  63. $type = $exp[1];
  64. $value = $exp[2];
  65. switch($type) {
  66. case 'in':
  67. $type = 0;
  68. break;
  69. case 'start':
  70. $type = 1;
  71. break;
  72. case 'end':
  73. $type = 2;
  74. break;
  75. default:
  76. $type = -1;
  77. break;
  78. }
  79. if ($type > -1) {
  80. $stmt = $db->prepare("INSERT INTO rules (userid, chatid, type, value, timestamp) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP())");
  81. $stmt->execute(array($fromid, $chatid, $type, $value));
  82. $reply = "Rule added, check with /list";
  83. } else {
  84. $reply = "Invalid rule type.";
  85. }
  86. break;
  87. case '/delete':
  88. $exp = explode(" ", $update['message']['text']);
  89. $id = $exp[1];
  90. $stmt = $db->prepare("DELETE FROM rules WHERE id = ? AND userid = ?");
  91. $stmt->execute(array($id, $fromid));
  92. $reply = "Rule ".$id." deleted";
  93. break;
  94. default:
  95. $reply = "Unknown command";
  96. break;
  97. }
  98. reply($chatid, $reply);
  99. ?>