Add a new webhook for sended sms status change. Add status and originating scheduled's id to webhook send_sms

This commit is contained in:
osaajani 2021-10-18 03:40:06 +02:00
parent ec108d8543
commit d0fa0a299c
13 changed files with 91 additions and 25 deletions

View File

@ -1 +1 @@
v3.1.13
v3.2.13

View File

@ -45,11 +45,12 @@ namespace controllers\internals;
* @param bool $flash : Is the sms a flash
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS
* @param ?int $originating_scheduled : Id of the scheduled message that was responsible for sending this message. By default null.
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return mixed : false on error, new sended id else
*/
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, bool $mms = false, array $medias = [], ?string $status = \models\Sended::STATUS_UNKNOWN)
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, bool $mms = false, array $medias = [], ?int $originating_scheduled = null, ?string $status = \models\Sended::STATUS_UNKNOWN)
{
$sended = [
'id_user' => $id_user,
@ -62,6 +63,7 @@ namespace controllers\internals;
'flash' => $flash,
'mms' => $mms,
'status' => $status,
'originating_scheduled' => $originating_scheduled,
];
//Ensure atomicity
@ -105,24 +107,22 @@ namespace controllers\internals;
'status' => $status,
];
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
}
$success = (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
/**
* Update a sended status for a sended.
*
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
*
* @return bool : false on error, true on success
*/
public function update_status(int $id_sended, string $status): bool
{
$sended = [
if (!$success)
{
return $success;
}
$webhook = [
'id' => $id_sended,
'status' => $status,
];
return (bool) $this->get_model()->update($id_sended, $sended);
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS_STATUS_CHANGE, $webhook);
return $success;
}
/**
@ -231,7 +231,7 @@ namespace controllers\internals;
* ?string 'error_message' => null if success, error message else
* ]
*/
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, bool $mms = false, array $medias = [], string $status = \models\Sended::STATUS_UNKNOWN): array
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, bool $mms = false, array $medias = [], $originating_scheduled = null, string $status = \models\Sended::STATUS_UNKNOWN): array
{
$return = [
'error' => false,
@ -278,7 +278,7 @@ namespace controllers\internals;
$return['error'] = true;
$return['error_message'] = $response['error_message'];
$status = \models\Sended::STATUS_FAILED;
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
$sended = [
'id' => $sended_id,
@ -289,6 +289,7 @@ namespace controllers\internals;
'origin' => $id_phone,
'mms' => $mms,
'medias' => $medias,
'originating_scheduled' => $originating_scheduled,
];
$internal_webhook = new Webhook($this->bdd);
@ -299,7 +300,7 @@ namespace controllers\internals;
$internal_quota->consume_credit($id_user, $nb_credits);
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
$sended = [
'id' => $sended_id,
@ -310,6 +311,7 @@ namespace controllers\internals;
'origin' => $id_phone,
'mms' => $mms,
'medias' => $medias,
'originating_scheduled' => $originating_scheduled,
];
$internal_webhook = new Webhook($this->bdd);

View File

@ -139,14 +139,14 @@ class Phone extends AbstractDaemon
//Do message sending
$this->logger->info('Try send message : ' . json_encode($message));
$response = $internal_sended->send($this->adapter, $this->phone['id_user'], $this->phone['id'], $message['text'], $message['destination'], $message['flash'], $message['mms'], $message['medias']);
$response = $internal_sended->send($this->adapter, $this->phone['id_user'], $this->phone['id'], $message['text'], $message['destination'], $message['flash'], $message['mms'], $message['medias'], $message['id_scheduled']);
if ($response['error'])
{
$this->logger->error('Failed send message : ' . json_encode($message) . ' with error : ' . $response['error_message']);
continue;
}
$this->logger->info('Successfully send message : ' . json_encode($message));
}
}

View File

@ -0,0 +1,42 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddScheduledIdToSended extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('sended');
$table->addColumn('originating_scheduled', 'integer', [
'null' => true,
'comment' => 'Id of the scheduled that was responsible for sending this message.',
'after' => 'mms'
])
->update();
}
}

View File

@ -0,0 +1,16 @@
<?php
use Phinx\Migration\AbstractMigration;
class AddWebhookTypeSendSmsStatusChange extends AbstractMigration
{
public function up()
{
$this->execute('ALTER TABLE `webhook` MODIFY `type` ENUM(\'send_sms\', \'send_sms_status_change\', \'receive_sms\', \'inbound_call\')');
}
public function down()
{
$this->execute('ALTER TABLE `webhook` MODIFY `type` ENUM(\'send_sms\', \'receive_sms\', \'inbound_call\')');
}
}

