raspisms/adapters/TwilioVirtualNumberAdapter.php

387 lines
10 KiB
PHP
Raw Permalink Normal View History

<?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;
use Twilio\Rest\Client;
/**
2020-06-23 21:06:13 +02:00
* Twilio SMS service with a virtual number adapter.
*/
class TwilioVirtualNumberAdapter implements AdapterInterface
{
/**
* Data used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
private $data;
/**
2020-06-23 21:06:13 +02:00
* Twilio Api client.
*/
private $client;
/**
2020-06-23 21:06:13 +02:00
* Twilio virtual number to use.
*/
private $number;
/**
2020-06-23 21:06:13 +02:00
* Callback address Twilio must call on SMS status change.
*/
private $status_change_callback;
/**
* Adapter constructor, called when instanciated by RaspiSMS.
*
* @param string $number : Phone number the adapter is used for
2021-02-23 00:31:54 +01:00
* @param json string $data : JSON string of the data to configure interaction with the implemented service
*/
public function __construct(string $data)
{
$this->data = json_decode($data, true);
$this->client = new Client(
$this->data['account_sid'],
$this->data['auth_token']
);
$this->number = $this->data['number'];
$this->status_change_callback = $this->data['status_change_callback'];
}
/**
* Classname of the adapter.
*/
public static function meta_classname(): string
{
return __CLASS__;
}
/**
* Uniq name of the adapter
2020-06-23 21:06:13 +02:00
* It should be the classname of the adapter un snakecase.
*/
2020-06-23 21:06:13 +02:00
public static function meta_uid(): string
{
return 'twilio_virtual_number_adapter';
}
2021-02-23 00:31:54 +01:00
2021-01-26 19:27:30 +01:00
/**
* Should this adapter be hidden in user interface for phone creation and
2021-02-23 00:31:54 +01:00
* available to creation through API only.
2021-01-26 19:27:30 +01:00
*/
public static function meta_hidden(): bool
{
return false;
}
2022-03-28 01:54:38 +02:00
/**
* Should this adapter data be hidden after creation
* this help to prevent API credentials to other service leak if an attacker gain access to RaspiSMS through user credentials.
*/
public static function meta_hide_data(): bool
{
return false;
}
/**
* 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 'Twilio Numéro virtuel';
}
/**
* Description of the adapter.
* A short description of the service the adapter implements.
*/
public static function meta_description(): string
{
$credentials_url = 'https://www.twilio.com/console';
return '
2020-07-04 21:35:39 +02:00
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/>
2021-02-04 16:44:13 +01:00
Pour plus d\'information sur l\'utilisation de ce téléphone, reportez-vous à <a href="https://documentation.raspisms.fr/users/adapters/twilio_virtual_number.html" target="_blank">la documentation sur le téléphone "Twilio Numéro Virtuel".</a>
';
}
/**
* List of entries we want in data for the adapter.
*
* @return array : Every line is a field as an array with keys : name, title, description, required
*/
public static function meta_data_fields(): array
{
return [
[
'name' => 'account_sid',
'title' => 'Account SID',
'description' => 'Identifiant unique Twilio, trouvable sur la page d\'accueil de la console Twilio.',
'required' => true,
],
[
'name' => 'auth_token',
'title' => 'Auth Token',
'description' => 'Jeton d\'identification Twilio, trouvable sous le Account SID.',
'required' => true,
],
[
'name' => 'number',
'title' => 'Numéro de téléphone virtuel',
'description' => 'Numéro de téléphone virtuel Twilio à utiliser parmis les numéro actifs (format international), <a href="https://www.twilio.com/console/phone-numbers/incoming" target="_blank">voir la liste ici</a>.',
'required' => true,
'type' => 'phone_number',
],
[
'name' => 'status_change_callback',
'title' => 'Callback de changement de status',
'description' => 'L\'adresse que Twilio devra appeler pour signaler le changement de statut d\'un SMS. Laissez tel quel par défaut.',
'required' => true,
'default_value' => \descartes\Router::url('Callback', 'update_sended_status', ['adapter_uid' => self::meta_uid()], ['api_key' => $_SESSION['user']['api_key'] ?? '<your_api_key>']),
],
];
}
/**
* Does the implemented service support reading smss.
*/
public static function meta_support_read(): bool
{
return true;
}
/**
* Does the implemented service support updating phone status.
*/
public static function meta_support_phone_status(): 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 false;
}
/**
2021-06-17 00:51:33 +02:00
* Does the implemented service support mms reception.
*/
public static function meta_support_mms_reception(): bool
{
return false;
}
/**
2021-06-17 00:51:33 +02:00
* Does the implemented service support mms sending.
*/
public static function meta_support_mms_sending(): bool
{
return false;
}
2021-06-17 00:51:33 +02:00
public static function meta_support_inbound_call_callback(): bool
{
return false;
}
2021-06-17 00:51:33 +02:00
public static function meta_support_end_call_callback(): bool
{
return false;
}
2021-06-17 00:51:33 +02:00
public function send(string $destination, string $text, bool $flash = false, bool $mms = false, array $medias = []): array
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
try
{
$message = $this->client->messages->create(
$destination,
[
'from' => $this->number,
'body' => $text,
'statusCallback' => $this->status_change_callback,
]
);
2020-06-23 21:06:13 +02:00
if (null !== $message->errorCode)
{
$response['error'] = true;
$response['error_message'] = $message->errorMessage;
2020-06-23 21:06:13 +02:00
return $response;
}
$response['uid'] = $message->sid;
2020-06-23 21:06:13 +02:00
return $response;
}
catch (\Throwable $t)
{
$response['error'] = true;
$response['error_message'] = $t->getMessage();
2020-06-23 21:06:13 +02:00
return $response;
}
}
public function read(): array
{
$response = [
'error' => false,
'error_message' => null,
'smss' => [],
];
try
{
$messages = $this->client->messages->read([
'to' => $this->number,
], 20);
foreach ($messages as $record)
{
2020-06-23 21:06:13 +02:00
if ('inbound' !== $record->direction)
{
continue;
}
$timezone = date_default_timezone_get();
$record->dateCreated->setTimezone(new \DateTimeZone($timezone));
$response['smss'][] = [
'at' => $record->dateCreated->format('Y-m-d H:i:s'),
'text' => $record->body,
'origin' => $record->from,
];
//Remove sms to prevent double reading
$this->client->messages($record->sid)->delete();
}
return $response;
}
catch (\Throwable $t)
{
$response['error'] = true;
$response['error_message'] = $t->getMessage();
2020-06-23 21:06:13 +02:00
return $response;
}
}
/**
* Method called to verify phone status
*
* @return string : Return one phone status among 'available', 'unavailable', 'no_credit'
*/
public function check_phone_status(): string
{
return \models\Phone::STATUS_AVAILABLE;
}
public function test(): bool
{
try
{
$phone_numbers = $this->client->incomingPhoneNumbers->read(['phoneNumber' => $this->number], 20);
foreach ($phone_numbers as $record)
{
//If not the same number, return false
2020-06-23 21:06:13 +02:00
if ($record->phoneNumber !== $this->number)
{
continue;
}
return true; //Same number, its all ok we can return true
}
return false;
}
catch (\Throwable $t)
{
return false;
}
}
public static function status_change_callback()
{
$sid = $_REQUEST['MessageSid'] ?? false;
$status = $_REQUEST['MessageStatus'] ?? false;
if (!$sid || !$status)
{
return false;
}
switch ($status)
{
2020-06-23 21:06:13 +02:00
case 'delivered':
$status = \models\Sended::STATUS_DELIVERED;
break;
2021-01-14 03:32:17 +01:00
2020-06-23 21:06:13 +02:00
case 'failed':
$status = \models\Sended::STATUS_FAILED;
2020-06-23 21:06:13 +02:00
break;
2021-01-14 03:32:17 +01:00
2020-06-23 21:06:13 +02:00
default:
$status = \models\Sended::STATUS_UNKNOWN;
2020-06-23 21:06:13 +02:00
break;
}
return ['uid' => $sid, 'status' => $status];
}
2020-06-23 21:06:13 +02:00
public static function reception_callback(): array
{
return [];
}
2021-06-17 00:51:33 +02:00
public static function inbound_call_callback(): array
{
return [];
}
2021-06-17 00:51:33 +02:00
public static function end_call_callback(): array
{
return [];
}
}