Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make attachments work with token #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SlackDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ protected function replyWithToken($message, $matchingMessage, $additionalParamet
$attachment = $message->getAttachment();
if (! is_null($attachment)) {
if ($attachment instanceof Image) {
$parameters['attachments'] = json_encode(['image_url' => $attachment->getUrl()]);
$parameters['attachments'] = json_encode([
[
'title' => $attachment->getTitle() ? $attachment->getTitle() : $attachment->getUrl(),
'image_url' => $attachment->getUrl(),
],
]);
}
}
} else {
Expand Down
42 changes: 40 additions & 2 deletions tests/SlackDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public function it_can_reply_message_objects()
}

/** @test */
public function it_can_reply_message_objects_with_image()
public function it_can_reply_message_objects_with_image_without_title()
{
$responseData = [
'event' => [
Expand All @@ -504,7 +504,7 @@ public function it_can_reply_message_objects_with_image()
'token' => 'Foo',
'channel' => 'general',
'text' => 'Test',
'attachments' => json_encode(['image_url' => 'http://image.url/foo.png']),
'attachments' => json_encode([['title' => 'http://image.url/foo.png', 'image_url' => 'http://image.url/foo.png']]),
]);

$request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]');
Expand All @@ -520,6 +520,44 @@ public function it_can_reply_message_objects_with_image()
$driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', Image::url('http://image.url/foo.png')), $message));
}

/** @test */
public function it_can_reply_message_objects_with_image_with_title()
{
$responseData = [
'event' => [
'user' => 'U0X12345',
'channel' => 'general',
'text' => 'response',
],
];

$html = m::mock(Curl::class);
$this->mockAuthTestEndpoint($html);
$this->mockUserInfoEndpoint($html);

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
'as_user' => true,
'token' => 'Foo',
'channel' => 'general',
'text' => 'Test',
'attachments' => json_encode([['title' => 'title', 'image_url' => 'http://image.url/foo.png']]),
]);

$request = m::mock(\Symfony\Component\HttpFoundation\Request::class.'[getContent]');
$request->shouldReceive('getContent')->andReturn(json_encode($responseData));

$driver = new SlackDriver($request, [
'slack' => [
'token' => 'Foo',
],
], $html);

$message = new IncomingMessage('', 'U0X12345', 'general');
$driver->sendPayload($driver->buildServicePayload(\BotMan\BotMan\Messages\Outgoing\OutgoingMessage::create('Test', (Image::url('http://image.url/foo.png'))->title('title')), $message));
}

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