Adding media to Twilio group messaging system - php

My group messaging app allowing people in a group (people.php) to sms each other in a closed system where all users can view and reply to the sms.
My next step would be to allow the media files which I can see on the Twilio Console to be sent to others in this group message system and possibly save a record of that media file.
This my code so far, it functions just missing the media part.
listener.php
<?php
include("../Services/Twilio.php");
include("config.php");
$client = new Services_Twilio($accountsid, $authtoken);
include("functions.php");
include("pdo.class.php");
include("people.php");
if( isset($_POST['Body']) ){
$phone = $_POST['From'];
$message = ($_POST['Body']);
$media = ($_POST['MediaUrl0']);
$name = $people[ $phone ];
if( empty($name) ) $name = $phone;
// resends sms message to group
$message = '['.$name.'] '.$message .$media;
foreach ($people as $number => $name) {
if( $number == $phone ) continue;
$sid = send_sms($number,$message,$media);
}
// reply message for succesfull sent sms
print_sms_reply("Message delivered");
// insert sms traffic into database for record purposes
$now = time();
$pdo = Db::singleton();
$sql = "INSERT INTO message_log SET `message`='{$message}', `sent_from`='{$name}'";
$pdo->exec( $sql );
}
?>
function.php
<?php
function send_sms($number,$message){
global $client,$fromNumber;
$sms = $client->account->sms_messages->create(
$fromNumber,
$number,
$message
);
return $sms->sid;
}
function print_sms_reply ($sms_reply){
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<Response>\n<Sms>\n";
echo $sms_reply;
echo "</Sms></Response>\n";
}
?>
people.php
<?php
include("config.php");
$conn=mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname")
or die (mysqli_error());
$query = "SELECT phone, name FROM grouplist";
$result = mysqli_query($conn, $query);
$people = array();
while ($row = mysqli_fetch_assoc($result)) {
$people[$row['phone']] = $row['name'];
}
?>

