Add octopsuh shortcode adapter, still testing. Add callback reception support. Add callback show in adapter.

This commit is contained in:
osaajani 2020-04-03 02:15:55 +02:00
parent 89cb3db678
commit 6ad299f21e
14 changed files with 591 additions and 42 deletions

View File

@ -68,14 +68,14 @@ namespace adapters;
public static function meta_support_flash(): bool;
/**
* Does the implemented service support status change.
* Does the implemented service support status change callback.
*/
public static function meta_support_status_change(): bool;
/**
* Does the implemented service support reception callback.
*/
//public static function meta_support_reception(): bool;
public static function meta_support_reception(): bool;
/**
* Method called to send a SMS to a number.
@ -98,7 +98,15 @@ namespace adapters;
* @return array : [
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'sms' => Array of the sms reads
* array 'smss' => Array of the sms reads
* [
* [
* string 'at' => sms reception date,
* string 'text' => sms text,
* string 'origin' => phone number who sent the sms
* ],
* ...
* ]
* ]
*/
public function read(): array;
@ -128,10 +136,9 @@ namespace adapters;
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* int 'id_phone' : Phone id the sms was received by,
* ]
*
* ]
*/
//public static function reception_callback() : array;
public static function reception_callback() : array;
}

View File

@ -116,6 +116,15 @@ namespace adapters;
{
return false;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return false;
}
/**
* Method called to send a SMS to a number.
@ -367,4 +376,24 @@ namespace adapters;
return (bool) $find;
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -0,0 +1,318 @@
<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace adapters;
/**
* Octopush SMS service with a shortcode adapter
*/
class OctopushShortcodeAdapter implements AdapterInterface
{
const ERROR_CODE_OK = '000';
/**
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
private $datas;
/**
* Octopush login
*/
private $login;
/**
* Octopush api key
*/
private $api_key;
/**
* Sender name to use instead of shortcode
*/
private $sender;
/**
* Octopush api baseurl
*/
private $api_url = 'https://www.octopush-dm.com/api';
/**
* Adapter constructor, called when instanciated by RaspiSMS.
*
* @param string $number : Phone number the adapter is used for
* @param json string $datas : JSON string of the datas to configure interaction with the implemented service
*/
public function __construct(string $datas)
{
$this->datas = json_decode($datas, true);
$this->login = $this->datas['login'];
$this->api_key = $this->datas['api_key'];
$this->sender = $this->datas['sender'] ?? null;
}
/**
* Classname of the adapter.
*/
public static function meta_classname(): string
{
return __CLASS__;
}
/**
* Uniq name of the adapter
* It should be the classname of the adapter un snakecase
*/
public static function meta_uid() : string
{
return 'octopush_shortcode_adapter';
}
/**
* Name of the adapter.
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.).
*/
public static function meta_name(): string
{
return 'Octopush Shortcode';
}
/**
* Description of the adapter.
* A short description of the service the adapter implements.
*/
public static function meta_description(): string
{
$credentials_url = 'https://www.octopush-dm.com/api-logins';
return '
Envoi de SMS avec un shortcode en utilisant <a target="_blank" href="https://www.octopush.com/">Octopush</a>. Pour trouver vos clés API Octopush <a target="_blank" href="' . $credentials_url . '">cliquez ici.</a>
';
}
/**
* List of entries we want in datas for the adapter.
*
* @return array : Every line is a field as an array with keys : name, title, description, required
*/
public static function meta_datas_fields(): array
{
return [
[
'name' => 'login',
'title' => 'Octopush Login',
'description' => 'Login du compte Octopush à employer. Trouvable sur la page des identifiants API Octopush.',
'required' => true,
],
[
'name' => 'api_key',
'title' => 'API Key',
'description' => 'Clef API octopush. Trouvable sur la page des identifiants API Octopush.',
'required' => true,
],
[
'name' => 'sender',
'title' => 'Nom de l\'expéditeur',
'description' => 'Nom de l\'expéditeur à afficher à la place du numéro (11 caractères max).<br/>
<b>Laissez vide pour ne pas utiliser d\'expéditeur nommé.</b><br/>
<b>Si vous utilisez un expéditeur nommé, le destinataire ne pourra pas répondre.</b>',
'required' => false,
],
];
}
/**
* Does the implemented service support reading smss.
*/
public static function meta_support_read(): bool
{
return false;
}
/**
* Does the implemented service support flash smss.
*/
public static function meta_support_flash(): bool
{
return false;
}
/**
* Does the implemented service support status change.
*/
public static function meta_support_status_change(): bool
{
return true;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return true;
}
/**
* Method called to send a SMS to a number.
*
* @param string $destination : Phone number to send the sms to
* @param string $text : Text of the SMS to send
* @param bool $flash : Is the SMS a Flash SMS
*
* @return mixed Uid of the sended message if send, False else
*/
public function send(string $destination, string $text, bool $flash = false)
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
try
{
$datas = [
'user_login' => $this->login,
'api_key' => $this->api_key,
'sms_text' => $text,
'sms_recipients' => str_replace('+', '00', $destination), //Must use 00 instead of + notation
'sms_sender' => '12345',
];
if ($this->sender !== null)
{
$datas['sms_sender'] = $this->sender;
}
$endpoint = $this->api_url . '/sms/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);
$response = curl_exec($curl);
curl_close($curl);
var_dump($response);
}
catch (\Throwable $t)
{
$response['error'] = true;
$response['error_message'] = $t->getMessage();
return $response;
}
}
/**
* Method called to read SMSs of the number.
*
* @return array : [
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'sms' => Array of the sms reads
* ]
*/
public function read(): array
{
return [];
}
/**
* Method called to verify if the adapter is working correctly
* should be use for exemple to verify that credentials and number are both valid.
*
* @return bool : False on error, true else
*/
public function test(): bool
{
try
{
$success = true;
if ($this->datas['sender'] && (mb_strlen($this->datas['sender']) < 3 || mb_strlen($this->datas['sender'] > 11)))
{
return false;
}
$datas = [
'user_login' => $this->login,
'api_key' => $this->api_key,
];
//Check service name
$endpoint = $this->api_url . '/balance/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);
$response = curl_exec($curl);
curl_close($curl);
if ($response === false)
{
return false;
}
$response_decode = json_decode($response, true);
if ($response_decode === null)
{
return false;
}
if ($response_decode['error_code'] != self::ERROR_CODE_OK)
{
return false;
}
return true;
}
catch (\Throwable $t)
{
return false;
}
}
/**
* Method called on reception of a status update notification for a SMS.
*
* @return mixed : False on error, else array ['uid' => uid of the sms, 'status' => New status of the sms (\models\Sended::STATUS_UNKNOWN, \models\Sended::STATUS_DELIVERED, \models\Sended::STATUS_FAILED)]
*/
public static function status_change_callback()
{
var_dump($_REQUEST);
return false;
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -28,11 +28,6 @@ namespace adapters;
*/
private $api;
/**
* Number formated to be compatible with http query according to the ovh way.
*/
private $formatted_number;
/**
* Adapter constructor, called when instanciated by RaspiSMS.
*
@ -83,14 +78,10 @@ namespace adapters;
*/
public static function meta_description(): string
{
$callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => self::meta_uid()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']);
$generate_credentials_url = 'https://eu.api.ovh.com/createToken/index.cgi?GET=/sms&GET=/sms/*&POST=/sms/*&PUT=/sms/*&DELETE=/sms/*&';
return '
Solution de SMS proposé par le groupe <a target="_blank" href="https://www.ovhtelecom.fr/sms/">OVH</a>. Pour générer les clefs API OVH, <a target="_blank" href="' . $generate_credentials_url . '">cliquez ici.</a>
<br/>
<br/>
<div class="alert alert-info">Adresse URL de callback de changement d\'état : <b>' . $callback . '</b></div>
';
}
@ -160,6 +151,15 @@ namespace adapters;
{
return true;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return false;
}
/**
* Method called to send a SMS to a number.
@ -300,7 +300,7 @@ namespace adapters;
{
$success = true;
if ($this->datas['sender'] && mb_strlen($this->datas['sender']))
if ($this->datas['sender'] && (mb_strlen($this->datas['sender']) < 3 || mb_strlen($this->datas['sender'] > 11)))
{
return false;
}
@ -352,4 +352,24 @@ namespace adapters;
return ['uid' => $uid, 'status' => $status];
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -86,14 +86,10 @@ namespace adapters;
*/
public static function meta_description(): string
{
$callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => self::meta_uid()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']);
$generate_credentials_url = 'https://eu.api.ovh.com/createToken/index.cgi?GET=/sms&GET=/sms/*&POST=/sms/*&PUT=/sms/*&DELETE=/sms/*&';
return '
Solution de SMS proposé par le groupe <a target="_blank" href="https://www.ovhtelecom.fr/sms/">OVH</a>. Pour générer les clefs API OVH, <a target="_blank" href="' . $generate_credentials_url . '">cliquez ici.</a>
<br/>
<br/>
<div class="alert alert-info">Adresse URL de callback de changement d\'état : <b>' . $callback . '</b></div>
';
}
@ -163,6 +159,14 @@ namespace adapters;
return true;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return false;
}
/**
* Method called to send a SMS to a number.
*
@ -341,4 +345,23 @@ namespace adapters;
return ['uid' => $uid, 'status' => $status];
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -112,7 +112,15 @@ namespace adapters;
*/
public static function meta_support_status_change(): bool
{
return true;
return false;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return false;
}
/**
@ -255,4 +263,24 @@ namespace adapters;
return $return;
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -90,14 +90,10 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
*/
public static function meta_description(): string
{
$callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => self::meta_uid()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']);
$credentials_url = 'https://www.twilio.com/console';
return '
Solution de SMS avec numéro virtuel proposé par <a target="_blank" href="https://www.twilio.com/sms">Twilio</a>. Pour trouver vos clés API Twilio <a target="_blank" href="' . $credentials_url . '">cliquez ici.</a>
<br/>
<br/>
<div class="alert alert-info">Adresse URL de callback de changement d\'état : <b>' . $callback . '</b></div>
';
}
@ -162,6 +158,14 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
return true;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return false;
}
/**
* Method called to send a SMS to a number.
*
@ -301,7 +305,7 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
{
$sid = $_REQUEST['MessageSid'] ?? false;
$status = $_REQUEST['MessageStatus'] ?? false;
if (!$sid || !$status)
{
return false;
@ -309,19 +313,38 @@ class TwilioVirtualNumberAdapter implements AdapterInterface
switch ($status)
{
case 'delivered' :
$status = \models\Sended::STATUS_DELIVERED;
break;
case 'delivered' :
$status = \models\Sended::STATUS_DELIVERED;
break;
case 'failed' :
$status = \models\Sended::STATUS_FAILED;
break;
case 'failed' :
$status = \models\Sended::STATUS_FAILED;
break;
default :
$status = \models\Sended::STATUS_UNKNOWN;
break;
default :
$status = \models\Sended::STATUS_UNKNOWN;
break;
}
return ['uid' => $sid, 'status' => $status];
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
public static function reception_callback() : array
{
return [];
}
}

View File

@ -23,6 +23,11 @@
font-style: italic;
}
.bold
{
font-weight: bold;
}
.float-right
{
float: right;

View File

@ -220,20 +220,21 @@ namespace controllers\internals;
* @param int $id_phone : Id of the phone the message was sent to
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param ?string $at : Message reception date, if null use current date
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
* @return array : [
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
*/
public function receive (int $id_user, int $id_phone, string $text, string $origin, string $status = \models\Received::STATUS_UNREAD) : array
public function receive (int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD) : array
{
$return = [
'error' => false,
'error_message' => null,
];
$at = (new \DateTime())->format('Y-m-d H:i:s');
$at = $at ?? (new \DateTime())->format('Y-m-d H:i:s');
$is_command = false;
//Process the message to check plus potentially execute command and anonymize text

View File

@ -123,4 +123,63 @@ use Monolog\Logger;
return true;
}
/**
* Function call on sms reception notification
* We return nothing, and we let the adapter do his things.
*
* @param string $adapter_uid : Uid of the adapter to use
* @param int $id_phone : Phone id
*
* @return bool : true on success, false on error
*/
public function reception (string $adapter_uid, int $id_phone)
{
$this->logger->info('Callback reception call with adapter uid : ' . $adapter_uid);
//Search for an adapter
$find_adapter = false;
$adapters = $this->internal_adapter->list_adapters();
foreach ($adapters as $adapter)
{
if (mb_strtolower($adapter['meta_uid']) === $adapter_uid)
{
$find_adapter = $adapter;
}
}
if (false === $find_adapter)
{
$this->logger->error('Callback reception use non existing adapter : ' . $adapter_uid);
return false;
}
//Instanciate adapter, check if status change is supported and if so call status change callback
$adapter_classname = $find_adapter['meta_classname'];
if (!$find_adapter['meta_support_reception'])
{
$this->logger->error('Callback recepetion use adapter ' . $adapter_uid . ' which does not support reception.');
return false;
}
$response = $adapter_classname::reception_callback();
if ($response['error'])
{
$this->logger->error('Callback reception with adapter ' . $adapter_uid . ' failed : ' . $response['error_message']);
return false;
}
$response = $internal_received->receive($this->user['id'], $id_phone, $response['sms']['text'], $response['sms']['origin'], $response['sms']['at']);
if ($response['error'])
{
$this->logger->error('Failed receive message : ' . json_encode($sms) . ' with error : ' . $response['error_message']);
return false;
}
$this->logger->info('Callback reception successfully received message ' . json_encode($response['sms']));
return true;
}
}

