【问题标题】:How to store send and incomming messages from twilio in my php application?如何在我的 php 应用程序中存储来自 twilio 的发送和传入消息?
【发布时间】:2019-02-19 18:20:50
【问题描述】:

我需要在我的 php 应用程序中存储传入和发送消息到我的数据库。我阅读了 twilio 文档,但我没有找到任何资源满足我的要求。谁能帮助我。

【问题讨论】:

    标签: twilio twilio-php twilio-programmable-chat


    【解决方案1】:

    你读过他们的 webhooks 文档吗?
    https://www.twilio.com/docs/chat/webhook-events
    下面是一些示例代码,用于捕获消息和通道事件并将它们存储到您的数据库中。

    <?php
         // Keep in mind, its just a sample code, you need to make it secure on your end
    
        // Capture the event type to identiy which event has occured 
        $event_type = $_POST['EventType'];
    
        switch($event_type){
            case 'onChannelAdded':
                $sid                =   $_POST['ChannelSid']; // The SID of the newly added Channel
                $attributes         =   $_POST['Attributes']; // The arbitrary JSON structure of the channel
                $date_created       =   $_POST['DateCreated']; // The date of channel creation
                $created_by         =   $_POST['CreatedBy']; // The identity of the user that created a channel
                $friendly_name      =   $_POST['FriendlyName']; // The friendly name of the channel, if set
                $unique_name        =   $_POST['UniqueName']; // The unique name of the channel, if set
                $channel_type       =   $_POST['ChannelType']; // The Channel type. Either private or public
    
                // INSERT a new channel into the channels table
            break;
            case 'onMessageSent':
                $sid                =   $_POST['MessageSid']; // The Message SID of the new Message
                $index              =   $_POST['Index']; // The index of the Message within the Channel Message list
                $channel_sid        =   $_POST['ChannelSid']; // Channel SID identifier of the Channel the Message is being sent to
                $body               =   $_POST['Body']; // The body of message
                $attributes         =   $_POST['Attributes']; // Stringified JSON structure. This can be null if attributes are not present in message entity
                $sender             =   $_POST['From']; // The author of the message
                $date_created       =   $_POST['DateCreated']; // The timestamp of message creation
    
                // INSERT a new message into the chat table
    
            break;
        }
        ?>
    

    此代码用作您的 webhook POST 事件处理程序。
    您需要将此文件的路径添加到您的 twilio 控制台 webhook 配置中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2018-02-25
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多