Forums: Sending record data via SMS (Twilio) - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sending record data via SMS (Twilio)

#1 User is offline   admin 

  • Administrator
  • PipPipPip
  • Group: Admin
  • Posts: 17233
  • Joined: 03-February 03
  • Gender:Male

Posted 21 November 2014 - 09:21 PM

In this tutorial we will show how to and SMS sending capabilities to your project. We will add a button to one of List page and that button will send record info via SMS to the current logged user.

1. Open an account with Twilio

Note that Twilio is not free. Their rates depend on number of messages you going to send and many other things.

2. Download Twilio PHP client code and unzip Services folder to your output directory.
This will create Services folder with one file (Twilio.php) and one folder (Twilio) in there.

3. Store logged user cell phone in session variable

Assuming that cell_phone is a field in the login table we can use AfterSuccessfulLogin event. That's the phone number where we will be sending SMSs.

AfterSuccessfulLogin event:
$_SESSION["cell_phone"]=$userdata["cell_phone"];


4. Add button to the List page grid.

Posted Image

Proceed to Twilio control panel and note your phone number, Account SID and Auth Token. You are going to need them soon.

Posted Image

Button Server event code

require "Services/Twilio.php";

$AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$TwilioNumber="xxx-xxx-xxxx";

$http = new Services_Twilio_TinyHttp('https://api.twilio.com', array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => false
))); 
$client = new Services_Twilio($AccountSid, $AuthToken, null, $http);

$record = $button->getCurrentRecord();

$sms = $client->account->messages->sendMessage(
$TwilioNumber, 
$_SESSION["cell_phone"],
"Make: ".$record["Make"]."\n".
"Model: ".$record["Model"]."\n".
"YearOfMake: ".$record["YearOfMake"]."\n".
"Price: ".$record["Price"]
);

Best regards,
Sergey Kornilov
0

#2 User is offline   jay2068 

  • Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 05-March 16

Posted 06 September 2017 - 07:48 PM

This code no longer works as far as I can tell.

I think Twilio may have changed something on the php sdk maybe? I can't find the twilio.php file. Or is this my own php code?

is the services folder in my web folder or php install folder?
0

#3 User is offline   jay2068 

  • Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 05-March 16

Posted 07 September 2017 - 01:18 PM

View Postjay2068, on 06 September 2017 - 02:48 PM, said:

This code no longer works as far as I can tell.

I think Twilio may have changed something on the php sdk maybe? I can't find the twilio.php file. Or is this my own php code?

is the services folder in my web folder or php install folder?


OK well after a good night's sleep I figured it out!

I'm sure there are more ways to solve this but this is what works for me.

I could get the new php software to work by uploading the SDK to my site and naming the folder 'vendor' then copying the 'Twilio' folder to the root also. (Not sure why but it works!)

I just use an include on my button to call my twilio code
$record = $button->getCurrentRecord();
$name = $record["Name"];
$phone = $record["Phone"];
include "twilio_sms.php";


and my twilio_sms.php code:
<?php

require_once "vendor/autoload.php";
use Twilio\Rest\Client;
$sid   = "ACXXXXXXXXXXXXXXXXXXXXX"; // Your Account SID from www.twilio.com/console
$token = "XXXXXXXXXXXXXXXXXXXXXXXXX"; // Your Auth Token from www.twilio.com/console

$client  = new Twilio\Rest\Client($sid, $token);
$message = $client->messages->create($phone, // Text this number
    array(
    'from' => '+15555555555', // From a valid Twilio number
    'body' => 'Hello '.$name.' from Twilio!'
));

print $message->sid;



0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users