Skip to content

Instantly share code, notes, and snippets.

@Leeibrah
Created April 29, 2016 17:11
Show Gist options
  • Save Leeibrah/12084ece9247568da7949ba6619d5b16 to your computer and use it in GitHub Desktop.
Save Leeibrah/12084ece9247568da7949ba6619d5b16 to your computer and use it in GitHub Desktop.
Callback URL
<?php
include("conn.php");
$time=time();
$from = $_POST['from'];
$to = $_POST['to'];
$text = $_POST['text'];
$date = $_POST['date'];
$id = $_POST['id'];
$linkId = $_POST['linkId'];
//This works for onDemand subscription products
// By Lee Test
// $time=time();
// $from = '+25471401079';
// $to = '0723864342';
// // $text = 'Register';
// $text = 'Lee#Rick#Mombasa';
// $date = '12/2/2015';
// $id = 2;
// $linkId = 'links/';
if($text=="Register" or $text=="register")
{
// Be sure to include the file you've just downloaded
// Specify the numbers that you want to send to in a comma-separated list
// Please ensure you include the country code (+254 for Kenya in this case)
$recipients = $from;
// Specify your premium shortCode and keyword
$shortCode = "20150";
$keyword = "DailyPesa";
// Set the bulkSMSMode flag to 0 so that the subscriber gets charged
$bulkSMSMode = 0;
$retryDurationInHours = 1;
// Create an array which would hold the following parameters:
// keyword: Your premium keyword,
// retryDurationInHours: The numbers of hours our API should retry to send the message
// incase it doesn't go through. It is optional
$options = array(
'keyword' => $keyword,
'retryDurationInHours' => $retryDurationInHours
);
// And of course we want our recipients to know what we really do
$message = "To register send your Firstname, Lastname and County in this format: firstname#lastname#county";
// Create a new instance of our awesome gateway class
$gateway = new AfricasTalkingGateway($username, $apikey);
// Any gateway error will be captured by our custom Exception class below,
// so wrap the call in a try-catch block
try
{
// Thats it, hit send and we'll take care of the rest.
$results = $gateway->sendMessage($recipient, $message, $shortCode, $bulkSMSMode, $options);
foreach($results as $result) {
// status is either "Success" or "error message"
echo " Number: " .$result->number;
echo " Status: " .$result->status;
echo " MessageId: " .$result->messageId;
echo " Cost: " .$result->cost."\n";
}
$insert=mysqli_query($mysqli,"INSERT INTO `inbox_sms` (`id`, `phone_number`, `text`) VALUES (NULL, '".$from."', '".$text."')");
}
catch ( AfricasTalkingGatewayException $e )
{
echo "Encountered an error while sending: ".$e->getMessage();
}
}
elseif(preg_match('/#/',$text))
{
//Get the customer text
$customer_text = explode("#", $text);
$customer_text[2] = strtoupper($customer_text[2]);
//send to model
if (
$customer_text[2] == "NAIROBI" ||
$customer_text[2] == "NAIVASHA" ||
$customer_text[2] == "MOMBASA" ||
$customer_text[2] != "/^\s*[a-zA-Z]/"
) {
switch ($customer_text[2]) {
case "NAIROBI":
$message = "Welcome to DailyPesa. Thank You for Registering for Ndovu 1.";
// die($message);
break;
case "NAIVASHA":
$table = "";
$message = "Welcome to DailyPesa. Thank You for Registering for Ndovu 2.";
break;
case "MOMBASA":
$table = "";
$message = "Welcome to DailyPesa. Thank You for Registering for Ndovu 3.";
break;
default:
$message = "Welcome to DailyPesa. Thank You for Registering.";
//SEND TEXT TO CUSTOMER TO START WITH EITHER PEST OR CROP
break;
}
if (strpos($customer_text,'#') !== false) {
$firstname = $customer_text[0];
$lastname = $customer_text[1];
$county = $customer_text[2];
$sql = "INSERT INTO `customers_table` (`id`, `first_name`, `last_name`, `county`, `phone_number`) VALUES
(NULL, '".$firstname."', '".$lastname."', '".$county."', '".$from."')";
$query = mysqli_query($mysqli, $sql);
}
$insert=mysqli_query($mysqli,"INSERT INTO `inbox_sms` (`id`, `phone_number`, `text`) VALUES (NULL, '".$from."', '".$text."')");
// Please ensure you include the country code (+254 for Kenya in this case)
$recipients = $from;
$shortCode = "20150";
$keyword = "DailyPesa";
// Set the bulkSMSMode flag to 0 so that the subscriber gets charged
$bulkSMSMode = 0;
$retryDurationInHours = 1;
// Create an array which would hold the following parameters:
// keyword: Your premium keyword,
// retryDurationInHours: The numbers of hours our API should retry to send the message
// incase it doesn't go through. It is optional
$options = array(
'keyword' => $keyword,
'retryDurationInHours' => $retryDurationInHours
);
// By Lee
$gateway = new AfricasTalkingGateway($username, $apikey);
// Any gateway error will be captured by our custom Exception class below,
// so wrap the call in a try-catch block
try
{
// Thats it, hit send and we'll take care of the rest.
$results = $gateway->sendMessage($recipient, $message, $shortCode, $bulkSMSMode, $options);
foreach($results as $result) {
// status is either "Success" or "error message"
echo " Number: " .$result->number;
echo " Status: " .$result->status;
echo " MessageId: " .$result->messageId;
echo " Cost: " .$result->cost."\n";
}
}
catch ( AfricasTalkingGatewayException $e )
{
echo "Encountered an error while sending: ".$e->getMessage();
}
}
}
else
{
$phoneNumber = $_POST['phoneNumber'];
$shortCode = $_POST['shortCode'];
$keyword = $_POST['keyword'];
$updateType = $_POST['updateType'];
if($updateType == "Addition") {
//Add phoneNumber to subscribers' list
$insert=mysqli_query($mysqli,"INSERT INTO `user` (`id`, `phone_number`, `shortcode`, `keyword`, `updateType`) VALUES (NULL, '".$phoneNumber."', '".$shortCode."', '".$keyword."', '".$updateType."')");
echo "New User has been added";
}elseif($updateType == "Deletion") {
//Remove phoneNumber from subscribers' list
echo "New User has been Removed";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment