简体   繁体   中英

Laravel/Twilio Receiving SMS

I use Laravel with https://github.com/aloha/laravel-twilio which allows me to send SMS really easily. The issue comes in that I'd like users to be able to respond back occasionally and I'm unsure of how to setup the webhooks and such. I've read through the Twilio documentation but it hasn't helped a lot, nor does there seem to be a "Laravel" method to solve this.

Are there any libraries or instructions for receiving texts through Laravel? I've tried to look into it and all I get is the Twilio PHP docs or the linked GitHub above. I'm just unsure of how to set it up, I don't have the applicable knowledge of Laravel's structure to link it with the PHP webhooks.

Also, while I'm asking, is there anyway to add a return in a Twilio message?

Twilio developer evangelist here.

When someone sends a message to your Twilio number and you have setup a webhook to your application here's what happens.

Twilio will make an HTTP POST request to your application's webhook URL . That request will contain everything about the message in the body. The request is made in the format application/x-www-form-urlencoded . To your Laravel application, this is the same as a user submitting a regular form on a web page. This means you can access the data the same way you would in a regular POST request. Something like this might get you started:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TwilioController extends Controller
{
    /**
     * receive an incoming SMS message
     *
     * @param  Request  $request
     * @return Response
     */
    public function receiveSMS(Request $request)
    {
        $messageBody = $request->input('Body');
        $phoneNumber = $request->input('From');

        // do something with the message
    }
}

You can respond to the webhook with TwiML , which is just a set of XML tags that TWilio understands. Or, if you just return a 200 OK response, you can use your existing Twilio integration with the Laravel package to send replies too.

Let me know if that helps at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM