b853bc5b6eb036cea2684c34c569865a9c460fbf
Share

In this fast educational by means of Phil Leggetter, we’ll quilt how you’ll be able to each ship and obtain SMS out of your Laravel software. We’ll do that the use of Nexmo, a cloud communications platform that provides APIs for provisioning telephone numbers, sending and receiving SMS (which is at hand since we’ll use that), making and receiving telephone calls and extra.

Prerequisites

You’ll want a Nexmo account, the Nexmo CLI (Command Line Interface) put in and setup, and your usual necessities for a Laravel app. You’ll additionally want a localtunnel choice in order that the Nexmo carrier could make an HTTP request on your native internet server. We counsel ngrok.

Getting Started

With that during position let’s create our Laravel SMS software:

composer create-project --prefer-dist laravel/laravel laravel-sms

cd laravel-sms

Next, let’s upload the Nexmo Laravel bundle to composer.json.

"require": {

...,

"nexmo/laravel": "dev-master as 1.0",

"nexmo/shopper": "dev-master as 1.0"

},

Note that once those programs pop out of beta, you’ll simplest want to come with the nexmo/laravel bundle and nexmo/shopper will probably be introduced in as a dependency.

Add the Nexmo Service Provider on your app.php config:

'suppliers' => [

...,

NexmoLaravelNexmoServiceProvider::class

]

Note: that you’ll be able to additionally upload to aliases if you wish to use the facade.

Install the programs and duplicate over the Nexmo configuration record:

› composer replace

php artisan seller:submit

Finally, upload your Nexmo API Key and API Secret to /config/nexmo.php, updating the API_KEY and API_SECRET values in accordance with the the ones discovered within the API settings within the Nexmo Dashboard.

'api_key' => 'API_KEY',

'api_secret' => 'API_SECRET',

Note: you’ll be able to clearly set those worth out of your .env and env(...) in the event you favor.

Sending an SMS

Note: for simplicity we’ll upload all capability without delay to the app/Http/routes.php record.

Add the next path to app/Http/routes.php:

Route::get('/sms/ship/{to}', serve as(NexmoClient $nexmo, $to){

$message = $nexmo->message()->ship([

'to' => $to,

'from' => '@leggetter',

'text' => 'Sending SMS from Laravel. Woohoo!'

]);

Log::data('despatched message: ' . $message['message-id']);

});

You’ll understand that within the above code the from worth is ready to a string with the price of @leggetter. This will paintings in the United Kingdom, however would possibly not paintings in different international locations. It truly will depend on the rustic.

For the ones of you that want to use an actual quantity right here, and for everyone else who’ll want an actual quantity for your next step, let’s have a look at both the use of an current quantity or purchasing one.

Listing, Searching and Renting Numbers

One of the necessities for the academic used to be the Nexmo CLI. We can use this to do a complete bunch of items together with operating with telephone numbers (Virtual Numbers to be exact) which are related to our account. If you haven’t already accomplished so that you will have to set up and setup the CLI the place API_KEY and API_SECRET will have to get replaced with the API credentials you used previous:

› npm set up -g nexmo-cli

nexmo setup API_KEY API_SECRET

We can now listing numbers that we’re already renting, seek for numbers which are to be had to hire, hire the ones numbers or cancel the apartment. Let’s get started by means of seeing if we have now any numbers related to our account:

› nexmo quantity:listing

14155550123

In the above instance, I’ve a host. If you don’t have a host, you’ll be able to seek for numbers to hire. All you wish to have to grasp to do this is the 2 personality ISO 3166-1 alpha-2 nation code. It’s more uncomplicated than it sounds. For instance, for Great Britain it’s GB and for the United States it’s US. Let’s seek for a host to hire in the USA:

› nexmo quantity:seek US

14155550111

14155550222

14155550333

If you don’t have a host, you will have to purchase one so as to ship an SMS in international locations that require an actual outbound quantity and in order that we will additionally obtain SMS in a while.

› nexmo quantity:purchase 14155550111 --confirm

Number bought: 14155550111

Really Sending an SMS

Now in the event you replace your code to make use of the quantity you’ve simply bought. Replace the from worth with the true quantity you’ve simply bought or load the quantity in the use of env('NEXMO_NUMBER') as we have now under:

Route::get('/sms/ship/{to}', serve as(NexmoClient $nexmo, $to){

$message = $nexmo->message()->ship([

'to' => $to,

'from' => env('NEXMO_NUMBER'),

'text' => 'Sending SMS from Laravel. Woohoo!'

]);

Log::data('despatched message: ' . $message['message-id']);

});

Then get started the server:

› php artisan serve

Laravel construction server began on http://localhost:8000/

And navigate to http://localhost:8000/sms/ship/YOUR_NUMBER the place YOUR_NUMBER will have to get replaced along with your actual quantity together with the rustic code in a e.164 layout, the similar layout that has been proven in all of the examples above.

