Skip to content

Commit 1dcd7bc

Browse files
committed
Add Service Provider
Add ability to receive file attachments
1 parent 261f882 commit 1dcd7bc

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"botman/botman": "~2.0"
2020
},
2121
"require-dev": {
22+
"botman/studio-addons": "~1.0",
23+
"illuminate/contracts": "~5.5.0",
2224
"phpunit/phpunit": "~5.0",
2325
"mockery/mockery": "dev-master",
2426
"botman/driver-facebook": "dev-master",
@@ -37,5 +39,12 @@
3739
"scripts": {
3840
"test": "vendor/bin/phpunit",
3941
"cs": "php-cs-fixer fix"
42+
},
43+
"extra": {
44+
"laravel": {
45+
"providers": [
46+
"BotMan\\Drivers\\Web\\Providers\\WebServiceProvider"
47+
]
48+
}
4049
}
4150
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace BotMan\Drivers\Web\Providers;
4+
5+
use BotMan\Drivers\Web\WebDriver;
6+
use Illuminate\Support\ServiceProvider;
7+
use BotMan\BotMan\Drivers\DriverManager;
8+
use BotMan\Studio\Providers\StudioServiceProvider;
9+
10+
class WebServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Perform post-registration booting of services.
14+
*
15+
* @return void
16+
*/
17+
public function boot()
18+
{
19+
if (! $this->isRunningInBotManStudio()) {
20+
$this->loadDrivers();
21+
22+
$this->publishes([
23+
__DIR__.'/../../stubs/web.php' => config_path('botman/web.php'),
24+
]);
25+
26+
$this->mergeConfigFrom(__DIR__.'/../../stubs/web.php', 'botman.web');
27+
}
28+
}
29+
30+
/**
31+
* Load BotMan drivers.
32+
*/
33+
protected function loadDrivers()
34+
{
35+
DriverManager::loadDriver(WebDriver::class);
36+
}
37+
38+
/**
39+
* @return bool
40+
*/
41+
protected function isRunningInBotManStudio()
42+
{
43+
return class_exists(StudioServiceProvider::class);
44+
}
45+
}

src/WebDriver.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BotMan\Drivers\Web;
44

5+
use BotMan\BotMan\Messages\Attachments\File;
56
use BotMan\BotMan\Users\User;
67
use Illuminate\Support\Collection;
78
use BotMan\BotMan\Drivers\HttpDriver;
@@ -24,6 +25,7 @@ class WebDriver extends HttpDriver
2425
const ATTACHMENT_IMAGE = 'image';
2526
const ATTACHMENT_AUDIO = 'audio';
2627
const ATTACHMENT_VIDEO = 'video';
28+
const ATTACHMENT_FILE = 'file';
2729
const ATTACHMENT_LOCATION = 'location';
2830

2931
/** @var OutgoingMessage[] */
@@ -234,7 +236,7 @@ protected function addAttachments($incomingMessage)
234236
$incomingMessage->setText(Image::PATTERN);
235237
$incomingMessage->setImages($images);
236238
} elseif ($attachment === self::ATTACHMENT_AUDIO) {
237-
$images = $this->files->map(function ($file) {
239+
$audio = $this->files->map(function ($file) {
238240
if ($file instanceof UploadedFile) {
239241
$path = $file->getRealPath();
240242
} else {
@@ -244,9 +246,9 @@ protected function addAttachments($incomingMessage)
244246
return new Audio($this->getDataURI($path));
245247
})->values()->toArray();
246248
$incomingMessage->setText(Audio::PATTERN);
247-
$incomingMessage->setAudio($images);
249+
$incomingMessage->setAudio($audio);
248250
} elseif ($attachment === self::ATTACHMENT_VIDEO) {
249-
$images = $this->files->map(function ($file) {
251+
$videos = $this->files->map(function ($file) {
250252
if ($file instanceof UploadedFile) {
251253
$path = $file->getRealPath();
252254
} else {
@@ -256,7 +258,19 @@ protected function addAttachments($incomingMessage)
256258
return new Video($this->getDataURI($path));
257259
})->values()->toArray();
258260
$incomingMessage->setText(Video::PATTERN);
259-
$incomingMessage->setVideos($images);
261+
$incomingMessage->setVideos($videos);
262+
} elseif ($attachment === self::ATTACHMENT_FILE) {
263+
$files = $this->files->map(function ($file) {
264+
if ($file instanceof UploadedFile) {
265+
$path = $file->getRealPath();
266+
} else {
267+
$path = $file['tmp_name'];
268+
}
269+
270+
return new File($this->getDataURI($path));
271+
})->values()->toArray();
272+
$incomingMessage->setText(File::PATTERN);
273+
$incomingMessage->setFiles($files);
260274
}
261275

262276
return $incomingMessage;

tests/WebDriverTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use BotMan\BotMan\Messages\Attachments\File;
56
use Mockery as m;
67
use BotMan\BotMan\Http\Curl;
78
use PHPUnit_Framework_TestCase;
@@ -182,6 +183,29 @@ public function it_returns_audio()
182183
$this->assertSame(Audio::PATTERN, $message->getText());
183184
}
184185

186+
/** @test */
187+
public function it_returns_files()
188+
{
189+
$driver = $this->getDriverWithFiles([
190+
'driver' => 'web',
191+
'userId' => '12345',
192+
'attachment' => WebDriver::ATTACHMENT_FILE,
193+
], [
194+
'file1' => [
195+
'name' => 'MyFile.png',
196+
'type' => 'image/png',
197+
'tmp_name' => __DIR__.'/fixtures/example.md',
198+
'size' => 1234,
199+
],
200+
]);
201+
/** @var IncomingMessage $message */
202+
$message = $driver->getMessages()[0];
203+
$files = $message->getFiles();
204+
$this->assertCount(1, $files);
205+
$this->assertInstanceOf(File::class, $files[0]);
206+
$this->assertSame(File::PATTERN, $message->getText());
207+
}
208+
185209
/** @test */
186210
public function it_returns_the_message_object_by_reference()
187211
{

0 commit comments

Comments
 (0)