View File

@ -36,19 +36,39 @@ class Phone extends \descartes\Controller
public function list($page = 0)
{
$id_user = $_SESSION['user']['id'];
$api_key = $_SESSION['user']['api_key'];
$page = (int) $page;
$phones = $this->internal_phone->list_for_user($id_user, 25, $page);
$adapters = [];
$adapters_metas = $this->internal_adapter->list_adapters();
foreach ($adapters_metas as $adapter_metas)
$adapters = $this->internal_adapter->list_adapters();
foreach ($adapters as $key => $adapter)
{
$adapters[$adapter_metas['meta_classname']] = $adapter_metas['meta_name'];
unset($adapters[$key]);
$adapters[$adapter['meta_classname']] = $adapter;
}
foreach ($phones as &$phone)
{
$phone['adapter'] = $adapters[$phone['adapter']] ?? 'Inconnu';
$adapter = $adapters[$phone['adapter']] ?? false;
if (!$adapter)
{
$phone['adapter'] = 'Inconnu';
continue;
}
$phone['adapter'] = $adapter['meta_name'];
if ($adapter['meta_support_reception'])
{
$phone['callback_reception'] = \descartes\Router::url('Callback', 'reception', ['adapter_uid' => $adapter['meta_uid'], 'id_phone' => $phone['id']], ['api_key' => $api_key]);
}
if ($adapter['meta_support_status_change'])
{
$phone['callback_status'] = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => $adapter['meta_uid']], ['api_key' => $api_key]);
}
}
$this->render('phone/list', ['phones' => $phones]);
@ -207,7 +227,7 @@ class Phone extends \descartes\Controller
if (!$adapter_working)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'utiliser l\'adaptateur choisis avec les données fournies. Vérifiez le numéro de téléphone et les réglages.');
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'utiliser l\'adaptateur choisis avec les données fournies. Vérifiez les réglages.');
return $this->redirect(\descartes\Router::url('Phone', 'add'));
}

