├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Markup ├── Markup.php ├── Message.php ├── Paragraph.php ├── Pause.php ├── Prosody.php ├── SayAs.php ├── Sentence.php └── Substitution.php ├── VonageVoiceChannel.php └── VonageVoiceChannelServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `nexmo-voice-channel` will be documented in this file 4 | 5 | ## 4.0.0 - 2020-12-28 6 | 7 | - Deprecated `call_from` configuration - replacing "voice name" with [new "language" and "style"](https://developer.nexmo.com/voice/voice-api/guides/text-to-speech) options. 8 | 9 | ## 3.1.0 - 2020-12-28 10 | 11 | - Added PHP 8.0 support 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Roommates International PTY LTD 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vonage Voice Notification Channel for Laravel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/roomies/vonage-voice-channel.svg)](https://packagist.org/packages/roomies/vonage-voice-channel) 4 | ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/roomies-com/vonage-voice-channel/phpunit.yml?branch=master) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/roomies/vonage-voice-channel.svg)](https://packagist.org/packages/roomies/vonage-voice-channel) 6 | 7 | This package provides a notification channel for the Laravel framework that works with Vonage's voice API, allowing text-to-speech phone calls. It also provides a fluent interface to construct your message content. 8 | 9 | ## Installation 10 | 11 | You can install the package via Composer: 12 | 13 | ```bash 14 | composer require roomies/vonage-voice-channel 15 | ``` 16 | 17 | Under the hood we use [`laravel/vonage-notification-channel`](https://github.com/laravel/vonage-notification-channel) to configure Vonage, so make sure you have it properly configured using Vonage environment variables. However, in order to make voice calls you need to provide additional credentials in your environment. Note that the private key can be a string or path to the key file. 18 | 19 | ```dotenv 20 | VONAGE_APPLICATION_ID= 21 | VONAGE_PRIVATE_KEY= 22 | ``` 23 | 24 | Then add your call from number and voice to `config/services.php` under the `vonage` key. You can review the [available voices in the Vonage documentation](https://developer.vonage.com/voice/voice-api/guides/text-to-speech). 25 | 26 | ```php 27 | 'vonage' => [ 28 | 'call_from' => env('VONAGE_CALL_FROM'), 29 | 'call_language' => env('VONAGE_CALL_LANGUAGE', 'en-US'), 30 | 'call_style' => env('VONAGE_CALL_STYLE', 0), 31 | ], 32 | ``` 33 | 34 | ## Usage 35 | 36 | Simply route a notification through the `VoiceChannel` and return a formatted message from the `toVoice` method. You use a string with your own [Speech Synthesis Markup Language (SSML)](https://developer.vonage.com/voice/voice-api/guides/customizing-tts) or use the the included wrapper API to build up your message. 37 | 38 | ```php 39 | use Roomies\VonageVoiceChannel\Markup\Message; 40 | use Roomies\VonageVoiceChannel\Markup\SayAs; 41 | use Roomies\VonageVoiceChannel\Markup\Sentence; 42 | use Roomies\VonageVoiceChannel\VonageVoiceChannel; 43 | 44 | /** 45 | * Get the notification's delivery channels. 46 | * 47 | * @param mixed $notifiable 48 | * @return array 49 | */ 50 | public function via($notifiable) 51 | { 52 | return [VonageVoiceChannel::class]; 53 | } 54 | 55 | /** 56 | * Get the voice representation of the notification. 57 | * 58 | * @param mixed $notifiable 59 | * @return \Roomies\VonageVoiceChannel\Markup\Message 60 | */ 61 | public function toVoice($notifiable) 62 | { 63 | return new Message([ 64 | new Sentence('Hi, thanks for joining Roomies.'), 65 | new Sentence([ 66 | 'Your verification code is', 67 | (new SayAs('ABC123'))->interpretAs('spell-out') 68 | ]), 69 | ]); 70 | } 71 | ``` 72 | 73 | ### Markup 74 | 75 | There are a handful of different markup types available to get the right message you're after. Here are some additional examples, otherwise browse `src/Markup` to see all the available options. 76 | 77 | ```php 78 | new Paragraph([ 79 | new Sentence('This is the first sentence of a paragraph.'), 80 | ]); 81 | 82 | new Sentence([ 83 | 'Hey!', 84 | (new Pause)->time('1s'), 85 | (new Prosody('Wake up!'))->volume('loud'), 86 | (new Substitution( 87 | (new SayAs('US'))->interpretAs('spell-out'), 88 | ))->alias('United States'), 89 | ]) 90 | ``` 91 | 92 | ### Custom 93 | 94 | Alternatively, you're free to just return your own SSML markup as a string. This gives you complete control if you need something more custom or have more complex requirements. 95 | 96 | ```php 97 | /** 98 | * Get the voice representation of the notification. 99 | * 100 | * @param mixed $notifiable 101 | * @return string 102 | */ 103 | public function toVoice($notifiable) 104 | { 105 | return ' 106 | Hi, thanks for joining Roomies 107 | Your verification code is ABC123 108 | '; 109 | } 110 | ``` 111 | 112 | ## License 113 | 114 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 115 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "roomies/vonage-voice-channel", 3 | "description": "Send Laravel notifications through a phone call", 4 | "keywords": [ 5 | "laravel", 6 | "roomies", 7 | "vonage-voice-channel", 8 | "laravel-notification-channel" 9 | ], 10 | "homepage": "https://github.com/roomies/vonage-voice-channel", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Dwight Watson", 15 | "email": "dwight@roomies.com", 16 | "homepage": "https://www.roomies.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.2", 22 | "guzzlehttp/guzzle": "^7.0", 23 | "illuminate/notifications": "^11.0 || ^12.0", 24 | "illuminate/support": "^11.0 || ^12.0", 25 | "laravel/vonage-notification-channel": "^3.3" 26 | }, 27 | "require-dev": { 28 | "mockery/mockery": "^1.6.12", 29 | "phpunit/phpunit": "^11.5" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Roomies\\VonageVoiceChannel\\": "src" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Roomies\\VonageVoiceChannel\\Tests\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "vendor/bin/phpunit", 43 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 44 | }, 45 | "config": { 46 | "sort-packages": true, 47 | "preferred-install": "dist" 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "Roomies\\VonageVoiceChannel\\VonageVoiceChannelServiceProvider" 53 | ] 54 | } 55 | }, 56 | "minimum-stability": "dev", 57 | "prefer-stable": true 58 | } 59 | -------------------------------------------------------------------------------- /src/Markup/Markup.php: -------------------------------------------------------------------------------- 1 | 26 | * 27 | * @param mixed $children 28 | * @return void 29 | */ 30 | public function __construct($children = []) 31 | { 32 | $this->children = Arr::wrap($children); 33 | } 34 | 35 | /** 36 | * Set the attribute if it is supported by the element. 37 | * 38 | * @param string $name 39 | * @param array $arguments 40 | * @return $this 41 | */ 42 | public function __call(string $name, array $arguments) 43 | { 44 | $key = Str::kebab($name); 45 | 46 | if (Arr::has($this->attributes, $key)) { 47 | $this->attributes[$key] = Arr::first($arguments); 48 | } 49 | 50 | return $this; 51 | } 52 | 53 | /** 54 | * Get the string representation of the element. 55 | * 56 | * @return string 57 | */ 58 | public function __toString(): string 59 | { 60 | if ($children = $this->getChildren()) { 61 | return sprintf('<%s%s>%s', static::NAME, $this->getAttributes(), $children, static::NAME); 62 | } 63 | 64 | return sprintf('<%s%s/>', static::NAME, $this->getAttributes()); 65 | } 66 | 67 | /** 68 | * Build the children of the markup. 69 | * 70 | * @return string 71 | */ 72 | public function getChildren(): string 73 | { 74 | return collect($this->children)->map(function ($component) { 75 | return (string) $component; 76 | })->implode(''); 77 | } 78 | 79 | /** 80 | * Build an XML attribute string from an array. 81 | * 82 | * @return string 83 | */ 84 | protected function getAttributes() 85 | { 86 | $attributes = collect($this->attributes) 87 | ->filter() 88 | ->map(function ($value, $key) { 89 | return $key . '="' . e($value, false) . '"'; 90 | }); 91 | 92 | if ($attributes->isEmpty()) { 93 | return ''; 94 | } 95 | 96 | return ' '.$attributes->implode(' '); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Markup/Message.php: -------------------------------------------------------------------------------- 1 | '', 21 | 'strength' => '', 22 | ]; 23 | } 24 | -------------------------------------------------------------------------------- /src/Markup/Prosody.php: -------------------------------------------------------------------------------- 1 | null, 21 | 'rate' => null, 22 | 'pitch' => null, 23 | ]; 24 | } 25 | -------------------------------------------------------------------------------- /src/Markup/SayAs.php: -------------------------------------------------------------------------------- 1 | null, 21 | 'format' => null, 22 | ]; 23 | } 24 | -------------------------------------------------------------------------------- /src/Markup/Sentence.php: -------------------------------------------------------------------------------- 1 | null, 21 | ]; 22 | } 23 | -------------------------------------------------------------------------------- /src/VonageVoiceChannel.php: -------------------------------------------------------------------------------- 1 | client = $client; 54 | $this->from = $from; 55 | $this->language = $language; 56 | $this->style = $style; 57 | } 58 | 59 | /** 60 | * Send the given notification. 61 | * 62 | * @param mixed $notifiable 63 | * @param \Illuminate\Notifications\Notification $notification 64 | * @return void|\Vonage\Voice\Webhook\Event 65 | */ 66 | public function send($notifiable, Notification $notification) 67 | { 68 | if (! $to = $notifiable->routeNotificationFor('vonage', $notification)) { 69 | return; 70 | } 71 | 72 | $message = $notification->toVoice($notifiable); 73 | 74 | return $this->call($to, (string) $message); 75 | } 76 | 77 | /** 78 | * Make the call to the given number with the given message. 79 | * 80 | * @param string $phoneNumber 81 | * @param string $message 82 | * @return \Vonage\Voice\Webhook\Event 83 | */ 84 | protected function call($phoneNumber, $message) 85 | { 86 | $outboundCall = new OutboundCall(new Phone($phoneNumber), new Phone($this->from)); 87 | 88 | $ncco = (new NCCO)->addAction(Talk::factory($message, [ 89 | 'level' => 1, 90 | 'language' => $this->language, 91 | 'style' => $this->style, 92 | ])); 93 | 94 | $outboundCall->setNCCO($ncco); 95 | 96 | return $this->client->voice()->createOutboundCall($outboundCall); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/VonageVoiceChannelServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(VonageVoiceChannel::class, function ($app) { 18 | $vonage = $app->make(VonageClient::class); 19 | 20 | return new VonageVoiceChannel( 21 | $vonage, 22 | $app['config']['services.vonage.call_from'], 23 | $app['config']['services.vonage.call_language'] ?? 'en-US', 24 | $app['config']['services.vonage.call_style'] ?? 0 25 | ); 26 | }); 27 | 28 | Notification::resolved(function (ChannelManager $service) { 29 | $service->extend('voice', function ($app) { 30 | return $app->make(VonageVoiceChannel::class); 31 | }); 32 | }); 33 | } 34 | } 35 | --------------------------------------------------------------------------------