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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测