getUpdates.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Telegram Bot Example whitout WebHook.
  4. * It uses getUpdates Telegram's API.
  5. *
  6. * @author Gabriele Grillo <gabry.grillo@alice.it>
  7. */
  8. include 'Telegram.php';
  9. $bot_token = 'bot_token';
  10. $telegram = new Telegram($bot_token);
  11. // Get all the new updates and set the new correct update_id
  12. $req = $telegram->getUpdates();
  13. for ($i = 0; $i < $telegram->UpdateCount(); $i++) {
  14. // You NEED to call serveUpdate before accessing the values of message in Telegram Class
  15. $telegram->serveUpdate($i);
  16. $text = $telegram->Text();
  17. $chat_id = $telegram->ChatID();
  18. if ($text == '/start') {
  19. $reply = 'Working';
  20. $content = ['chat_id' => $chat_id, 'text' => $reply];
  21. $telegram->sendMessage($content);
  22. }
  23. if ($text == '/test') {
  24. if ($telegram->messageFromGroup()) {
  25. $reply = 'Chat Group';
  26. } else {
  27. $reply = 'Private Chat';
  28. }
  29. // Create option for the custom keyboard. Array of array string
  30. $option = [['A', 'B'], ['C', 'D']];
  31. // Get the keyboard
  32. $keyb = $telegram->buildKeyBoard($option);
  33. $content = ['chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => $reply];
  34. $telegram->sendMessage($content);
  35. }
  36. if ($text == '/git') {
  37. $reply = 'Check me on GitHub: https://github.com/Eleirbag89/TelegramBotPHP';
  38. // Build the reply array
  39. $content = ['chat_id' => $chat_id, 'text' => $reply];
  40. $telegram->sendMessage($content);
  41. }
  42. }