Greatly improve adapters. Add twilio adapter for virtual numbers (no shortcode support before next version). Improve daemons exception + ERRORS handling. A lot of other little things

This commit is contained in:
osaajani 2020-04-02 01:55:55 +02:00
parent cbaa186c9e
commit 0c8fc7b3ac
17 changed files with 713 additions and 118 deletions

View file

@ -59,6 +59,15 @@ namespace adapters;
return __CLASS__;
}
/**
* Uniq name of the adapter
* It should be the classname of the adapter un snakecase
*/
public static function meta_uid() : string
{
return 'ovh_sms_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.).
@ -74,7 +83,7 @@ namespace adapters;
*/
public static function meta_description(): string
{
$callback = \descartes\Router::url('Callback', 'update_sended_status', ['adapter_name' => self::meta_name()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']);
$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 '
@ -163,6 +172,12 @@ namespace adapters;
*/
public function send(string $destination, string $text, bool $flash = false)
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
try
{
$success = true;
@ -185,39 +200,63 @@ namespace adapters;
$nb_invalid_receivers = \count(($response['invalidReceivers'] ?? []));
if ($nb_invalid_receivers > 0)
{
return false;
$response['error'] = true;
$response['error_message'] = 'Invalid receiver';
return $response;
}
$uids = $response['ids'] ?? [];
$uid = $response['ids'][0] ?? false;
if (!$uid)
{
$response['error'] = true;
$response['error_message'] = 'Cannot retrieve uid.';
return $response;
}
return $uids[0] ?? false;
$response['uid'] = $uid;
return $response;
}
catch (\Exception $e)
catch (\Throwable $t)
{
return false;
$response['error'] = true;
$response['error_message'] = $t->getMessage();
return $response;
}
}
/**
* Method called to read SMSs of the number.
*
* @return array : Array of the sms reads
* @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
{
$response = [
'error' => false,
'error_message' => null,
'smss' => [],
];
try
{
$success = true;
//If we use a sender we cannot receive response, no need to make queries
if ($this->datas['sended'])
{
return $response;
}
$endpoint = '/sms/' . $this->datas['service_name'] . '/incoming';
$uids = $this->api->get($endpoint);
if (!\is_array($uids) || !$uids)
{
return [];
return $response;
}
$received_smss = [];
foreach ($uids as $uid)
{
$endpoint = '/sms/' . $this->datas['service_name'] . '/incoming/' . $uid;
@ -228,7 +267,7 @@ namespace adapters;
continue;
}
$received_smss[] = [
$response['smss'][] = [
'at' => (new \DateTime($sms_details['creationDatetime']))->format('Y-m-d H:i:s'),
'text' => $sms_details['message'],
'origin' => $sms_details['sender'],
@ -239,11 +278,13 @@ namespace adapters;
$this->api->delete($endpoint);
}
return $received_smss;
return $response;
}
catch (\Exception $e)
catch (\Throwable $t)
{
return [];
$response['error'] = true;
$response['error_message'] = $t->getMessage();
return $response;
}
}
@ -271,7 +312,7 @@ namespace adapters;
return $success;
}
catch (\Exception $e)
catch (\Throwable $t)
{
return false;
}
@ -280,7 +321,7 @@ namespace adapters;
/**
* 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')]
* @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()
{
@ -295,16 +336,16 @@ namespace adapters;
switch ($dlr)
{
case 1:
$status = 'delivered';
$status = \models\Sended::STATUS_DELIVERED;
break;
case 2:
case 16:
$status = 'failed';
$status = \models\Sended::STATUS_FAILED;
break;
default:
$status = 'unknown';
$status = \models\Sended::STATUS_UNKNOWN;
break;
}