You are here

public function Command::messageSend in Twilio 8

Send an SMS message.

Parameters

string $number: The number to send the message to.

string|array $message: Message text or an array: [ from => number body => message string mediaUrl => absolute URL ].

File

src/Services/Command.php, line 79

Class

Command
Service class for Twilio API commands.

Namespace

Drupal\twilio\Services

Code

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);
}