From d9b45c10ec524cc17aae326ddeb15fb9e7d8fa8c Mon Sep 17 00:00:00 2001 From: Daniele Rapisarda Date: Tue, 25 Sep 2018 18:36:53 +0200 Subject: [PATCH 1/2] added contact driver --- .discovery/Discovery.php | 80 +++++++++++++++++ .discovery/discovery_asset_types.php | 102 ++++++++++++++++++++++ .discovery/discovery_values.php | 21 +++++ discovery.json | 1 + src/Providers/TelegramServiceProvider.php | 2 + src/TelegramDriver.php | 9 +- tests/TelegramDriverTest.php | 40 +++++++++ 7 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 .discovery/Discovery.php create mode 100644 .discovery/discovery_asset_types.php create mode 100644 .discovery/discovery_values.php diff --git a/.discovery/Discovery.php b/.discovery/Discovery.php new file mode 100644 index 0000000..db92a7c --- /dev/null +++ b/.discovery/Discovery.php @@ -0,0 +1,80 @@ +values = require __DIR__.'/discovery_values.php'; + $this->assetTypesArray = require __DIR__.'/discovery_asset_types.php'; + } + + /** + * Returns the unique instance of this class (singleton). + * + * @return self + */ + public static function getInstance(): self + { + if (!self::$instance) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Returns the asset values of the requested type. + * + * If no assets are found, an empty array is returned. + * + * @param string $assetType + * @return string[] + */ + public function get(string $assetType) : array + { + return $this->values[$assetType] ?? []; + } + + /** + * Returns an asset type object for the requested type. + * + * If no assets are found, an AssetType object containing no assets is returned. + * + * @param string $assetType + * @return AssetTypeInterface + */ + public function getAssetType(string $assetType) : AssetTypeInterface + { + if (!isset($this->assetTypes[$assetType])) { + if (isset($this->assetTypesArray[$assetType])) { + $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, $this->assetTypesArray[$assetType]); + } else { + $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, []); + } + } + return $this->assetTypes[$assetType]; + } +} diff --git a/.discovery/discovery_asset_types.php b/.discovery/discovery_asset_types.php new file mode 100644 index 0000000..78718c5 --- /dev/null +++ b/.discovery/discovery_asset_types.php @@ -0,0 +1,102 @@ + + array ( + 0 => + array ( + 'value' => 'stubs/telegram.php', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + ), + 'botman/driver' => + array ( + 0 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 1 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramAudioDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 2 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramFileDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 3 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramLocationDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 4 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramContactDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 5 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramPhotoDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + 6 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\TelegramVideoDriver', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + ), + 'botman/commands' => + array ( + 0 => + array ( + 'value' => 'BotMan\\Drivers\\Telegram\\Console\\Commands\\TelegramRegisterCommand', + 'package' => 'botman/driver-telegram', + 'packageDir' => './', + 'priority' => 0.0, + 'metadata' => + array ( + ), + ), + ), +); diff --git a/.discovery/discovery_values.php b/.discovery/discovery_values.php new file mode 100644 index 0000000..d6185ba --- /dev/null +++ b/.discovery/discovery_values.php @@ -0,0 +1,21 @@ + + array ( + 0 => 'stubs/telegram.php', + ), + 'botman/driver' => + array ( + 0 => 'BotMan\\Drivers\\Telegram\\TelegramDriver', + 1 => 'BotMan\\Drivers\\Telegram\\TelegramAudioDriver', + 2 => 'BotMan\\Drivers\\Telegram\\TelegramFileDriver', + 3 => 'BotMan\\Drivers\\Telegram\\TelegramLocationDriver', + 4 => 'BotMan\\Drivers\\Telegram\\TelegramContactDriver', + 5 => 'BotMan\\Drivers\\Telegram\\TelegramPhotoDriver', + 6 => 'BotMan\\Drivers\\Telegram\\TelegramVideoDriver', + ), + 'botman/commands' => + array ( + 0 => 'BotMan\\Drivers\\Telegram\\Console\\Commands\\TelegramRegisterCommand', + ), +); 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() { From a1fdc566f276f560cb2f1d1ddc72c520eac72075 Mon Sep 17 00:00:00 2001 From: Daniele Rapisarda Date: Tue, 25 Sep 2018 18:43:27 +0200 Subject: [PATCH 2/2] removed self-generated files --- .discovery/Discovery.php | 80 --------------------- .discovery/discovery_asset_types.php | 102 --------------------------- .discovery/discovery_values.php | 21 ------ 3 files changed, 203 deletions(-) delete mode 100644 .discovery/Discovery.php delete mode 100644 .discovery/discovery_asset_types.php delete mode 100644 .discovery/discovery_values.php diff --git a/.discovery/Discovery.php b/.discovery/Discovery.php deleted file mode 100644 index db92a7c..0000000 --- a/.discovery/Discovery.php +++ /dev/null @@ -1,80 +0,0 @@ -values = require __DIR__.'/discovery_values.php'; - $this->assetTypesArray = require __DIR__.'/discovery_asset_types.php'; - } - - /** - * Returns the unique instance of this class (singleton). - * - * @return self - */ - public static function getInstance(): self - { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; - } - - /** - * Returns the asset values of the requested type. - * - * If no assets are found, an empty array is returned. - * - * @param string $assetType - * @return string[] - */ - public function get(string $assetType) : array - { - return $this->values[$assetType] ?? []; - } - - /** - * Returns an asset type object for the requested type. - * - * If no assets are found, an AssetType object containing no assets is returned. - * - * @param string $assetType - * @return AssetTypeInterface - */ - public function getAssetType(string $assetType) : AssetTypeInterface - { - if (!isset($this->assetTypes[$assetType])) { - if (isset($this->assetTypesArray[$assetType])) { - $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, $this->assetTypesArray[$assetType]); - } else { - $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, []); - } - } - return $this->assetTypes[$assetType]; - } -} diff --git a/.discovery/discovery_asset_types.php b/.discovery/discovery_asset_types.php deleted file mode 100644 index 78718c5..0000000 --- a/.discovery/discovery_asset_types.php +++ /dev/null @@ -1,102 +0,0 @@ - - array ( - 0 => - array ( - 'value' => 'stubs/telegram.php', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - ), - 'botman/driver' => - array ( - 0 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 1 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramAudioDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 2 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramFileDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 3 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramLocationDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 4 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramContactDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 5 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramPhotoDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - 6 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\TelegramVideoDriver', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - ), - 'botman/commands' => - array ( - 0 => - array ( - 'value' => 'BotMan\\Drivers\\Telegram\\Console\\Commands\\TelegramRegisterCommand', - 'package' => 'botman/driver-telegram', - 'packageDir' => './', - 'priority' => 0.0, - 'metadata' => - array ( - ), - ), - ), -); diff --git a/.discovery/discovery_values.php b/.discovery/discovery_values.php deleted file mode 100644 index d6185ba..0000000 --- a/.discovery/discovery_values.php +++ /dev/null @@ -1,21 +0,0 @@ - - array ( - 0 => 'stubs/telegram.php', - ), - 'botman/driver' => - array ( - 0 => 'BotMan\\Drivers\\Telegram\\TelegramDriver', - 1 => 'BotMan\\Drivers\\Telegram\\TelegramAudioDriver', - 2 => 'BotMan\\Drivers\\Telegram\\TelegramFileDriver', - 3 => 'BotMan\\Drivers\\Telegram\\TelegramLocationDriver', - 4 => 'BotMan\\Drivers\\Telegram\\TelegramContactDriver', - 5 => 'BotMan\\Drivers\\Telegram\\TelegramPhotoDriver', - 6 => 'BotMan\\Drivers\\Telegram\\TelegramVideoDriver', - ), - 'botman/commands' => - array ( - 0 => 'BotMan\\Drivers\\Telegram\\Console\\Commands\\TelegramRegisterCommand', - ), -);