public function Twilio::send in Twilio SMS Integration 8
Sends an SMS.
Parameters
\Drupal\sms\Message\SmsMessageInterface $sms: The sms to be sent.
Return value
\Drupal\sms\Message\SmsMessageResultInterface The result of the sms messaging operation.
Overrides SmsGatewayPluginInterface::send
File
- src/
Plugin/ SmsGateway/ Twilio.php, line 104
Class
- Twilio
- Plugin annotation @SmsGateway( id = "twilio", label = @Translation("Twilio"), outgoing_message_max_recipients = 1, reports_push = TRUE, incoming = TRUE, incoming_route = TRUE )
Namespace
Drupal\sms_twilio\Plugin\SmsGatewayCode
public function send(SmsMessageInterface $sms_message) {
// Messages: https://www.twilio.com/docs/api/rest/message
// Testing API: https://www.twilio.com/docs/api/rest/test-credentials
$recipient = $sms_message
->getRecipients()[0];
$result = new SmsMessageResult();
$account_sid = $this->configuration['account_sid'];
$auth_token = $this->configuration['auth_token'];
$client = new Client($account_sid, $auth_token);
$options = [
'from' => $this->configuration['from'],
'body' => $sms_message
->getMessage(),
];
$report = new SmsDeliveryReport();
$report
->setRecipient($recipient);
try {
$message = $client->messages
->create($recipient, $options);
$report
->setStatus(SmsMessageReportStatus::QUEUED);
$report
->setMessageId($message->uri);
} catch (RestException $e) {
$code = $e
->getCode();
$message = $e
->getMessage();
if (in_array($code, [
21211,
21612,
21610,
21614,
])) {
// 21211: Recipient is invalid. (Test recipient: +15005550001)
// 21612: Cannot route to this recipient. (Test recipient: +15005550002)
// 21610: Recipient is blacklisted. (Test recipient: +15005550004)
// 21614: Recipient is incapable of receiving SMS.
// (Test recipient: +15005550009)
$report
->setStatus(SmsMessageReportStatus::INVALID_RECIPIENT);
$report
->setStatusMessage($message);
}
elseif ($code == 21408) {
// 21408: Account doesn't have the international permission.
// (Test recipient: +15005550003)
$result
->setError(SmsMessageResultStatus::ACCOUNT_ERROR);
$report
->setStatus(SmsMessageReportStatus::ERROR);
$report
->setStatusMessage($message);
}
else {
$report
->setStatus(SmsMessageReportStatus::ERROR);
$report
->setStatusMessage($message);
}
}
if ($report
->getStatus()) {
$result
->addReport($report);
}
return $result;
}