View File

@ -183,6 +183,7 @@
'Callback' => [
'update_sended_status' => '/callback/status/{adapter_uid}/',
'reception' => '/callback/reception/{adapter_uid}/{id_phone}/',
],
'Api' => [

View File

@ -56,7 +56,6 @@
<?php foreach ($adapters as $adapter) { ?>
<option
value="<?= $adapter['meta_classname'] ?>"
title="<?php $this->s($adapter['meta_description']); ?>"
data-description="<?php $this->s($adapter['meta_description']); ?>"
data-datas-fields="<?php $this->s(json_encode($adapter['meta_datas_fields'])); ?>"
>

View File

@ -44,6 +44,7 @@
<tr>
<th>Nom</th>
<th>Adaptateur</th>
<th>Callbacks</th>
<th style="width:5%;">Sélectionner</th>
</tr>
</thead>
@ -52,6 +53,21 @@
<tr>
<td><?php $this->s(\controllers\internals\Tool::phone_format($phone['name'])); ?></td>
<td><?php $this->s($phone['adapter']); ?></td>
<td>
<div class="bold">Reception d'un SMS : </div>
<?php if ($phone['callback_reception'] ?? false) { ?>
<div><code><?= $phone['callback_reception']; ?></code></div>
<?php } else { ?>
<div>Non disponible.</div>
<?php } ?>
<br/>
<div class="bold">Changement de status d'un SMS : </div>
<?php if ($phone['callback_status'] ?? false) { ?>
<div><code><?= $phone['callback_status']; ?></code></div>
<?php } else { ?>
<div>Non disponible.</div>
<?php } ?>
</td>
<td><input type="checkbox" value="<?php $this->s($phone['id']); ?>" name="ids[]"></td>
</tr>
<?php } ?>