diff --git a/src/TelegramContactDriver.php b/src/TelegramContactDriver.php new file mode 100644 index 0000000..947c9b8 --- /dev/null +++ b/src/TelegramContactDriver.php @@ -0,0 +1,68 @@ +event->get('from')) && ! is_null($this->event->get('contact')); + } + + /** + * @return bool + */ + public function hasMatchingEvent() + { + return false; + } + + /** + * Retrieve the chat message. + * + * @return array + */ + public function getMessages() + { + if (empty($this->messages)) { + $this->loadMessages(); + } + + return $this->messages; + } + + /** + * Load Telegram messages. + */ + public function loadMessages() + { + $message = new IncomingMessage(Contact::PATTERN, $this->event->get('from')['id'], $this->event->get('chat')['id'], $this->event); + $message->setContact(new Contact( + $this->event->get('contact')['phone_number'], + $this->event->get('contact')['first_name'], + $this->event->get('contact')['last_name'], + $this->event->get('contact')['user_id'], + $this->event->get('contact')['vcard'] + )); + + $this->messages = [$message]; + } + + /** + * @return bool + */ + public function isConfigured() + { + return false; + } +} diff --git a/src/TelegramDriver.php b/src/TelegramDriver.php index a907b2a..cb40f0e 100644 --- a/src/TelegramDriver.php +++ b/src/TelegramDriver.php @@ -17,6 +17,7 @@ use BotMan\BotMan\Drivers\Events\GenericEvent; use Symfony\Component\HttpFoundation\Response; use BotMan\BotMan\Messages\Attachments\Location; +use BotMan\BotMan\Messages\Attachments\Contact; use Symfony\Component\HttpFoundation\ParameterBag; use BotMan\BotMan\Messages\Incoming\IncomingMessage; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; diff --git a/tests/TelegramDriverTest.php b/tests/TelegramDriverTest.php index 3fd7f8f..f38739b 100644 --- a/tests/TelegramDriverTest.php +++ b/tests/TelegramDriverTest.php @@ -1461,6 +1461,45 @@ public function it_can_reply_message_objects_with_location() $driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', new Location('123', '321')), $message)); } + /** @test */ + public function it_can_reply_message_objects_with_contact() + { + $responseData = [ + 'update_id' => '1234567890', + 'message' => [ + 'message_id' => '123', + 'from' => [ + 'id' => 'from_id', + ], + 'chat' => [ + 'id' => '12345', + ], + 'date' => '1480369277', + 'text' => 'Telegram Text', + ], + ]; + + $html = m::mock(Curl::class); + $html->shouldReceive('post') + ->once() + ->with('https://api.telegram.org/botTELEGRAM-BOT-TOKEN/sendContact', [], [ + 'chat_id' => '12345', + 'phone_number' => '0775269856', + 'first_name' => 'Daniele', + 'first_name' => 'Rapisarda', + 'user_id' => '123', + 'caption' => 'Test', + ]); + + $request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]'); + $request->shouldReceive('getContent')->andReturn(json_encode($responseData)); + + $driver = new TelegramDriver($request, $this->telegramConfig, $html); + + $message = $driver->getMessages()[0]; + $driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', new Contact('0775269856', 'Daniele', 'Rapisarda', '123')), $message)); + } + /** @test */ public function it_can_reply_message_objects_with_contact() {