From c6a44ea2b3afbf91809248b6ce3decd1a4c88264 Mon Sep 17 00:00:00 2001 From: max-baranikov Date: Fri, 15 Oct 2021 13:39:36 +0300 Subject: [PATCH] Added suggestions to MessageSendRequest (#16) --- src/Bot/Model/Entity/Message/Suggestion.php | 74 +++++++++++++++++++ .../Entity/Message/TransportAttachments.php | 50 +++++++++++++ src/Bot/Model/Request/MessageSendRequest.php | 27 +++++++ tests/Bot/Tests/MessagesTest.php | 64 ++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 src/Bot/Model/Entity/Message/Suggestion.php create mode 100644 src/Bot/Model/Entity/Message/TransportAttachments.php diff --git a/src/Bot/Model/Entity/Message/Suggestion.php b/src/Bot/Model/Entity/Message/Suggestion.php new file mode 100644 index 0000000..6809863 --- /dev/null +++ b/src/Bot/Model/Entity/Message/Suggestion.php @@ -0,0 +1,74 @@ +title; + } + + /** + * @param string $title + */ + public function setTitle(string $title) + { + $this->title = $title; + } + + /** + * @return string|null + */ + public function getType(): ?string + { + return $this->type; + } + + /** + * @param string $type + */ + public function setType(string $type) + { + $this->type = $type; + } +} diff --git a/src/Bot/Model/Entity/Message/TransportAttachments.php b/src/Bot/Model/Entity/Message/TransportAttachments.php new file mode 100644 index 0000000..c46a24f --- /dev/null +++ b/src/Bot/Model/Entity/Message/TransportAttachments.php @@ -0,0 +1,50 @@ +suggestions = $suggestions; + } + + /** + * @return array + */ + public function getSuggestions(): array + { + return $this->suggestions; + } +} diff --git a/src/Bot/Model/Request/MessageSendRequest.php b/src/Bot/Model/Request/MessageSendRequest.php index 1cc0ac5..2bf4c4c 100644 --- a/src/Bot/Model/Request/MessageSendRequest.php +++ b/src/Bot/Model/Request/MessageSendRequest.php @@ -16,6 +16,7 @@ use JMS\Serializer\Annotation\SkipWhenEmpty; use JMS\Serializer\Annotation\Type; use RetailCrm\Mg\Bot\Model\Entity\Message\MessageOrder; use RetailCrm\Mg\Bot\Model\Entity\Message\MessageProduct; +use RetailCrm\Mg\Bot\Model\Entity\Message\TransportAttachments; use RetailCrm\Mg\Bot\Model\ModelInterface; use Symfony\Component\Validator\Constraints as Assert; @@ -81,6 +82,16 @@ class MessageSendRequest implements ModelInterface */ private $scope; + + /** + * @var TransportAttachments $transportAttachments + * + * @Type("RetailCrm\Mg\Bot\Model\Entity\Message\TransportAttachments") + * @Accessor(getter="getTransportAttachments",setter="setTransportAttachments") + * @SkipWhenEmpty + */ + private $transportAttachments; + /** * @var int $chatId * @@ -134,6 +145,22 @@ class MessageSendRequest implements ModelInterface $this->scope = $scope; } + /** + * @return TransportAttachments + */ + public function getTransportAttachments() + { + return $this->transportAttachments; + } + + /** + * @param TransportAttachments $transportAttachments + */ + public function setTransportAttachments(TransportAttachments $transportAttachments) + { + $this->transportAttachments = $transportAttachments; + } + /** * @return string */ diff --git a/tests/Bot/Tests/MessagesTest.php b/tests/Bot/Tests/MessagesTest.php index bff2943..2926a1c 100644 --- a/tests/Bot/Tests/MessagesTest.php +++ b/tests/Bot/Tests/MessagesTest.php @@ -19,6 +19,8 @@ use RetailCrm\Mg\Bot\Model\Entity\Message\MessageOrderPaymentStatus; use RetailCrm\Mg\Bot\Model\Entity\Message\MessageProduct; use RetailCrm\Mg\Bot\Model\Entity\Message\MessageQuantity; use RetailCrm\Mg\Bot\Model\Entity\Message\MessageStatus; +use RetailCrm\Mg\Bot\Model\Entity\Message\Suggestion; +use RetailCrm\Mg\Bot\Model\Entity\Message\TransportAttachments; use RetailCrm\Mg\Bot\Model\Request\MessageEditRequest; use RetailCrm\Mg\Bot\Model\Request\MessageSendRequest; use RetailCrm\Mg\Bot\Model\Response\MessageSendResponse; @@ -297,4 +299,66 @@ class MessagesTest extends TestCase self::assertTrue($response->isSuccessful()); self::assertCount(0, $response->getErrors()); } + + /** + * @group("messages") + * @throws \Exception + */ + public function testMessageSendSuggestions() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse( + '{"message_id":3636,"time":"2019-06-24T06:02:04.434291791Z"}', + 201 + ) + ); + + $suggestionsData = [ + [ + 'title' => 'Hello', + 'type' => 'text' + ], + [ + 'type' => 'email' + ], + [ + 'type' => 'phone' + ], + ]; + + + $suggestions = []; + foreach ($suggestionsData as $suggestionsDatum) { + $suggestion = new Suggestion(); + $suggestion->setType($suggestionsDatum['type']); + + if (isset($suggestionsDatum['title'])) { + $suggestion->setTitle($suggestionsDatum['title']); + } + + $suggestions[] = $suggestion; + } + + $transportAttachments = new TransportAttachments(); + $transportAttachments->setSuggestions($suggestions); + + $request = new MessageSendRequest(); + $request->setChatId(28); + $request->setScope(Constants::MESSAGE_SCOPE_PUBLIC); + $request->setContent("Hello"); + $request->setTransportAttachments($transportAttachments); + + $response = $client->messageSend($request); + + self::assertInstanceOf(MessageSendResponse::class, $response); + + if ($response instanceof MessageSendResponse) { + self::assertTrue($response->isSuccessful()); + self::assertCount(0, $response->getErrors()); + self::assertEquals(3636, $response->getMessageId()); + } + } }