If you test garage/logs/laravel.log you’re going to see a log access associated with the message that has simply been despatched with a singular message-id on the finish of the log access e.g.

[2016-08-02 13:45:01] native.INFO: despatched message: 03000000068F5D97

Receiving an Inbound SMS

For the Laravel software to obtain an SMS we want to do a couple of issues:

  1. Ensure our software is reachable by means of Nexmo the use of a localtunnel
  2. Create a path to be referred to as when receiving an SMS
  3. Disable CSRF for our SMS receiving course
  4. Inform Nexmo the place to make a webhook name to when a message is won

Let’s get started by means of making our software reachable by means of the Nexmo platform. Assuming you’re the use of ngrok and your Laravel internet server is listening on port 8000 you’ll be able to do that as follows:

› ngrok http 8000

ngrok by means of @inconshreveable (Ctrl+C to surrender)

 

Tunnel Status on-line

Version 2.1.3

Region United States (us)

Web Interface http://127.0.0.1:4041

Forwarding http://814882e9.ngrok.io -> localhost:8000

Forwarding https://814882e9.ngrok.io -> localhost:8000

 

Connections ttl opn rt1 rt5 p50 p90

0 0 0.00 0.00 0.00 0.00

As you’ll be able to see from the above output from ngrok, a subdomain on ngrok has been created, and any requests to that can now be forwarded directly to localhost:8000.

With the localtunnel in position we will have to now create a path to obtain the SMS in routes.php:

Route::submit('/sms/obtain', serve as(NexmoClient $nexmo){

});

By default, Laravel gained’t permit POST requests to this course with no CSRF token. Since the request to this course will probably be made by means of Nexmo, which gained’t have a token, we’ll disable CSRF for this course. Add this in App/Http/Middleware/VerifyCsrfToken.php:

safe $with the exception of = [

'/sms/receive'

];

Finally, let’s tell Nexmo the place to make the webhook name to when an SMS is won on our quantity. We can do that the use of the Nexmo CLI. When executing the next command you need to use the telephone quantity you will have rented from Nexmo and your ngrok subdomain:

› nexmo hyperlink:sms 14155550111 https://814882e9.ngrok.io/sms/obtain

Number up to date

When you execute this command, the Nexmo carrier will try to name your /sms/obtain course, and also you will have to see the ones requests for your ngrok terminal.

HTTP Requests

-------------

 

POST /sms/obtain 200 OK

If you spot the rest rather than a 200 reaction, then you definately’ll want to repair that for the quantity and webhook affiliation to effectively entire.

The Nexmo PHP Client library supplies some way of simply taking pictures the HTTP parameters which were despatched by means of Nexmo to the Laravel software and growing an InboundMessage message object that we will paintings with. Update the /sms/obtain code as follows:

Route::submit('/sms/obtain', serve as(NexmoClient $nexmo){

$message = NexmoMessageInboundMessage::createFromGlobals();

Log::data('were given textual content: ' . $message->getBody());

});

Then ship an SMS on your rented quantity and test garage/logs/laravel.log the place you’re going to see your message frame logged.

[2016-08-02 13:45:01] native.INFO: despatched message: 03000000068F5D97

[2016-08-02 14:20:50] native.INFO: despatched message: 0300000006917797

[2016-08-02 14:21:17] native.INFO: were given textual content: Sending one again.

Auto-Replying to an Inbound SMS

Finally, let’s create an auto-responder to the inbound message. The Nexmo PHP Client supplies a pleasing approach of doing that the use of the InboundMessage->createReply serve as:

Route::submit('/sms/obtain', serve as(NexmoClient $nexmo){

$message = NexmoMessageInboundMessage::createFromGlobals();

Log::data('were given textual content: ' . $message->getBody());

$respond =$nexmo->message()->ship($message->createReply('Laravel Auto-Reply FTW!'));

Log::data('despatched respond: ' . $respond['message-id']);

});

Send a message on your rented quantity and also you’ll get an auto-reply. It’s now not somewhat magic, but it surely’s beautiful shut!

Conclusion

In this educational, we’ve coated the entirety you wish to have to grasp so as to add sending and receiving SMS, or even auto-replying, on your Laravel software the use of the Nexmo cloud communications platform.

You can to find the code for this pattern by the use of github.com/nexmo-community/laravel-sms.

Please don’t hesitate to get involved with any ideas or comments. You can get involved by the use of e mail the use of [email protected] or touch me on Twitter by the use of @leggetter.

If you do practice this educational thru and would really like some addition credit score, please drop us an e mail along with your Nexmo API Key (now not secret), let us know you noticed this on Laravel News, and we’ll upload some extra credit score on your account.


Many because of Nexmo for sponsoring Laravel News this week and for sharing this educational on learn how to get began the use of their carrier.


supply By https://laravel-news.com/sending-receiving-sms-laravel-nexmo