Skip to content

Commit

Permalink
Allow sending attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jan 19, 2018
1 parent ae073d2 commit cc03f9c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace BotMan\Drivers\Web;

use BotMan\BotMan\Messages\Attachments\Audio;
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Attachments\Video;
use BotMan\BotMan\Users\User;
use Illuminate\Support\Collection;
use BotMan\BotMan\Drivers\HttpDriver;
Expand All @@ -17,6 +20,11 @@ class WebDriver extends HttpDriver
{
const DRIVER_NAME = 'Web';

const ATTACHMENT_IMAGE = 'image';
const ATTACHMENT_AUDIO = 'audio';
const ATTACHMENT_VIDEO = 'video';
const ATTACHMENT_LOCATION = 'location';

/** @var OutgoingMessage[] */
protected $replies = [];

Expand All @@ -29,13 +37,17 @@ class WebDriver extends HttpDriver
/** @var array */
protected $messages = [];

/** @var array */
protected $files = [];

/**
* @param Request $request
*/
public function buildPayload(Request $request)
{
$this->payload = $request->request->all();
$this->event = Collection::make($this->payload);
$this->files = Collection::make($request->files->all());
$this->config = Collection::make($this->config->get('web', []));
}

Expand Down Expand Up @@ -80,7 +92,13 @@ public function getMessages()
if (empty($this->messages)) {
$message = $this->event->get('message');
$userId = $this->event->get('userId');
$this->messages = [new IncomingMessage($message, $userId, $userId, $this->payload)];
$sender = $this->event->get('sender', $userId);

$incomingMessage = new IncomingMessage($message, $sender, $userId, $this->payload);

$incomingMessage = $this->addAttachments($incomingMessage);

$this->messages = [$incomingMessage];
}

return $this->messages;
Expand Down Expand Up @@ -191,4 +209,46 @@ public function sendRequest($endpoint, array $parameters, IncomingMessage $match
{
// Not available with the web driver.
}

/**
* Add potential attachments to the message object.
*
* @param IncomingMessage $incomingMessage
* @return IncomingMessage
*/
protected function addAttachments($incomingMessage)
{
$attachment = $this->event->get('attachment');

if ($attachment === self::ATTACHMENT_IMAGE) {
$images = $this->files->map(function ($file) {
return new Image($this->getDataURI($file['tmp_name']));
})->values()->toArray();
$incomingMessage->setText(Image::PATTERN);
$incomingMessage->setImages($images);
} elseif ($attachment === self::ATTACHMENT_AUDIO) {
$images = $this->files->map(function ($file) {
return new Audio($this->getDataURI($file['tmp_name']));
})->values()->toArray();
$incomingMessage->setText(Audio::PATTERN);
$incomingMessage->setAudio($images);
} elseif ($attachment === self::ATTACHMENT_VIDEO) {
$images = $this->files->map(function ($file) {
return new Video($this->getDataURI($file['tmp_name']));
})->values()->toArray();
$incomingMessage->setText(Video::PATTERN);
$incomingMessage->setVideos($images);
}

return $incomingMessage;
}

/**
* @param $file
* @param string $mime
* @return string
*/
protected function getDataURI($file, $mime = '') {
return 'data: '.(function_exists('mime_content_type') ? mime_content_type($file) : $mime).';base64,'.base64_encode(file_get_contents($file));
}
}
108 changes: 108 additions & 0 deletions tests/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use BotMan\BotMan\Http\Curl;
use PHPUnit_Framework_TestCase;
use BotMan\Drivers\Web\WebDriver;
use BotMan\BotMan\Messages\Attachments\Audio;
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Attachments\Video;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use BotMan\Drivers\Facebook\Extensions\Element;
Expand Down Expand Up @@ -48,6 +50,30 @@ private function getDriver($responseData, $htmlInterface = null)

return new WebDriver($request, $config, $htmlInterface);
}
/**
* @param $responseData
* @param array $files
* @param null $htmlInterface
* @return WebDriver
*/
private function getDriverWithFiles($responseData, $files, $htmlInterface = null)
{
$request = Request::create('', 'POST', $responseData, [], $files);
if ($htmlInterface === null) {
$htmlInterface = m::mock(Curl::class);
}

$config = [
'web' => [
'matchingData' => [
'custom' => 'my-custom-string',
'driver' => 'web',
],
],
];

return new WebDriver($request, $config, $htmlInterface);
}

/** @test */
public function it_returns_the_driver_name()
Expand Down Expand Up @@ -86,6 +112,75 @@ public function it_returns_the_message_object()
$this->assertTrue(is_array($driver->getMessages()));
}

/** @test */
public function it_returns_images()
{
$driver = $this->getDriverWithFiles([
'driver' => 'web',
'userId' => '12345',
'attachment' => WebDriver::ATTACHMENT_IMAGE,
], [
'file1' => [
'name' => 'MyFile.png',
'type' => 'image/png',
'tmp_name' => __DIR__ . '/fixtures/image.png',
'size' => 1234
]
]);
/** @var IncomingMessage $message */
$message = $driver->getMessages()[0];
$images = $message->getImages();
$this->assertCount(1, $images);
$this->assertInstanceOf(Image::class, $images[0]);
$this->assertSame(Image::PATTERN, $message->getText());
}

/** @test */
public function it_returns_videos()
{
$driver = $this->getDriverWithFiles([
'driver' => 'web',
'userId' => '12345',
'attachment' => WebDriver::ATTACHMENT_VIDEO,
], [
'file1' => [
'name' => 'MyFile.png',
'type' => 'image/png',
'tmp_name' => __DIR__ . '/fixtures/video.mp4',
'size' => 1234
]
]);
/** @var IncomingMessage $message */
$message = $driver->getMessages()[0];
$videos = $message->getVideos();
$this->assertCount(1, $videos);
$this->assertInstanceOf(Video::class, $videos[0]);
$this->assertSame(Video::PATTERN, $message->getText());
}

/** @test */
public function it_returns_audio()
{
$driver = $this->getDriverWithFiles([
'driver' => 'web',
'userId' => '12345',
'attachment' => WebDriver::ATTACHMENT_AUDIO,
], [
'file1' => [
'name' => 'MyFile.png',
'type' => 'image/png',
'tmp_name' => __DIR__ . '/fixtures/audio.mp3',
'size' => 1234
]
]);
/** @var IncomingMessage $message */
$message = $driver->getMessages()[0];
$audio = $message->getAudio();
$this->assertCount(1, $audio);
$this->assertInstanceOf(Audio::class, $audio[0]);
$this->assertSame(Audio::PATTERN, $message->getText());
}

/** @test */
public function it_returns_the_message_object_by_reference()
{
Expand Down Expand Up @@ -123,6 +218,19 @@ public function it_returns_the_user_id()
$this->assertSame('12345', $driver->getMessages()[0]->getSender());
}

/** @test */
public function it_allows_custom_sender_id()
{
$driver = $this->getDriver([
'driver' => 'web',
'custom' => 'my-custom-string',
'message' => 'Hi Julia',
'userId' => '12345',
'sender' => '54321',
]);
$this->assertSame('54321', $driver->getMessages()[0]->getSender());
}

/** @test */
public function it_can_reply_string_messages()
{
Expand Down
Binary file added tests/fixtures/audio.mp3
Binary file not shown.
Binary file added tests/fixtures/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/fixtures/video.mp4
Binary file not shown.

0 comments on commit cc03f9c

Please sign in to comment.