View File

@ -14,6 +14,7 @@ namespace models;
class Webhook extends StandardModel
{
const TYPE_SEND_SMS = 'send_sms';
const TYPE_SEND_SMS_STATUS_CHANGE = 'send_sms_status_change';
const TYPE_RECEIVE_SMS = 'receive_sms';
const TYPE_INBOUND_CALL = 'inbound_call';
const TYPE_QUOTA_LEVEL_ALERT = 'quota_level';

View File

@ -182,7 +182,7 @@ jQuery(document).ready(function()
//Datatable
jQuery('.datatable').DataTable({
"pageLength": 25,
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000, "All"]],
"lengthMenu": [[25, 50, 100, 1000, 10000, Math.pow(10, 10)], [25, 50, 100, 1000, 10000, "All"]],
"language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json",
},

View File

@ -71,7 +71,7 @@ jQuery(document).ready(function ()
{
jQuery('.datatable').DataTable({
"pageLength": 25,
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000, "All"]],
"lengthMenu": [[25, 50, 100, 1000, 10000, Math.pow(10, 10)], [25, 50, 100, 1000, 10000, "All"]],
"language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json",
},

View File

@ -79,7 +79,7 @@ jQuery(document).ready(function ()
{
jQuery('.datatable').DataTable({
"pageLength": 25,
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000, "All"]],
"lengthMenu": [[25, 50, 100, 1000, 10000, Math.pow(10, 10)], [25, 50, 100, 1000, 10000, "All"]],
"language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json",
},

View File

@ -69,7 +69,7 @@ jQuery(document).ready(function ()
{
jQuery('.datatable').DataTable({
"pageLength": 25,
"lengthMenu": [[25, 50, 100, 1000, 10000, -1], [25, 50, 100, 1000, 10000]],
"lengthMenu": [[25, 50, 100, 1000, 10000, Math.pow(10, 10)], [25, 50, 100, 1000, 10000, "All"]],
"language": {
"url": HTTP_PWD + "/assets/js/datatables/french.json",
},

View File

@ -47,6 +47,7 @@
<select name="type" class="form-control" required>
<option value="receive_sms" <?= ($_SESSION['previous_http_post']['type'] ?? '') == 'receive_sms' ? 'selected' : '' ?>>Réception d'un SMS</option>
<option value="send_sms" <?= ($_SESSION['previous_http_post']['type'] ?? '') == 'send_sms' ? 'selected' : '' ?>>Envoi d'un SMS</option>
<option value="send_sms_status_change" <?= ($_SESSION['previous_http_post']['type'] ?? '') == 'send_sms_status_change' ? 'selected' : '' ?>>Mise à jour du statut d'un SMS envoyé</option>
<option value="inbound_call" <?= ($_SESSION['previous_http_post']['type'] ?? '') == 'inbound_call' ? 'selected' : '' ?>>Réception d'un appel téléphonique</option>
</select>
</div>

View File

@ -50,6 +50,8 @@
<select name="webhooks[<?php $this->s($webhook['id']); ?>][type]" class="form-control" required>
<option <?php echo $webhook['type'] == 'receive_sms' ? 'selected="selected"' : '' ?> value="receive_sms">Réception d'un SMS</option>
<option <?php echo $webhook['type'] == 'send_sms' ? 'selected="selected"' : '' ?> value="send_sms">Envoi d'un SMS</option>
<option <?php echo $webhook['type'] == 'send_sms_status_change' ? 'selected="selected"' : '' ?> value="send_sms_status_change">Mise à jour du statut d'un SMS envoyé</option>
<option <?php echo $webhook['type'] == 'inbound_call' ? 'selected="selected"' : '' ?> value="inbound_call">Réception d'un appel téléphonique</option>
</select>
</div>
<hr/>

View File

@ -90,6 +90,8 @@ jQuery(document).ready(function ()
switch (data) {
case 'send_sms':
return 'Envoi de SMS';
case 'send_sms_status_change':
return 'Mise à jour du statut d\'un SMS envoyé';
case 'receive_sms':
return 'Réception de SMS';
case 'inbound_call':