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

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 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键