You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.2 KiB

1 year ago
  1. <?php
  2. /**
  3. * This file is part of the PHP Telegram Bot example-bot package.
  4. * https://github.com/php-telegram-bot/example-bot/
  5. *
  6. * (c) PHP Telegram Bot Team
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Inline query command
  13. *
  14. * Command that handles inline queries and returns a list of results.
  15. */
  16. namespace Longman\TelegramBot\Commands\SystemCommands;
  17. use Longman\TelegramBot\Commands\SystemCommand;
  18. use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle;
  19. use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultContact;
  20. use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultLocation;
  21. use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultVenue;
  22. use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent;
  23. use Longman\TelegramBot\Entities\ServerResponse;
  24. class InlinequeryCommand extends SystemCommand
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $name = 'inlinequery';
  30. /**
  31. * @var string
  32. */
  33. protected $description = 'Handle inline query';
  34. /**
  35. * @var string
  36. */
  37. protected $version = '1.2.0';
  38. /**
  39. * Main command execution
  40. *
  41. * @return ServerResponse
  42. */
  43. public function execute(): ServerResponse
  44. {
  45. $inline_query = $this->getInlineQuery();
  46. $query = $inline_query->getQuery();
  47. $results = [];
  48. if ($query !== '') {
  49. // https://core.telegram.org/bots/api#inlinequeryresultarticle
  50. $results[] = new InlineQueryResultArticle([
  51. 'id' => '001',
  52. 'title' => 'Simple text using InputTextMessageContent',
  53. 'description' => 'this will return Text',
  54. // Here you can put any other Input...MessageContent you like.
  55. // It will keep the style of an article, but post the specific message type back to the user.
  56. 'input_message_content' => new InputTextMessageContent([
  57. 'message_text' => 'The query that got you here: ' . $query,
  58. ]),
  59. ]);
  60. // https://core.telegram.org/bots/api#inlinequeryresultcontact
  61. $results[] = new InlineQueryResultContact([
  62. 'id' => '002',
  63. 'phone_number' => '12345678',
  64. 'first_name' => 'Best',
  65. 'last_name' => 'Friend',
  66. ]);
  67. // https://core.telegram.org/bots/api#inlinequeryresultlocation
  68. $results[] = new InlineQueryResultLocation([
  69. 'id' => '003',
  70. 'title' => 'The center of the world!',
  71. 'latitude' => 40.866667,
  72. 'longitude' => 34.566667,
  73. ]);
  74. // https://core.telegram.org/bots/api#inlinequeryresultvenue
  75. $results[] = new InlineQueryResultVenue([
  76. 'id' => '004',
  77. 'title' => 'No-Mans-Land',
  78. 'address' => 'In the middle of Nowhere',
  79. 'latitude' => 33,
  80. 'longitude' => -33,
  81. ]);
  82. }
  83. return $inline_query->answer($results);
  84. }
  85. }