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.

82 lines
2.1 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 "/editmessage" command
  13. *
  14. * Command to edit a message sent by the bot.
  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. use Longman\TelegramBot\Request;
  21. class EditmessageCommand extends UserCommand
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name = 'editmessage';
  27. /**
  28. * @var string
  29. */
  30. protected $description = 'Edit a message sent by the bot';
  31. /**
  32. * @var string
  33. */
  34. protected $usage = '/editmessage';
  35. /**
  36. * @var string
  37. */
  38. protected $version = '1.1.0';
  39. /**
  40. * Main command execution
  41. *
  42. * @return ServerResponse
  43. * @throws TelegramException
  44. */
  45. public function execute(): ServerResponse
  46. {
  47. $message = $this->getMessage();
  48. $chat_id = $message->getChat()->getId();
  49. $reply_to_message = $message->getReplyToMessage();
  50. $text = $message->getText(true);
  51. if ($reply_to_message && $message_to_edit = $reply_to_message->getMessageId()) {
  52. // Try to edit the selected message.
  53. $result = Request::editMessageText([
  54. 'chat_id' => $chat_id,
  55. 'message_id' => $message_to_edit,
  56. 'text' => $text ?: 'Edited message',
  57. ]);
  58. // If successful, delete this editing reply message.
  59. if ($result->isOk()) {
  60. Request::deleteMessage([
  61. 'chat_id' => $chat_id,
  62. 'message_id' => $message->getMessageId(),
  63. ]);
  64. }
  65. return $result;
  66. }
  67. return $this->replyToChat(sprintf("Reply to any bots' message and use /%s <your text> to edit it.", $this->getName()));
  68. }
  69. }