update.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Telegram Bot example.
  4. *
  5. * @author Gabriele Grillo <gabry.grillo@alice.it>
  6. */
  7. include 'Telegram.php';
  8. // Set the bot TOKEN
  9. $bot_token = 'bot_token';
  10. // Instances the class
  11. $telegram = new Telegram($bot_token);
  12. /* If you need to manually take some parameters
  13. * $result = $telegram->getData();
  14. * $text = $result["message"] ["text"];
  15. * $chat_id = $result["message"] ["chat"]["id"];
  16. */
  17. // Take text and chat_id from the message
  18. $text = $telegram->Text();
  19. $chat_id = $telegram->ChatID();
  20. // Test CallBack
  21. $callback_query = $telegram->Callback_Query();
  22. if ($callback_query !== null && $callback_query != '') {
  23. $reply = 'Callback value '.$telegram->Callback_Data();
  24. $content = ['chat_id' => $telegram->Callback_ChatID(), 'text' => $reply];
  25. $telegram->sendMessage($content);
  26. $content = ['callback_query_id' => $telegram->Callback_ID(), 'text' => $reply, 'show_alert' => true];
  27. $telegram->answerCallbackQuery($content);
  28. }
  29. //Test Inline
  30. $data = $telegram->getData();
  31. if ($data['inline_query'] !== null && $data['inline_query'] != '') {
  32. $query = $data['inline_query']['query'];
  33. // GIF Examples
  34. if (strpos('testText', $query) !== false) {
  35. $results = json_encode([['type' => 'gif', 'id'=> '1', 'gif_url' => 'http://i1260.photobucket.com/albums/ii571/LMFAOSPEAKS/LMFAO/113481459.gif', 'thumb_url'=>'http://i1260.photobucket.com/albums/ii571/LMFAOSPEAKS/LMFAO/113481459.gif']]);
  36. $content = ['inline_query_id' => $data['inline_query']['id'], 'results' => $results];
  37. $reply = $telegram->answerInlineQuery($content);
  38. }
  39. if (strpos('dance', $query) !== false) {
  40. $results = json_encode([['type' => 'gif', 'id'=> '1', 'gif_url' => 'https://media.tenor.co/images/cbbfdd7ff679e2ae442024b5cfed229c/tenor.gif', 'thumb_url'=>'https://media.tenor.co/images/cbbfdd7ff679e2ae442024b5cfed229c/tenor.gif']]);
  41. $content = ['inline_query_id' => $data['inline_query']['id'], 'results' => $results];
  42. $reply = $telegram->answerInlineQuery($content);
  43. }
  44. }
  45. // Check if the text is a command
  46. if (!is_null($text) && !is_null($chat_id)) {
  47. if ($text == '/test') {
  48. if ($telegram->messageFromGroup()) {
  49. $reply = 'Chat Group';
  50. } else {
  51. $reply = 'Private Chat';
  52. }
  53. // Create option for the custom keyboard. Array of array string
  54. $option = [['A', 'B'], ['C', 'D']];
  55. // Get the keyboard
  56. $keyb = $telegram->buildKeyBoard($option);
  57. $content = ['chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => $reply];
  58. $telegram->sendMessage($content);
  59. } elseif ($text == '/git') {
  60. $reply = 'Check me on GitHub: https://github.com/Eleirbag89/TelegramBotPHP';
  61. // Build the reply array
  62. $content = ['chat_id' => $chat_id, 'text' => $reply];
  63. $telegram->sendMessage($content);
  64. } elseif ($text == '/img') {
  65. // Load a local file to upload. If is already on Telegram's Servers just pass the resource id
  66. $img = curl_file_create('test.png', 'image/png');
  67. $content = ['chat_id' => $chat_id, 'photo' => $img];
  68. $telegram->sendPhoto($content);
  69. //Download the file just sended
  70. $file_id = $message['photo'][0]['file_id'];
  71. $file = $telegram->getFile($file_id);
  72. $telegram->downloadFile($file['result']['file_path'], './test_download.png');
  73. } elseif ($text == '/where') {
  74. // Send the Catania's coordinate
  75. $content = ['chat_id' => $chat_id, 'latitude' => '37.5', 'longitude' => '15.1'];
  76. $telegram->sendLocation($content);
  77. } elseif ($text == '/inlinekeyboard') {
  78. // Shows the Inline Keyboard and Trigger a callback on a button press
  79. $option = [
  80. [
  81. $telegram->buildInlineKeyBoardButton('Callback 1', $url = '', $callback_data = '1'),
  82. $telegram->buildInlineKeyBoardButton('Callback 2', $url = '', $callback_data = '2'),
  83. ],
  84. ];
  85. $keyb = $telegram->buildInlineKeyBoard($option);
  86. $content = ['chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => 'This is an InlineKeyboard Test with Callbacks'];
  87. $telegram->sendMessage($content);
  88. }
  89. }