Created
May 25, 2022 23:12
-
-
Save kalamalahala/9400ea61c4ffdbcd23d7af11d3ef186f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function twilio_csv_show_results() | |
{ | |
// Exit unless the stars are aligned | |
if (!$_POST['csv-submit']) return 'Form was not submitted.'; | |
if ($_POST['confirm-twilio'] !== 'confirm') return 'Confirmation box wasn\'t checked.'; | |
if (!$_POST['body']) return 'No message to send!'; | |
// Start tracking execution time | |
$start_time = microtime(true); | |
// Go get relevant JSON data and decode for PHP | |
global $wpdb; | |
$csv_table = $wpdb->prefix . 'twilio_csv_entries'; | |
$results = $wpdb->get_results('SELECT contact_data FROM ' . $csv_table . ' WHERE id=' . $_POST['csv-select'] . ';'); | |
foreach ($results as $entry) { | |
// Everyone on the uploaded xlsx file | |
$contact_array = json_decode($entry->contact_data); | |
} | |
if (!is_null($contact_array[0]->{'First Name'})) { | |
$uploaded_file_type = 'RMS'; | |
} else if (!is_null($contact_array[0]->Name)) { | |
$uploaded_file_type = 'RMS2'; | |
} else { | |
print 'Error'; | |
die; | |
} | |
// Go get API Keys and open a new Client | |
$api_details = get_option('twilio-csv'); | |
if (is_array($api_details) and count($api_details) != 0) { | |
$TWILIO_SID = $api_details['api_sid']; | |
$TWILIO_TOKEN = $api_details['api_auth_token']; | |
} | |
$client = new Client($TWILIO_SID, $TWILIO_TOKEN); | |
$message_result_list = '<ul>'; | |
$message_count = 0; | |
$contact_count = 0; | |
// List of programmed messages with replacement variables. | |
$messages = array(); | |
$contacted_numbers = array(); | |
$introductory_message = ''; | |
$messages['message-1'] = 'Hey FIRSTNAME, my name is Amila with The Johnson Group. We saw your resume online. Are you still looking for a career opportunity?'; | |
$messages['message-2'] = 'Hi FIRSTNAME, Im Amila with Globe Life - Liberty Division. We received your request for employment consideration. Are you still looking for a career?'; | |
$opt_out_instructions = 'If you are no longer looking for a career opportunity, reply STOP to be removed from our list.'; | |
$selected_message = $messages[$_POST['body']]; | |
// Process list of contacts with selected message | |
foreach ($contact_array as $contact) { | |
$recipient = $contact->CellPhone ?? $contact->Telephone; | |
if ($uploaded_file_type == 'RMS') $first_name = $contact->{'First Name'}; | |
else if ($uploaded_file_type == 'RMS2') $first_name = explode(' ', $contact->Name)[0]; | |
if (!$recipient) { | |
$message_result_list .= '<li>Error: No Cell Phone or Telephone Number</li>'; | |
continue; | |
} | |
// Query Twilio API for all messages sent to this number within the last 30 days. | |
// If any messages are found, skip this contact. | |
$message_query = $client->messages->read( | |
[ | |
'To' => $recipient, | |
'dateSentAfter' => date('Y-m-d', strtotime('-30 days')) | |
], | |
1, | |
1 | |
); | |
if (count($message_query) > 0) { | |
$message_result_list .= '<li>Error: Message already sent to ' . $recipient . ' within the last 30 days. Contact skipped.</li>'; | |
continue; | |
} | |
$TWILIO_MESSAGE_BODY = str_replace('FIRSTNAME', $first_name, $selected_message); | |
// Add each message phone number to array of contacted numbers to prevent duplicates on same CSV file. | |
if (!in_array($recipient, $contacted_numbers)) { | |
try { | |
$send_message = $client->messages->create( | |
$recipient, | |
[ | |
'body' => $TWILIO_MESSAGE_BODY, | |
'from' => 'MGed693e77e70d6f52882605d37cc30d4c' | |
] | |
); | |
$opt_out_message = $client->messages->create( | |
$recipient, | |
[ | |
'body' => $opt_out_instructions, | |
'from' => 'MGed693e77e70d6f52882605d37cc30d4c' | |
] | |
); | |
if ($send_message) $message_result_list .= '<li>Message sent to <a href="tel:' . $recipient . '" title="Call ' . $recipient . '">' . $recipient . '</a></li>'; | |
$message_count++; // total messages sent | |
} catch (\Exception $throwable) { | |
GFCommon::log_error($throwable); | |
write_log('Error sending message to ' . $recipient . '. Details: ' . $throwable); | |
} | |
} | |
array_push($contacted_numbers, $recipient); | |
$contact_count++; // total contacts processed | |
} | |
// Get total execution time in milliseconds. | |
$total_time = round((microtime(true) - $start_time) * 1000); | |
return '<div class="results">Run time: ' . $total_time . ' milliseconds. Messages processed: ' . $message_count . ' to ' . $contact_count . ' contacts. ' . 'Results below: ' . $message_result_list . '</ul></div>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment