Skip to content

Commit

Permalink
Allow originating messages using the twilio API
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed May 30, 2018
1 parent 5226459 commit c6accd7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/TwilioDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BotMan\Drivers\Twilio;

use BotMan\BotMan\Users\User;
use Twilio\Rest\Client as Twilio;
use Illuminate\Support\Collection;
use BotMan\BotMan\Drivers\HttpDriver;
use Twilio\Security\RequestValidator;
Expand All @@ -20,6 +21,9 @@ abstract class TwilioDriver extends HttpDriver
/** @var string */
protected $signature;

/** @var Twilio */
protected $client;

/**
* @param Request $request
*/
Expand All @@ -41,6 +45,14 @@ public function getUser(IncomingMessage $matchingMessage)
return new User($matchingMessage->getSender());
}

/**
* @param Twilio $client
*/
public function setClient(Twilio $client)
{
$this->client = $client;
}

/**
* @return bool
*/
Expand Down
23 changes: 23 additions & 0 deletions src/TwilioMessageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BotMan\Drivers\Twilio;

use Twilio\Twiml;
use Twilio\Rest\Client as Twilio;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -72,6 +73,8 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
$parameters = $additionalParameters;
$text = '';

$parameters['originate'] = $matchingMessage->getRecipient() === '';
$parameters['recipient'] = $matchingMessage->getSender();
$parameters['buttons'] = [];

if ($message instanceof Question) {
Expand All @@ -97,13 +100,33 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
/**
* @param mixed $payload
* @return Response
* @throws \Twilio\Rest\Api\V2010\Account\TwilioException
*/
public function sendPayload($payload)
{
if (isset($payload['twiml'])) {
return Response::create((string) $payload['twiml'])->send();
}

if (isset($payload['originate']) && $payload['originate'] === true) {
if (! $this->client) {
$this->client = new Twilio($this->config->get('sid'), $this->config->get('token'));
}

$originatePayload = [
'from' => $this->config->get('fromNumber'),
'body' => $payload['text']
];

if (isset($payload['media'])) {
$originatePayload ['mediaUrl'] = $payload['media'];
}

$message = $this->client->messages->create($payload['recipient'], $originatePayload);

return Response::create(json_encode($message->toArray()));
}

$response = new Twiml();
$message = $response->message();

Expand Down
20 changes: 20 additions & 0 deletions stubs/twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

return [

/*
|--------------------------------------------------------------------------
| Twilio SID
|--------------------------------------------------------------------------
|
| Your Twilio account SID - this will be used when originating messages.
|
*/
'sid' => env('TWILIO_SID'),

/*
|--------------------------------------------------------------------------
| Twilio From Number
|--------------------------------------------------------------------------
|
| This number will be used when originating messages / calls.
|
*/
'fromNumber' => env('TWILIO_FROM_NUMBER'),

/*
|--------------------------------------------------------------------------
| Twilio Token
Expand Down
60 changes: 55 additions & 5 deletions tests/TwilioMessageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Mockery as m;
use Twilio\Rest\Client;
use Twilio\Twiml;
use BotMan\BotMan\Http\Curl;
use PHPUnit_Framework_TestCase;
Expand All @@ -27,7 +28,11 @@ private function getDriver($parameters = [], $htmlInterface = null)
$htmlInterface = m::mock(Curl::class);
}

return new TwilioMessageDriver($request, [], $htmlInterface);
return new TwilioMessageDriver($request, [
'twilio' => [
'fromNumber' => 'My-From-Number'
]
], $htmlInterface);
}

private function getValidDriver($htmlInterface = null)
Expand Down Expand Up @@ -144,12 +149,26 @@ public function it_can_build_payload()
{
$driver = $this->getValidDriver();

$incomingMessage = new IncomingMessage('text', '123456', '');

$message = 'string';
$payload = $driver->buildServicePayload($message, $incomingMessage);

$this->assertSame([
'originate' => true,
'recipient' => '123456',
'buttons' => [],
'text' => 'string',
], $payload);

$incomingMessage = new IncomingMessage('text', '123456', '987654');

$message = 'string';
$payload = $driver->buildServicePayload($message, $incomingMessage);

$this->assertSame([
'originate' => false,
'recipient' => '123456',
'buttons' => [],
'text' => 'string',
], $payload);
Expand All @@ -158,6 +177,8 @@ public function it_can_build_payload()
$payload = $driver->buildServicePayload($message, $incomingMessage);

$this->assertSame([
'originate' => false,
'recipient' => '123456',
'buttons' => [],
'text' => 'message object',
], $payload);
Expand All @@ -166,6 +187,8 @@ public function it_can_build_payload()
$payload = $driver->buildServicePayload($message, $incomingMessage);

$this->assertSame([
'originate' => false,
'recipient' => '123456',
'buttons' => [],
'text' => 'question object',
], $payload);
Expand All @@ -192,14 +215,41 @@ public function it_can_build_and_send_payload()
{
$driver = $this->getValidDriver();

$payload = $driver->buildServicePayload('string', new IncomingMessage('', '', ''), []);
$payload = $driver->buildServicePayload('string', new IncomingMessage('', '123', '456'), []);

/** @var Response $response */
$response = $driver->sendPayload($payload);
$expected = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.'<Response><Message><Body>string</Body></Message></Response>'.PHP_EOL;
$this->assertSame($expected, $response->getContent());
}

/** @test */
public function it_can_originate_messages()
{
$driver = $this->getValidDriver();

$mock = m::mock(\Twilio\Http\Client::class);
$mock
->shouldReceive('request')
->withArgs(function ($method, $url, $params, $data) {
return $data === [
'To' => '123',
'From' => 'My-From-Number',
'Body' => 'string'
];
})
->andReturn(new \Twilio\Http\Response(200, '{}'));

$client = new Client('foo', 'bar');
$client->setHttpClient($mock);

$driver->setClient($client);

$payload = $driver->buildServicePayload('string', new IncomingMessage('', '123', ''), []);

$driver->sendPayload($payload);
}

/** @test */
public function it_can_build_and_send_custom_twiml()
{
Expand All @@ -209,7 +259,7 @@ public function it_can_build_and_send_custom_twiml()
$message = $twiml->message();
$message->body('custom twiml');

$payload = $driver->buildServicePayload($twiml, new IncomingMessage('', '', ''), []);
$payload = $driver->buildServicePayload($twiml, new IncomingMessage('', '123', '456'), []);

/** @var Response $response */
$response = $driver->sendPayload($payload);
Expand All @@ -227,7 +277,7 @@ public function it_can_send_questions()
Button::create('Button 2')->value('2'),
]);

$payload = $driver->buildServicePayload($question, new IncomingMessage('', '', ''), []);
$payload = $driver->buildServicePayload($question, new IncomingMessage('', '123', '456'), []);

/** @var Response $response */
$response = $driver->sendPayload($payload);
Expand All @@ -242,7 +292,7 @@ public function it_can_send_image_attachments()

$message = OutgoingMessage::create('This has an attachment')->withAttachment(Image::url('https://botman.io/img/logo.png'));

$payload = $driver->buildServicePayload($message, new IncomingMessage('', '', ''), []);
$payload = $driver->buildServicePayload($message, new IncomingMessage('', '123', '456'), []);

/** @var Response $response */
$response = $driver->sendPayload($payload);
Expand Down

0 comments on commit c6accd7

Please sign in to comment.