drny60365 2018-05-11 02:16
浏览 203
已采纳

Laravel / Twilio接收短信

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?

  • 写回答

1条回答 默认 最新

  • duanhuancong1969 2018-05-11 02:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?