class Twilio in Twilio SMS Integration 8
Plugin annotation
@SmsGateway(
id = "twilio",
label = @Translation("Twilio"),
outgoing_message_max_recipients = 1,
reports_push = TRUE,
incoming = TRUE,
incoming_route = TRUE
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\sms\Plugin\SmsGatewayPluginBase implements SmsGatewayPluginInterface
- class \Drupal\sms_twilio\Plugin\SmsGateway\Twilio
- class \Drupal\sms\Plugin\SmsGatewayPluginBase implements SmsGatewayPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Twilio
2 string references to 'Twilio'
- sms_twilio_requirements in ./
sms_twilio.install - Implements hook_requirements().
- Twilio::buildConfigurationForm in src/
Plugin/ SmsGateway/ Twilio.php - Form constructor.
File
- src/
Plugin/ SmsGateway/ Twilio.php, line 33
Namespace
Drupal\sms_twilio\Plugin\SmsGatewayView source
class Twilio extends SmsGatewayPluginBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'account_sid' => '',
'auth_token' => '',
'from' => '',
];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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');
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Validates the webhook request and creates an SMS message object.
*
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \Drupal\sms\Message\SmsMessage The parsed message.
*/
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;
}
/**
* Callback for processing incoming messages.
*
* @param \Symfony\Component\HttpFoundation\Request $request The active request.
* @param \Drupal\sms\Entity\SmsGatewayInterface $gateway The SMS gateway.
*
* @return \Drupal\sms\SmsProcessingResponse The processing response.
*/
public function processIncoming(Request $request, SmsGatewayInterface $sms_gateway) {
$task = new SmsProcessingResponse();
$sms = $this
->buildIncomingFromRequest($request);
$sms
->setGateway($sms_gateway);
// Replies should be handled in implementing code.
$response = new Response();
if ($sms
->getResult()
->getError()) {
$response = new Response($sms
->getResult()
->getErrorMessage(), $sms
->getResult()
->getError());
}
$task
->setMessages([
$sms,
]);
$task
->setResponse($response);
return $task;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SmsGatewayPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
SmsGatewayPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
SmsGatewayPluginBase:: |
public | function |
Returns the credit balance available on this gateway. Overrides SmsGatewayPluginInterface:: |
1 |
SmsGatewayPluginBase:: |
public | function |
Gets delivery reports from the gateway. Overrides SmsGatewayPluginInterface:: |
1 |
SmsGatewayPluginBase:: |
public | function |
Parses incoming delivery reports and returns the created delivery reports. Overrides SmsGatewayPluginInterface:: |
1 |
SmsGatewayPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
SmsGatewayPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
SmsGatewayPluginBase:: |
public | function |
Constructs a new SmsGateway plugin. Overrides PluginBase:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Twilio:: |
public | function |
Form constructor. Overrides SmsGatewayPluginBase:: |
|
Twilio:: |
protected | function | Validates the webhook request and creates an SMS message object. | |
Twilio:: |
public | function |
Gets default configuration for this plugin. Overrides SmsGatewayPluginBase:: |
|
Twilio:: |
public | function | Callback for processing incoming messages. | |
Twilio:: |
public | function |
Sends an SMS. Overrides SmsGatewayPluginInterface:: |
|
Twilio:: |
public | function |
Form submission handler. Overrides SmsGatewayPluginBase:: |