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.

85 lines
2.0 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. * Generic message command
  13. *
  14. * Gets executed when any type of message is sent.
  15. *
  16. * In this conversation-related context, we must ensure that active conversations get executed correctly.
  17. */
  18. namespace Longman\TelegramBot\Commands\SystemCommands;
  19. use Longman\TelegramBot\Commands\SystemCommand;
  20. use Longman\TelegramBot\Conversation;
  21. use Longman\TelegramBot\Entities\ServerResponse;
  22. use Longman\TelegramBot\Exception\TelegramException;
  23. use Longman\TelegramBot\Request;
  24. class GenericmessageCommand extends SystemCommand
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $name = 'genericmessage';
  30. /**
  31. * @var string
  32. */
  33. protected $description = 'Handle generic message';
  34. /**
  35. * @var string
  36. */
  37. protected $version = '1.0.0';
  38. /**
  39. * @var bool
  40. */
  41. protected $need_mysql = true;
  42. /**
  43. * Command execute method if MySQL is required but not available
  44. *
  45. * @return ServerResponse
  46. */
  47. public function executeNoDb(): ServerResponse
  48. {
  49. // Do nothing
  50. return Request::emptyResponse();
  51. }
  52. /**
  53. * Main command execution
  54. *
  55. * @return ServerResponse
  56. * @throws TelegramException
  57. */
  58. public function execute(): ServerResponse
  59. {
  60. $message = $this->getMessage();
  61. // If a conversation is busy, execute the conversation command after handling the message.
  62. $conversation = new Conversation(
  63. $message->getFrom()->getId(),
  64. $message->getChat()->getId()
  65. );
  66. // Fetch conversation command if it exists and execute it.
  67. if ($conversation->exists() && $command = $conversation->getCommand()) {
  68. return $this->telegram->executeCommand($command);
  69. }
  70. return Request::emptyResponse();
  71. }
  72. }