View source
<?php
namespace Drupal\sms_twilio\Plugin\SmsGateway;
use Drupal\Core\Form\FormStateInterface;
use Drupal\sms\Direction;
use Drupal\sms\Entity\SmsGatewayInterface;
use Drupal\sms\Message\SmsDeliveryReport;
use Drupal\sms\Message\SmsMessage;
use Drupal\sms\Message\SmsMessageResultStatus;
use Drupal\sms\Message\SmsMessageReportStatus;
use Drupal\sms\Plugin\SmsGatewayPluginBase;
use Drupal\sms\Message\SmsMessageInterface;
use Drupal\sms\Message\SmsMessageResult;
use Drupal\sms\SmsProcessingResponse;
use Drupal\sms_twilio\Utility\TwilioMedia;
use Drupal\sms_twilio\Utility\TwilioValidation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twilio\Exceptions\RestException;
use Twilio\Rest\Client;
class Twilio extends SmsGatewayPluginBase {
public function defaultConfiguration() {
return [
'account_sid' => '',
'auth_token' => '',
'from' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$config = $this
->getConfiguration();
$form['twilio'] = [
'#type' => 'details',
'#title' => $this
->t('Twilio'),
'#open' => TRUE,
];
$form['twilio']['help'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('API keys can be found at <a href="https://www.twilio.com/console">https://www.twilio.com/console</a>.'),
];
$form['twilio']['account_sid'] = [
'#type' => 'textfield',
'#title' => $this
->t('Account SID'),
'#default_value' => $config['account_sid'],
'#placeholder' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'#required' => TRUE,
];
$form['twilio']['auth_token'] = [
'#type' => 'textfield',
'#title' => $this
->t('Auth token'),
'#default_value' => $config['auth_token'],
'#placeholder' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'#required' => TRUE,
];
$form['twilio']['from'] = [
'#type' => 'textfield',
'#title' => $this
->t('From number'),
'#default_value' => $config['from'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['account_sid'] = trim($form_state
->getValue('account_sid'));
$this->configuration['auth_token'] = trim($form_state
->getValue('auth_token'));
$this->configuration['from'] = $form_state
->getValue('from');
}
public function send(SmsMessageInterface $sms_message) {
$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,
])) {
$report
->setStatus(SmsMessageReportStatus::INVALID_RECIPIENT);
$report
->setStatusMessage($message);
}
elseif ($code == 21408) {
$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;
}
protected function buildIncomingFromRequest(Request $request) {
$result = new SmsMessageResult();
$params = $request->request
->all();
$report = (new SmsDeliveryReport())
->setRecipient($params['To'])
->setStatus(SmsMessageReportStatus::DELIVERED);
$sms = (new SmsMessage())
->setMessage(trim($params['Body']))
->setDirection(Direction::INCOMING)
->setOption('data', $params)
->setSenderNumber($params['From'])
->addRecipients([
$params['To'],
]);
if ($files = TwilioMedia::processMedia($params)) {
$sms
->setOption('media', $files);
}
if (!TwilioValidation::validateIncoming($request, $this)) {
$report
->setStatus(SmsMessageReportStatus::REJECTED);
$report
->setStatusMessage($e
->getMessage());
$result
->setError($e
->getCode());
$result
->setErrorMessage($e
->getMessage());
}
$result
->addReport($report);
$sms
->setResult($result);
return $sms;
}
public function processIncoming(Request $request, SmsGatewayInterface $sms_gateway) {
$task = new SmsProcessingResponse();
$sms = $this
->buildIncomingFromRequest($request);
$sms
->setGateway($sms_gateway);
$response = new Response();
if ($sms
->getResult()
->getError()) {
$response = new Response($sms
->getResult()
->getErrorMessage(), $sms
->getResult()
->getError());
}
$task
->setMessages([
$sms,
]);
$task
->setResponse($response);
return $task;
}
}