You are here

public function TwilioController::receiveMessage in Twilio 8

Handle incoming SMS message requests.

@todo Needs Work.

1 string reference to 'TwilioController::receiveMessage'
twilio.routing.yml in ./twilio.routing.yml
twilio.routing.yml

File

src/Controller/TwilioController.php, line 18

Class

TwilioController
Default controller for the twilio module.

Namespace

Drupal\twilio\Controller

Code

public function receiveMessage() {
  if (!empty($_REQUEST['From']) && !empty($_REQUEST['Body']) && !empty($_REQUEST['ToCountry']) && twilio_command('validate')) {
    $codes = $this
      ->countryDialCodes();
    $dial_code = $this
      ->countryIsoToDialCodes($_REQUEST['ToCountry']);
    if (empty($codes[$dial_code])) {
      $this
        ->logger('Twilio')
        ->notice('A message was blocked from the country @country, due to your currrent country code settings.', [
        '@country' => $_REQUEST['ToCountry'],
      ]);
      return;
    }
    $number = SafeMarkup::checkPlain(str_replace('+' . $dial_code, '', $_REQUEST['From']));
    $number_twilio = !empty($_REQUEST['To']) ? SafeMarkup::checkPlain(str_replace('+', '', $_REQUEST['To'])) : '';
    $message = SafeMarkup::checkPlain(htmlspecialchars_decode($_REQUEST['Body'], ENT_QUOTES));

    // @todo: Support more than one media entry.
    $media = !empty($_REQUEST['MediaUrl0']) ? $_REQUEST['MediaUrl0'] : '';
    $options = [];

    // Add the receiver to the options array.
    if (!empty($_REQUEST['To'])) {
      $options['receiver'] = SafeMarkup::checkPlain($_REQUEST['To']);
    }
    $this
      ->logger('Twilio')
      ->notice('An SMS message was sent from @number containing the message "@message"', [
      '@number' => $number,
      '@message' => $message,
    ]);
    $this
      ->messageIncoming($number, $number_twilio, $message, $media, $options);
  }
}