Command.php in Twilio 8
File
src/Services/Command.php
View source
<?php
namespace Drupal\twilio\Services;
use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;
use Drupal\Component\Utility\UrlHelper;
class Command {
private $sid;
private $token;
private $number;
public function __construct() {
$this->sid = $this
->getSid();
$this->token = $this
->getToken();
$this->number = $this
->getNumber();
}
public function getSid() {
if (empty($this->sid)) {
$this->sid = \Drupal::config('twilio.settings')
->get('account');
}
return $this->sid;
}
public function getToken() {
if (empty($this->token)) {
$this->token = \Drupal::config('twilio.settings')
->get('token');
}
return $this->token;
}
public function getNumber() {
if (empty($this->number)) {
$this->number = \Drupal::config('twilio.settings')
->get('number');
}
return $this->number;
}
public function messageSend(string $number, $message) {
if (is_string($message)) {
$message = [
'from' => $this->number,
'body' => $message,
];
}
$message['from'] = !empty($message['from']) ? $message['from'] : $this->number;
if (empty($message['body'])) {
throw new TwilioException("Message requires a body.");
}
if (!empty($message['mediaUrl']) && !UrlHelper::isValid($message['mediaUrl'], TRUE)) {
throw new TwilioException("Media URL must be a valid, absolute URL.");
}
$client = new Client($this->sid, $this->token);
$client->messages
->create($number, $message);
}
}
Classes
Name |
Description |
Command |
Service class for Twilio API commands. |