diff --git a/discovery.json b/discovery.json index f824228..e371239 100644 --- a/discovery.json +++ b/discovery.json @@ -7,6 +7,7 @@ "BotMan\\Drivers\\Telegram\\TelegramAudioDriver", "BotMan\\Drivers\\Telegram\\TelegramFileDriver", "BotMan\\Drivers\\Telegram\\TelegramLocationDriver", + "BotMan\\Drivers\\Telegram\\TelegramContactDriver", "BotMan\\Drivers\\Telegram\\TelegramPhotoDriver", "BotMan\\Drivers\\Telegram\\TelegramVideoDriver" ], diff --git a/src/Providers/TelegramServiceProvider.php b/src/Providers/TelegramServiceProvider.php index bf5e532..4ae7f2f 100644 --- a/src/Providers/TelegramServiceProvider.php +++ b/src/Providers/TelegramServiceProvider.php @@ -11,6 +11,7 @@ use BotMan\Drivers\Telegram\TelegramVideoDriver; use BotMan\Studio\Providers\StudioServiceProvider; use BotMan\Drivers\Telegram\TelegramLocationDriver; +use BotMan\Drivers\Telegram\TelegramContactDriver; use BotMan\Drivers\Telegram\Console\Commands\TelegramRegisterCommand; class TelegramServiceProvider extends ServiceProvider @@ -46,6 +47,7 @@ protected function loadDrivers() DriverManager::loadDriver(TelegramAudioDriver::class); DriverManager::loadDriver(TelegramFileDriver::class); DriverManager::loadDriver(TelegramLocationDriver::class); + DriverManager::loadDriver(TelegramContactDriver::class); DriverManager::loadDriver(TelegramPhotoDriver::class); DriverManager::loadDriver(TelegramVideoDriver::class); } diff --git a/src/TelegramDriver.php b/src/TelegramDriver.php index 7306ab6..4116b55 100644 --- a/src/TelegramDriver.php +++ b/src/TelegramDriver.php @@ -91,7 +91,7 @@ public function getUser(IncomingMessage $matchingMessage) public function matchesRequest() { $noAttachments = $this->event->keys()->filter(function ($key) { - return in_array($key, ['audio', 'voice', 'video', 'photo', 'location', 'document']); + return in_array($key, ['audio', 'voice', 'video', 'photo', 'location', 'contact', 'document']); })->isEmpty(); return $noAttachments && (! is_null($this->event->get('from')) || ! is_null($this->payload->get('callback_query'))) && ! is_null($this->payload->get('update_id')); @@ -343,6 +343,13 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam if (isset($parameters['title'], $parameters['address'])) { $this->endpoint = 'sendVenue'; } + } elseif ($attachment instanceof Contact) { + $this->endpoint = 'sendContact'; + $parameters['phone_number'] = $attachment->getPhoneNumber(); + $parameters['first_name'] = $attachment->getFirstName(); + $parameters['last_name'] = $attachment->getLastName(); + $parameters['user_id'] = $attachment->getUserId(); + $parameters['vcard'] = $attachment->getVcard(); } } else { $parameters['text'] = $message->getText(); diff --git a/tests/TelegramDriverTest.php b/tests/TelegramDriverTest.php index 6f530df..89b4330 100644 --- a/tests/TelegramDriverTest.php +++ b/tests/TelegramDriverTest.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 BotMan\BotMan\Messages\Outgoing\Actions\Button; use BotMan\Drivers\Telegram\Exceptions\TelegramException; @@ -1128,6 +1129,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_throws_exception_in_get_user() {