如何在我的php应用程序中存储来自twilio的已发送和传入消息?

问题描述 投票:0回答:1

我需要在我的php应用程序中存储传入和发送消息到我的数据库。我读了twilio文档,但我找不到任何资源符合我的要求。任何人都可以帮助我。

twilio twilio-php twilio-programmable-chat
1个回答
0
投票

你有没有看过他们的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配置中。

© www.soinside.com 2019 - 2024. All rights reserved.