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.

67 lines
1.5 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. * User "/slap" command
  13. *
  14. * Slap a user around with a big trout!
  15. */
  16. namespace Longman\TelegramBot\Commands\UserCommands;
  17. use Longman\TelegramBot\Commands\UserCommand;
  18. use Longman\TelegramBot\Entities\ServerResponse;
  19. use Longman\TelegramBot\Exception\TelegramException;
  20. class SlapCommand extends UserCommand
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $name = 'slap';
  26. /**
  27. * @var string
  28. */
  29. protected $description = 'Slap someone with their username';
  30. /**
  31. * @var string
  32. */
  33. protected $usage = '/slap <@user>';
  34. /**
  35. * @var string
  36. */
  37. protected $version = '1.2.0';
  38. /**
  39. * Main command execution
  40. *
  41. * @return ServerResponse
  42. * @throws TelegramException
  43. */
  44. public function execute(): ServerResponse
  45. {
  46. $message = $this->getMessage();
  47. $text = $message->getText(true);
  48. $sender = '@' . $message->getFrom()->getUsername();
  49. // Username validation (simply checking for `@something` in the text)
  50. if (0 === preg_match('/@[\w_]{5,}/', $text)) {
  51. return $this->replyToChat('Sorry, no one to slap around...');
  52. }
  53. return $this->replyToChat($sender . ' slaps ' . $text . ' around a bit with a large trout');
  54. }
  55. }