PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 18, 2022

[FIXED] How to store send and incomming messages from twilio in my php application?

 November 18, 2022     twilio, twilio-php, twilio-programmable-chat     No comments   

Issue

I need to store incoming and send messages to my database in my php appication.I read twilio documentation but i don't find any resource for my requirement.Can anyone help me with that.


Solution

Have you read their webhooks documentation?
https://www.twilio.com/docs/chat/webhook-events
Here is some sample code for capturing message and channel events and storing them to your database.

<?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;
    }
    ?>

This code serves as your webhook POST events handler.
You need to add this file's path into your twilio console webhook configurations.



Answered By - Yousaf Hassan
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing