Skip to content

Commit

Permalink
Feature - add bot to bot communication to slack drivers (#7)
Browse files Browse the repository at this point in the history
* add protected variables for botID and botUserId to slack REST driver

* add methods to get botID and botUserId to slack REST driver

* change logic in isBot and matchesRequest methods for slack REST driver

* add botUserID to slackRTM driver and set if exists

* change isBot logic in slackRTM driver

* styleCI changes

* styleCI on comments

* set botID not botUserName

* testing WIP

* More testing WIP

* Tweaks to slack driver for tests

* making tests pass!

* styleCI tweaks

* Comma

* remove extra lines
  • Loading branch information
charliebois authored and mpociot committed Sep 13, 2017
1 parent 582d639 commit 527c1d6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
40 changes: 38 additions & 2 deletions src/SlackDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SlackDriver extends HttpDriver implements VerifiesService

protected $resultType = self::RESULT_JSON;

protected $botID;

protected $botUserID;

/**
* @param Request $request
*/
Expand All @@ -52,6 +56,9 @@ public function buildPayload(Request $request)
} else {
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
$this->event = Collection::make($this->payload->get('event'));
if (! empty($this->config['token']) && empty($this->botID)) {
$this->getBotUserId();
}
}
}

Expand All @@ -62,7 +69,7 @@ public function buildPayload(Request $request)
*/
public function matchesRequest()
{
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain'));
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain')) || ! is_null($this->event->get('bot_id'));
}

/**
Expand Down Expand Up @@ -126,7 +133,7 @@ public function getMessages()
*/
protected function isBot()
{
return $this->event->has('bot_id');
return $this->event->has('bot_id') && $this->event->get('bot_id') == $this->botID;
}

/**
Expand Down Expand Up @@ -337,4 +344,33 @@ public function verifyRequest(Request $request)
return Response::create($payload->get('challenge'))->send();
}
}

/**
* Get bot userID.
*/
private function getBotUserId()
{
$message = $this->getMessages()[0];
$botUserIdRequest = $this->sendRequest('auth.test', [], $message);
$botUserIdPayload = new ParameterBag((array) json_decode($botUserIdRequest->getContent(), true));

if ($botUserIdPayload->get('user_id')) {
$this->botUserID = $botUserIdPayload->get('user_id');
$this->getBotId();
}
}

/**
* Get bot ID.
*/
private function getBotId()
{
$message = $this->getMessages()[0];
$botUserRequest = $this->sendRequest('users.info', ['user' => $this->botUserID], $message);
$botUserPayload = (array) json_decode($botUserRequest->getContent(), true);

if ($botUserPayload['user']['is_bot']) {
$this->botID = $botUserPayload['user']['profile']['bot_id'];
}
}
}
10 changes: 8 additions & 2 deletions src/SlackRTMDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class SlackRTMDriver implements DriverInterface
/** @var string */
protected $bot_id;

/** @var string */
protected $botUserID;

const DRIVER_NAME = 'SlackRTM';

protected $file;
Expand Down Expand Up @@ -65,7 +68,10 @@ public function __construct(array $config, RealTimeClient $client)
public function connected()
{
$this->client->getAuthedUser()->then(function ($user) {
$this->bot_id = $user->getId();
$this->botUserID = $user->getId();
if (isset($user->data['is_bot']) && $user->data['is_bot']) {
$this->bot_id = $user->data['profile']['bot_id'];
}
});
}

Expand Down Expand Up @@ -192,7 +198,7 @@ public function getMessages()
*/
protected function isBot()
{
return $this->event->has('bot_id') && $this->event->get('bot_id') !== $this->bot_id;
return $this->event->has('bot_id') && $this->event->get('bot_id') == $this->bot_id;
}

/**
Expand Down
80 changes: 76 additions & 4 deletions tests/SlackDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ private function getDriver($responseData, $htmlInterface = null)
$htmlInterface = m::mock(Curl::class);
}

return new SlackDriver($request, [], $htmlInterface);
$slackConfig = ['token' => 'Foo'];
$response = new Response('{"ok": true,"url": "https:\/\/myteam.slack.com\/","team": "My Team","user": "cal","team_id": "T12345","user_id": "U0X12345"}');

$htmlInterface->shouldReceive('post')
->once()
->with('https://slack.com/api/auth.test', [], $slackConfig)
->andReturn($response);

$this->mockUserInfoEndpoint($htmlInterface);

return new SlackDriver($request, ['slack' => $slackConfig], $htmlInterface);
}

/** @test */
Expand Down Expand Up @@ -183,11 +193,13 @@ public function it_returns_the_user_object()
],
];

$response = new Response('{"ok":true,"user":{"id": "U0X12345","name": "botman","deleted": false,"color": "9f69e7","profile": {"avatar_hash": "ge3b51ca72de","status_emoji": ":mountain_railway:","status_text": "riding a train","first_name": "Bot","last_name": "Man","real_name": "Bot Man","email": "[email protected]","skype": "my-skype-name","phone": "+1 (123) 456 7890","image_24": "http:\/\/via.placeholder.com\/24","image_32": "http:\/\/via.placeholder.com\/32","image_48": "http:\/\/via.placeholder.com\/48","image_72": "http:\/\/via.placeholder.com\/72","image_192": "http:\/\/via.placeholder.com\/192","image_512": "http:\/\/via.placeholder.com\/512"},"is_admin": true, "is_owner": true,"is_primary_owner": true,"is_restricted": false,"is_ultra_restricted": false,"updated": 1490054400,"has_2fa": false,"two_factor_type": "sms"}}');

