Skip to content

Commit

Permalink
Add media functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Nov 2, 2017
1 parent c762d35 commit 132850d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/TwilioMessageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BotMan\Drivers\Twilio;

use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Attachments\Location;
use Twilio\Twiml;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
Expand Down Expand Up @@ -81,7 +83,11 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
} elseif ($message instanceof Twiml) {
$parameters['twiml'] = $message;
} elseif ($message instanceof OutgoingMessage) {
$attachment = $message->getAttachment();
$text = $message->getText();
if ($attachment instanceof Location === false && !is_null($attachment)) {
$parameters['media'] = $attachment->getUrl();
}
} else {
$text = $message;
}
Expand Down Expand Up @@ -110,6 +116,9 @@ public function sendPayload($payload)
$body .= "\n".$button['text'];
}
$message->body($body);
if (isset($payload['media'])) {
$message->media($payload['media']);
}

return Response::create((string)$response)->send();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/TwilioMessageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Twilio\Twiml;
use BotMan\BotMan\Http\Curl;
use PHPUnit_Framework_TestCase;
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -233,6 +234,21 @@ public function it_can_send_questions()
$this->assertSame($expected, $response->getContent());
}

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

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

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

/** @var Response $response */
$response = $driver->sendPayload($payload);
$expected = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL.'<Response><Message><Body>This has an attachment</Body><Media>https://botman.io/img/logo.png</Media></Message></Response>'.PHP_EOL;
$this->assertSame($expected, $response->getContent());
}

/** @test */
public function it_no_events_for_regular_messages()
{
Expand Down

0 comments on commit 132850d

Please sign in to comment.