diff --git a/src/TwilioMessageDriver.php b/src/TwilioMessageDriver.php index a6f9ed4..b3310e6 100644 --- a/src/TwilioMessageDriver.php +++ b/src/TwilioMessageDriver.php @@ -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; @@ -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; } @@ -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(); } diff --git a/tests/TwilioMessageDriverTest.php b/tests/TwilioMessageDriverTest.php index 1998467..6a2734d 100644 --- a/tests/TwilioMessageDriverTest.php +++ b/tests/TwilioMessageDriverTest.php @@ -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; @@ -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 = ''.PHP_EOL.'This has an attachmenthttps://botman.io/img/logo.png'.PHP_EOL; + $this->assertSame($expected, $response->getContent()); + } + /** @test */ public function it_no_events_for_regular_messages() {