$html = m::mock(Curl::class);

$this->mockAuthTestEndpoint($html);

$response = new Response('{"ok":true,"user":{"id": "U0X12345","name": "botman","deleted": false,"color": "9f69e7","profile": {"bot_id":"foo", "avatar_hash": "ge3b51ca72de","status_emoji": ":mountain_railway:","status_text": "riding a train","first_name": "Bot","last_name": "Man","real_name": "Bot Man","email": "[email protected]","skype": "my-skype-name","phone": "+1 (123) 456 7890","image_24": "http:\/\/via.placeholder.com\/24","image_32": "http:\/\/via.placeholder.com\/32","image_48": "http:\/\/via.placeholder.com\/48","image_72": "http:\/\/via.placeholder.com\/72","image_192": "http:\/\/via.placeholder.com\/192","image_512": "http:\/\/via.placeholder.com\/512"},"is_admin": true, "is_owner": true,"is_primary_owner": true,"is_restricted": false,"is_ultra_restricted": false,"updated": 1490054400,"has_2fa": false,"two_factor_type": "sms","is_bot": true}}');

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/users.info', [], [
'token' => 'Foo',
'user' => 'U0X12345',
Expand Down Expand Up @@ -392,6 +404,10 @@ public function it_can_reply_string_messages()
];

$html = m::mock(Curl::class);

$this->mockAuthTestEndpoint($html);
$this->mockUserInfoEndpoint($html);

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -426,6 +442,9 @@ public function it_can_reply_message_objects()
];

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -460,6 +479,9 @@ public function it_can_reply_message_objects_with_image()
];

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -538,6 +560,9 @@ public function it_can_reply_questions_with_additional_button_parameters()
->addButton(Button::create('Good'));

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -577,6 +602,9 @@ public function it_can_reply_questions()
->addButton(Button::create('Good'));

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -612,6 +640,9 @@ public function it_can_reply_with_additional_parameters()
];

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -658,6 +689,9 @@ public function it_can_reply_in_threads()
];

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

$html->shouldReceive('post')
->once()
->with('https://slack.com/api/chat.postMessage', [], [
Expand Down Expand Up @@ -706,6 +740,9 @@ public function it_is_configured()
$request->shouldReceive('getContent')->andReturn('');
$htmlInterface = m::mock(Curl::class);

$this->mockAuthTestEndpoint($htmlInterface);
$this->mockUserInfoEndpoint($htmlInterface);

$driver = new SlackDriver($request, [
'slack' => [
'token' => 'Foo',
Expand All @@ -726,4 +763,39 @@ public function it_is_configured()

$this->assertFalse($driver->isConfigured());
}

/**
* Mocks the auth.test endpoint request.
*
* @param $htmlInterface
*/
private function mockAuthTestEndpoint($htmlInterface)
{
$response = new Response('{"ok": true,"url": "https:\/\/myteam.slack.com\/","team": "My Team","user": "cal","team_id": "T12345","user_id": "U0X12345"}');

$htmlInterface->shouldReceive('post')
->once()
->with('https://slack.com/api/auth.test', [], [
'token' => 'Foo',
])
->andReturn($response);
}

/**
* Mocks the users.info endpoint request.
*
* @param $htmlInterface
*/
private function mockUserInfoEndpoint($htmlInterface)
{
$response = new Response('{"ok":true,"user":{"id": "U0X12345","name": "botman","deleted": false,"color": "9f69e7","profile": {"avatar_hash": "ge3b51ca72de","status_emoji": ":mountain_railway:","status_text": "riding a train","first_name": "Bot","last_name": "Man","real_name": "Bot Man","email": "[email protected]","skype": "my-skype-name","phone": "+1 (123) 456 7890","image_24": "http:\/\/via.placeholder.com\/24","image_32": "http:\/\/via.placeholder.com\/32","image_48": "http:\/\/via.placeholder.com\/48","image_72": "http:\/\/via.placeholder.com\/72","image_192": "http:\/\/via.placeholder.com\/192","image_512": "http:\/\/via.placeholder.com\/512"},"is_admin": true, "is_owner": true,"is_primary_owner": true,"is_restricted": false,"is_ultra_restricted": false,"updated": 1490054400,"has_2fa": false,"two_factor_type": "sms", "is_bot": true, "profile":{"bot_id":"foo"}}}');

$htmlInterface->shouldReceive('post')
->once()
->with('https://slack.com/api/users.info', [], [
'token' => 'Foo',
'user' => 'U0X12345',
])
->andReturn($response);
}
}
2 changes: 1 addition & 1 deletion tests/SlackRTMDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function it_detects_bots()

$driver = $this->getDriver([
'user' => 'U0X12345',
'bot_id' => 'foo',
'bot_id' => null,
'text' => 'Hi Julia',
]);
$messages = $driver->getMessages();
Expand Down

0 comments on commit 527c1d6

Please sign in to comment.