1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-11 08:06:13 +01:00
phorge-phorge/src/applications/metamta/adapter/PhabricatorMailTwilioAdapter.php
epriestley 35f0e31ed3 Add a Twilio SMS message adapter
Summary: Ref T920. Adds a "phone number" object, an "SMS" message type, and Twilio glue.

Test Plan:
Used this test script to send myself some text messages after configuring Twilio in `cluster.mailers`.

```
<?php

require_once 'scripts/init/init-script.php';

if ($argc < 3) {
  throw new Exception('usage: test.php <number> <body>');
}
$to_number = $argv[1];
$text_body = $argv[2];

$mailers = PhabricatorMetaMTAMail::newMailers(
  array(
    'outbound' => true,
    'media' => array(
      PhabricatorMailSMSMessage::MESSAGETYPE,
    ),
  ));
if (!$mailers) {
  return new Aphront404Response();
}
$mailer = head($mailers);

$message = id(new PhabricatorMailSMSMessage())
  ->setToNumber(new PhabricatorPhoneNumber($to_number))
  ->setTextBody($text_body);

$mailer->sendMessage($message);
```

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T920

Differential Revision: https://secure.phabricator.com/D19971
2019-01-16 13:25:59 -08:00

61 lines
1.5 KiB
PHP

<?php
final class PhabricatorMailTwilioAdapter
extends PhabricatorMailAdapter {
const ADAPTERTYPE = 'twilio';
public function getSupportedMessageTypes() {
return array(
PhabricatorMailSMSMessage::MESSAGETYPE,
);
}
protected function validateOptions(array $options) {
PhutilTypeSpec::checkMap(
$options,
array(
'account-sid' => 'string',
'auth-token' => 'string',
'from-number' => 'string',
));
// Construct an object from the "from-number" to validate it.
$number = new PhabricatorPhoneNumber($options['from-number']);
}
public function newDefaultOptions() {
return array(
'account-sid' => null,
'auth-token' => null,
'from-number' => null,
);
}
public function sendMessage(PhabricatorMailExternalMessage $message) {
$account_sid = $this->getOption('account-sid');
$auth_token = $this->getOption('auth-token');
$auth_token = new PhutilOpaqueEnvelope($auth_token);
$from_number = $this->getOption('from-number');
$from_number = new PhabricatorPhoneNumber($from_number);
$to_number = $message->getToNumber();
$text_body = $message->getTextBody();
$parameters = array(
'From' => $from_number->toE164(),
'To' => $to_number->toE164(),
'Body' => $text_body,
);
$result = id(new PhabricatorTwilioFuture())
->setAccountSID($account_sid)
->setAuthToken($auth_token)
->setMethod('Messages.json', $parameters)
->setTimeout(60)
->resolve();
}
}