mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Improve adapters datas interface by defining fields and implement real OVH SMS API adapter !
This commit is contained in:
parent
4cd1105ae6
commit
c4bc7d94c1
7 changed files with 591 additions and 48 deletions
|
@ -31,6 +31,12 @@
|
|||
* Description of the datas expected by the adapter to help the user. (e.g : A list of expecteds Api credentials fields, with name and value)
|
||||
*/
|
||||
public static function meta_datas_help() : string;
|
||||
|
||||
/**
|
||||
* List of entries we want in datas for the adapter
|
||||
* @return array : Eachline line is a field as an array with keys : name, title, description, required
|
||||
*/
|
||||
public static function meta_datas_fields() : array;
|
||||
|
||||
/**
|
||||
* Does the implemented service support flash smss
|
||||
|
@ -58,7 +64,7 @@
|
|||
* @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);
|
||||
public function send (string $destination, string $text, bool $flash = false);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -68,6 +74,14 @@
|
|||
public function read () : array;
|
||||
|
||||
|
||||
/**
|
||||
* 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 boolean : False on error, true else
|
||||
*/
|
||||
public function test () : bool;
|
||||
|
||||
|
||||
/**
|
||||
* 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 ('unknown', 'delivered', 'failed')]
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
namespace adapters;
|
||||
use \Ovh\Api;
|
||||
|
||||
/**
|
||||
* Interface for phones adapters
|
||||
|
@ -30,7 +31,47 @@
|
|||
/**
|
||||
* Description of the datas expected by the adapter to help the user. (e.g : A list of expecteds Api credentials fields, with name and value)
|
||||
*/
|
||||
public static function meta_datas_help() : string { return 'See later.'; }
|
||||
public static function meta_datas_help() : string { return 'Clefs API OVH, https://api.ovh.com/createToken/index.cgi.'; }
|
||||
|
||||
/**
|
||||
* 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' => 'app_key',
|
||||
'title' => 'Application Key',
|
||||
'description' => 'Paramètre "Application Key" obtenu lors de la génération de la clef API OVH.',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'app_secret',
|
||||
'title' => 'Application Secret',
|
||||
'description' => 'Paramètre "Application Secret" obtenu lors de la génération de la clef API OVH.',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'consumer_key',
|
||||
'title' => 'Consumer Key',
|
||||
'description' => 'Paramètre "Consumer Key" obtenu lors de la génération de la clef API OVH.',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'endpoint',
|
||||
'title' => 'Endpoint',
|
||||
'description' => 'Endpoint de l\'API OVH, voir https://github.com/ovh/php-ovh/#supported-apis.',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'service_name',
|
||||
'title' => 'Service Name',
|
||||
'description' => 'Service Name de votre service SMS chez OVH. Il s\'agit du nom associé à votre service SMS dans la console OVH, probablement quelque chose comme "sms-xxxxx-1" ou "xxxx" est votre identifiant client OVH.',
|
||||
'required' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the implemented service support flash smss
|
||||
|
@ -53,6 +94,15 @@
|
|||
*/
|
||||
private $datas;
|
||||
|
||||
/**
|
||||
* OVH Api instance
|
||||
*/
|
||||
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
|
||||
|
@ -62,7 +112,15 @@
|
|||
public function __construct (string $number, string $datas)
|
||||
{
|
||||
$this->number = $number;
|
||||
$this->datas = $datas;
|
||||
$this->formatted_number = str_replace('+', '00', $number);
|
||||
$this->datas = json_decode($datas, true);
|
||||
|
||||
$this->api = new Api(
|
||||
$this->datas['app_key'],
|
||||
$this->datas['app_secret'],
|
||||
$this->datas['endpoint'],
|
||||
$this->datas['consumer_key']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,9 +131,33 @@
|
|||
* @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)
|
||||
public function send (string $destination, string $text, bool $flash = false)
|
||||
{
|
||||
return uniqid();
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/jobs';
|
||||
$params = [
|
||||
'message' => $text,
|
||||
'receivers' => [$destination],
|
||||
];
|
||||
|
||||
$response = $this->api->post($endpoint, $params);
|
||||
|
||||
$nb_invalid_receivers = count(($response['invalidReceivers'] ?? []));
|
||||
if ($nb_invalid_receivers > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$uids = $response['ids'] ?? [];
|
||||
return ($uids[0] ?? false);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -85,7 +167,77 @@
|
|||
*/
|
||||
public function read () : array
|
||||
{
|
||||
return [];
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming';
|
||||
$uids = $this->api->get($endpoint);
|
||||
|
||||
if (!is_array($uids) || !$uids)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$received_smss = [];
|
||||
foreach ($uids as $uid)
|
||||
{
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming/' . $uid;
|
||||
$sms_details = $this->api->get($endpoint);
|
||||
|
||||
if (!isset($sms_details['creationDatetime'], $sms_details['message'], $sms_details['sender']))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$received_smss[] = [
|
||||
'at' => (new \DateTime($sms_details['creationDatetime']))->format('Y-m-d H:i:s'),
|
||||
'text' => $sms_details['message'],
|
||||
'origin' => $sms_details['sender'],
|
||||
'destination' => $this->number,
|
||||
];
|
||||
|
||||
//Remove the sms to prevent double reading as ovh do not offer a filter for unread messages only
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming/' . $uid;
|
||||
$this->api->delete($endpoint);
|
||||
}
|
||||
|
||||
return $received_smss;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
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 boolean : False on error, true else
|
||||
*/
|
||||
public function test () : bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
//Check service name
|
||||
$endpoint = '/sms/' . $this->datas['service_name'];
|
||||
$response = $this->api->get($endpoint);
|
||||
$success = $success && (bool) $response;
|
||||
|
||||
//Check virtualnumber
|
||||
$endpoint = '/sms/virtualNumbers/' . $this->formatted_number;
|
||||
$response = $this->api->get($endpoint);
|
||||
$success = $success && (bool) $response;
|
||||
|
||||
return $success;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -95,6 +247,30 @@
|
|||
*/
|
||||
public static function status_change_callback ()
|
||||
{
|
||||
return false;
|
||||
$uid = $_GET['id'] ?? false;
|
||||
$dlr = $_GET['dlr'] ?? false;
|
||||
|
||||
if ($uid === false || $dlr ==== false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($dlr)
|
||||
{
|
||||
case 1:
|
||||
$status = 'delivered';
|
||||
break;
|
||||
|
||||
case 2:
|
||||
case 16:
|
||||
$status = 'failed';
|
||||
break;
|
||||
|
||||
default:
|
||||
$status = 'unknown';
|
||||
break;
|
||||
}
|
||||
|
||||
return ['uid' => $uid, 'status' => $status];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,12 @@
|
|||
* Description of the datas expected by the adapter to help the user. (e.g : A list of expecteds Api credentials fields, with name and value)
|
||||
*/
|
||||
public static function meta_datas_help() : string { return 'No datas.'; }
|
||||
|
||||
/**
|
||||
* List of entries we want in datas for the adapter
|
||||
* @return array : Eachline line is a field as an array with keys : name, title, description, required
|
||||
*/
|
||||
public static function meta_datas_fields() : array { return []; }
|
||||
|
||||
/**
|
||||
* Does the implemented service support flash smss
|
||||
|
@ -84,7 +90,7 @@
|
|||
* @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)
|
||||
public function send (string $destination, string $text, bool $flash = false)
|
||||
{
|
||||
$uid = uniqid();
|
||||
|
||||
|
@ -125,6 +131,17 @@
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 boolean : False on error, true else
|
||||
*/
|
||||
public function test () : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 ('unknown', 'delivered', 'failed')]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue