⚡Voxel Black Friday Sale is now Live

Send Twilio Text on App Event

Description/Instructions

Getting notified of new posts, updated posts, new timeline posts and all of those important notifications is key to running your Voxel themed directory. Twilio is powerful and very inexpensive to send texts out for important information. In order to do so, follow these instructions:

  1. Navigate to the backend of your site and go to Voxel > App Events.
  2. Click on the notification you want to have sent via Twilio.
  3. Open the accordion and click on “Advanced”.
  4. Copy all of that information and paste it into the functions.php file of your Voxel Child Theme. If you do not have a child theme setup, visit this article to learn how.
  5. Where it says “// your custom code…” paste the following code and get rid of the “// your custom code…”

Be sure to replace all of the necessary information where needed including Account SID, Auth Token, Twilio number with leading country code, and the destination number with country code. Feel free to customize the message under the destination number to further enhance the message.

If this snippet helped, feel free to buy me a taco :)

// Twilio credentials
$account_sid = 'YOUR_TWILIO_ACCOUNT_SID';
$auth_token = 'YOUR_TWILIO_AUTH_TOKEN';
$twilio_number = 'YOUR_TWILIO_PHONE_NUMBER';

// The destination phone number and message content
$to_number = 'DESTINATION_PHONE_NUMBER';
$message_content = 'A post has been updated!';

// Twilio API URL
$url = 'https://api.twilio.com/2010-04-01/Accounts/' . $account_sid . '/Messages.json';

// Setup request to send json via POST
$data = array(
'From' => $twilio_number,
'To' => $to_number,
'Body' => $message_content
);
$post = http_build_query($data);

// Set up cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $account_sid . ':' . $auth_token);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// Send the request and get the response
$response = curl_exec($ch);
$error = curl_error($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close cURL session handle
curl_close($ch);

// Check for errors and process the response
if ($error) {
error_log('Curl error: ' . $error);
} else {
// Process the response as needed
error_log('Twilio API response: ' . $response);
}

  • PHP
Copy Code

Let's Chat About this Snippet