Twilio developer evangelist here.
Looks like you need to update your send_sms function to include the media url. Something like this should work:
<?php
function send_sms($number,$message,$media_url){
global $client,$fromNumber;
$sms = $client->account->messages->create(array(
'To' => $number,
'From' => $fromNumber,
'Body' => $message,
'MediaUrl' => $media_url
);
return $sms->sid;
}
?>
Notably, I've updated the call from $client->account->sms_messages->create because the sms_messages resource is deprecated and cannot send media messages. Using $client->account->messages->create uses the newer Messages resource and means you will be able to send media messages.
Let me know if this helps at all.

Related

PHP returning extra whitespace with return statement?

My code works, but for some strange reason when I return a string it adds extra space and I think a new line as well.
The code below works fine. However the length of this when fetched with Ajax is 11, the string success has a length of seven so this isn't right and it also doesn't equal to 'success' when comparing.
I've looked through a few other questions but all the solutions they offer don't seem to be working for me. No trailing spaces anywhere as far as I can see. And it's also not coming from any of the files I'm requiring.
<?php
/*
Plugin Name: Quotation Rest API
Plugin URI:
Description: A rest API for sending quotation request through to email using Sendgrid.
Version: 1.0
Author: Niels
Author URI:
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'offerte', '/send',array(
'methods' => 'POST',
'callback' => 'send_mail',
) );
} );
include('MailChimp.php');
use \DrewM\MailChimp\MailChimp;
function send_mail( WP_REST_Request $request) {
$data = $request['data'];
$name = htmlspecialchars($request['name']);
$email = htmlspecialchars($request['email']);
$comment = htmlspecialchars($request['comment']);
$website = htmlspecialchars($request['url']);
$company = htmlspecialchars($request['company']);
$tel = htmlspecialchars($request['tel']);
$mailchimp = htmlspecialchars($request['mailchimp']);
$dataArray = json_decode($data, true);
ob_start();
include('mail.php');
$mailHTML = ob_get_clean();
$success = 'success';
if (!empty($mailchimp)) {
// add email to mailchimp list
$client = new MailChimp('-us14');// get from setttings
$list_id = ' ';// get from setttings
$result = $client->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
}
$to = " #icloud.com";// get from setttings
$subject = "Offerte van " . $name;// // get from setttings (maybe...)
$message = $mailHTML;
if(wp_mail( $to, $subject, $message)){
return $success;
} else {
// return 'error';
}
}
?>

PHP Mysql insert statement from a function

This code gets values from a database and forms a sms template and then passes the moble number and message to webservice to send the sms. It's part of a function wall().....
$name = $resultarr['name'];
$amount = $resultarr['amount'];
$transaction_id = $resultarr['trans_id'];
$date = $resultarr['time_paid'];
//message template
$message = "Dear $name we have received $amount from you. MPESA transaction Id $transaction_id on $date.";
$mobilenumber = $resultarr['msisdn']; // get mobile number from array
$message_sent = $message;
$serviceArguments = array(
"mobilenumber" => $mobilenumber,
"message" => $message_sent
);
$client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL");
$result = $client->process($serviceArguments);
grabdetails($message_sent, $mobilenumber);
return $result;
}
//I call the function wall() to send sms
wall();
$perm = wall();
$status = $sperm->return; //outputing the status
// Here I want to capture the $status variable and put it in a db below
echo "$status";
function grabdetails($messagee, $mobno)
{
$message_sent = $messagee;
$mobilenumber = $mobno;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smsdb";
// Create connection
// Check connection
$sql = "INSERT INTO smsdb (sms_text, receiver_number, time_sent, status)
VALUES
('$message_sent', '$mobilenumber', NOW(), $status )";
Question is how do I grab $status ind insert it into the db since its not in the function? Kindly. help, anyone?
The code above is not complete, but I assume that the function on top where you do $client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL"); is actually the wall function. If so, then what that function returns, i.e. $result actually has the status you need. So with this code snippet (assuming $sperm is a typo and should actually be $perm, the response from the wall function), you get the response from wall(), which is an object and has the status you need.
$perm = wall();
$status = $sperm->return; //outputing the status
// Here I want to capture the $status variable and put it in a db below
echo "$status";
If that's right, then before calling grabdetails in the wall function, you actually have the status and you can send it to the function like this:
grabdetails($message_sent, $mobilenumber, $result->return);
And then change the definition of the grabdetails to receive the status as well and use it in the DB insert.

Android GCM, want to push custom message

How to push messages into Google Cloud Messaging?
I've viewed this: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/>Android Hive Info
I've tweaked the script a little bit to send messages to everyone in one go.
I just want to push a message from three different fields in a form. How do I go about it?
Here's the code:
<?php
if (isset($_GET["message"])) {
include_once 'db_functions.php';
$db = new DB_Functions();
$users = $db->getAllUsers();
$regIds = array();
if ($users != false){
while ($row = mysqli_fetch_array($users)) {
$regIds[] = $row["gcm_regid"];
}
}
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = $regIds;
$message = "array("price" => $_GET["message"]);"
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
?>

getting an error Class 'TwilioRestClient' not found

I have a message application for ios in which i wish to use twilio sms service. I need to make a webservice for this purpose, but i am getting an error:
Class 'TwilioRestClient' not found
Can anyone help me with it
<?php
include_once "config.php";
require "twilio-php-latest/Services/Twilio.php";
$phoneno = $_REQUEST['phoneno'];
$selectlist = mysql_query("select * from employee where phoneno = '".$phoneno."'");
//set authentication
$AccountSid = '***';
$Authtoken = '***';
//message values
$data = array(
'From' => "***",
'To' => "$phoneno",
'Body' => "Hello, Your guest has arrived at the reception"
);
//new twilio rest client
$client = new Services_Twilio($AccountSid,$AuthToken);
$response = $client -> request("/$ApiVersion/AccountSid/SMS/Messages","POST",$data);
//check response for success or failure
if($response->IsError)
{
while($post = mysql_fetch_assoc($selectlist))
{
$data[] = $post;
}
header('Content-type: application/json');
echo stripslashes(json_encode(array('error reading sms'=>'$response->ErrorMessage')));//echo" error reading sms: ($response->ErrorMessage)";
}
else
{
header('Content-type: application/json');
echo stripslashes(json_encode(array('sent message'=>'ResponseXml->Sid')));//echo "sent message: ($response->ResponseXml->Sid)";
}
?>

can i send multiple messages to multiple devices at a time using GCM push plugin in php

how to send multiple messages to multiple devices at a time
$apiKey = "xxxxx";
$registrationIdArray = array('xxx_1','xxx_2');
$msg = array('message' => 'message_1','title'=> 'title_1','message' => 'message_2','title'=> 'title_2');
$pusher = new Pusher($apiKey);
$pusher->notify($registrationIdArray, $msg);
I have edited my code please use this
<?php
include_once './GCM.php';
$regid = $_POST['regid'];//your registration id array
$fields1=$_POST['message'];//your message array
$total_message = count($fields1);//count total no of message
$total_records = count($regid);//Count total number of registration id
for($j=0;$j< $total_message;$j++)//loop for sending multiple messgae
{
for($i=0;$i< $total_records;$i++)//loop for sending to multiple device
{
$registatoin_ids = array($regid[$i]);
$message = array("price" => $total_message[$j]);
$result = $gcm->send_notification($registatoin_ids, $message);
}
}
?>

Categories