Skip to content
Snippets Groups Projects
Commit 48e3ca4e authored by Malcolm Young's avatar Malcolm Young Committed by Shashank Kumar
Browse files

#3394958 capture SMS and display in log

parent a157420f
No related branches found
No related tags found
1 merge request!34#3394958 capture SMS and display in log
......@@ -6,3 +6,4 @@ long_sms: ''
registration_form: ''
twilio_country_codes_container:
country_codes: []
capture_messages: FALSE
......@@ -24,3 +24,6 @@ twilio.settings:
type: sequence
country_codes:
type: sequence
capture_messages:
type: boolean
label: 'Capture messages to log locally, without sending SMS'
<?php declare(strict_types = 1);
namespace Drupal\twilio\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for Twilio routes.
*/
final class TwilioLogController extends ControllerBase {
/**
* The controller constructor.
*/
public function __construct(
private readonly Connection $connection,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self(
$container->get('database'),
);
}
/**
* Builds the response.
*/
public function logPage(): array {
$build['intro'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('If <a href=":config_link">Twilio is configured to capture messages</a>, they will be displayed here.', [
':config_link' => Url::fromRoute('twilio.admin_form')->toString(),
]),
];
$header = [
'id' => t('ID'),
'from' => t('From'),
'to' => t('To'),
'body' => t('Body'),
'mediaUrl' => t('Media URL'),
];
$build['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $this->logs(),
'#empty' => t('No content has been found.'),
];
return $build;
}
public function logs() {
$logs = $this->connection->select('twilio_log', 'tl')
->fields('tl')
->execute()
->fetchAll();
return $logs;
}
}
......@@ -7,6 +7,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\twilio\Controller\TwilioController;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -157,11 +158,11 @@ class TwilioAdminForm extends ConfigFormBase {
],
'#default_value' => $config->get('registration_form'),
];
$form['registration_send'] = [
'#type' => 'checkbox',
'#title' => $this->t('Send confirmation SMS on registration'),
'#default_value' => $config->get('registration_send'),
'#default_value' => $config->get('registration_send'),
];
$form['twilio_country_codes_container'] = [
......@@ -209,6 +210,15 @@ class TwilioAdminForm extends ConfigFormBase {
'#markup' => '<p>' . $status_callback . '</p>',
];
$form['capture_messages'] = [
'#type' => 'checkbox',
'#title' => $this->t('Capture messages to log locally, without sending SMS'),
'#default_value' => $config->get('capture_messages'),
'#description' => $this->t('This will prevent SMS from being sent. Messages will be available in the <a href=":log_link">Twilio log</a>.', [
':log_link' => Url::fromRoute('twilio.twilio_log')->toString(),
])
];
return parent::buildForm($form, $form_state);
}
......
......@@ -67,6 +67,9 @@ class Sms extends TwilioBase {
* ].
*/
public function messageSend(string $number, $message) {
$capture = $this->configFactory->get('twilio.settings')->get('capture_messages');
$fromNumber = $this->twilioCommand->getNumber();
if (is_string($message)) {
$message = [
......@@ -81,10 +84,22 @@ class Sms extends TwilioBase {
throw new TwilioException("Media URL must be a valid, absolute URL.");
}
$client = new Client($this->twilioCommand->getSid(), $this->twilioCommand->getToken());
try {
try {
if ($capture) {
$log = array_merge($message, [
'to' => $number,
]);
$this->connection->insert('twilio_log')
->fields($log)
->execute();
return 'capture';
}
$client->messages->create($number, $message);
$message_status = 'send';
return $message_status;
return $message_status;
}
catch (TwilioException $e) {
$this->messenger->addError($e->getMessage());
......@@ -122,7 +137,7 @@ class Sms extends TwilioBase {
$message = $confirmation_code_text . ': ' . $code;
$number = '+' . $country . $number;
$message_status = $this->messageSend($number, $message);
if ($message_status == 'send') {
$this->twilioInsert($data);
} else {
......@@ -131,7 +146,7 @@ class Sms extends TwilioBase {
[':account_link' => $link]);
$this->messenger->addMessage($message);
}
return $message_status;
}
......
......@@ -49,6 +49,38 @@ function twilio_schema() {
'primary key' => ['number'],
'indexes' => ['uid' => ['uid']],
];
$schema['twilio_log'] = [
'fields' => [
'id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'from' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
],
'to' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
],
'body' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'mediaUrl' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
],
'primary key' => ['id'],
];
return $schema;
}
......@@ -68,3 +100,12 @@ function twilio_update_9001() {
);
}
}
/**
* Add log table.
*/
function twilio_update_10101() {
$spec = twilio_schema();
$schema = \Drupal::service('database')->schema();
$schema->createTable('twilio_log', $spec['twilio_log']);
}
......@@ -3,3 +3,8 @@ twilio.admin_form:
title: Twilio
description: 'Administer your twilio settings'
parent: 'system.admin_config_system'
twilio.log:
route_name: twilio.twilio_log
title: 'Twilio Log'
parent: system.admin_reports
......@@ -4,3 +4,5 @@ administer twilio:
access twilio:
title: 'Phone verification'
description: 'Access the Phone verification page.'
access twilio log:
title: 'Access twilio log'
......@@ -38,3 +38,11 @@ twilio.receive_status:
_controller: '\Drupal\twilio\Controller\TwilioController::receiveStatus'
requirements:
_access: 'TRUE'
twilio.twilio_log:
path: '/admin/reports/twilio'
defaults:
_title: 'Twilio SMS Log'
_controller: '\Drupal\twilio\Controller\TwilioLogController'
requirements:
_permission: 'access twilio log'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment