Advertisement
myarkqub

Telegram Webhook n Database Example

Jun 24th, 2021
2,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "password_here", "dbname");
  3.  
  4. $content = file_get_contents("php://input");
  5. $update = json_decode($content, true);
  6. $bot_token = "1809829596:AAF5zQdYM4lSddaXzpH8pfU_0OBpXh2tjdE";
  7.  
  8. $chat_id = $update["message"]["chat"]["id"];
  9. $message = $update["message"]["text"];
  10. $command1 = explode(" ", $message, 2);
  11. $command = $command1[0];
  12.  
  13. if($command == "/add_user"){
  14.     $user_name = $command1[1];
  15.     mysqli_query($conn, "INSERT INTO tablename (username) VALUES ($user_name)");
  16.     send_message($chat_id, "A user '$user_name' added to database");
  17. }
  18.  
  19. if($command == "/get_user"){
  20.     $output_name = "";
  21.     $query = mysqli_query($conn, "SELECT * FROM tablename");
  22.     while($fetch = mysqli_fetch_assoc($query)){
  23.         $output_name .= $fetch["username"]."\n";
  24.     }
  25.     send_message($chat_id, $output_name);
  26.  
  27. }
  28.  
  29. function send_message($id, $text)
  30. {
  31.     global $bot_token;
  32.     $text = urlencode($text);
  33.     file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$id&text=$text");
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement