Skip to content

Commit 527c1d6

Browse files
charlieboismpociot
charliebois
authored andcommitted
Feature - add bot to bot communication to slack drivers (#7)
* 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
1 parent 582d639 commit 527c1d6

File tree

4 files changed

+123
-9
lines changed

4 files changed

+123
-9
lines changed

src/SlackDriver.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class SlackDriver extends HttpDriver implements VerifiesService
2727

2828
protected $resultType = self::RESULT_JSON;
2929

30+
protected $botID;
31+
32+
protected $botUserID;
33+
3034
/**
3135
* @param Request $request
3236
*/
@@ -52,6 +56,9 @@ public function buildPayload(Request $request)
5256
} else {
5357
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
5458
$this->event = Collection::make($this->payload->get('event'));
59+
if (! empty($this->config['token']) && empty($this->botID)) {
60+
$this->getBotUserId();
61+
}
5562
}
5663
}
5764

@@ -62,7 +69,7 @@ public function buildPayload(Request $request)
6269
*/
6370
public function matchesRequest()
6471
{
65-
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain'));
72+
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain')) || ! is_null($this->event->get('bot_id'));
6673
}
6774

6875
/**
@@ -126,7 +133,7 @@ public function getMessages()
126133
*/
127134
protected function isBot()
128135
{
129-
return $this->event->has('bot_id');
136+
return $this->event->has('bot_id') && $this->event->get('bot_id') == $this->botID;
130137
}
131138

132139
/**
@@ -337,4 +344,33 @@ public function verifyRequest(Request $request)
337344
return Response::create($payload->get('challenge'))->send();
338345
}
339346
}
347+
348+
/**
349+
* Get bot userID.
350+
*/
351+
private function getBotUserId()
352+
{
353+
$message = $this->getMessages()[0];
354+
$botUserIdRequest = $this->sendRequest('auth.test', [], $message);
355+
$botUserIdPayload = new ParameterBag((array) json_decode($botUserIdRequest->getContent(), true));
356+
357+
if ($botUserIdPayload->get('user_id')) {
358+
$this->botUserID = $botUserIdPayload->get('user_id');
359+
$this->getBotId();
360+
}
361+
}
362+
363+
/**
364+
* Get bot ID.
365+
*/
366+
private function getBotId()
367+
{
368+
$message = $this->getMessages()[0];
369+
$botUserRequest = $this->sendRequest('users.info', ['user' => $this->botUserID], $message);
370+
$botUserPayload = (array) json_decode($botUserRequest->getContent(), true);
371+
372+
if ($botUserPayload['user']['is_bot']) {
373+
$this->botID = $botUserPayload['user']['profile']['bot_id'];
374+
}
375+
}
340376
}

src/SlackRTMDriver.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class SlackRTMDriver implements DriverInterface
3434
/** @var string */
3535
protected $bot_id;
3636

37+
/** @var string */
38+
protected $botUserID;
39+
3740
const DRIVER_NAME = 'SlackRTM';
3841

3942
protected $file;
@@ -65,7 +68,10 @@ public function __construct(array $config, RealTimeClient $client)
6568
public function connected()
6669
{
6770
$this->client->getAuthedUser()->then(function ($user) {
68-
$this->bot_id = $user->getId();
71+
$this->botUserID = $user->getId();
72+
if (isset($user->data['is_bot']) && $user->data['is_bot']) {
73+
$this->bot_id = $user->data['profile']['bot_id'];
74+
}
6975
});
7076
}
7177

@@ -192,7 +198,7 @@ public function getMessages()
192198
*/
193199
protected function isBot()
194200
{
195-
return $this->event->has('bot_id') && $this->event->get('bot_id') !== $this->bot_id;
201+
return $this->event->has('bot_id') && $this->event->get('bot_id') == $this->bot_id;
196202
}
197203

198204
/**

tests/SlackDriverTest.php

+76-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ private function getDriver($responseData, $htmlInterface = null)
3131
$htmlInterface = m::mock(Curl::class);
3232
}
3333

34-
return new SlackDriver($request, [], $htmlInterface);
34+
$slackConfig = ['token' => 'Foo'];
35+
$response = new Response('{"ok": true,"url": "https:\/\/myteam.slack.com\/","team": "My Team","user": "cal","team_id": "T12345","user_id": "U0X12345"}');
36+
37+
$htmlInterface->shouldReceive('post')
38+
->once()
39+
->with('https://slack.com/api/auth.test', [], $slackConfig)
40+
->andReturn($response);
41+
42+
$this->mockUserInfoEndpoint($htmlInterface);
43+
44+
return new SlackDriver($request, ['slack' => $slackConfig], $htmlInterface);
3545
}
3646

3747
/** @test */
@@ -183,11 +193,13 @@ public function it_returns_the_user_object()
183193
],
184194
];
185195

186-
$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"}}');
187-
188196
$html = m::mock(Curl::class);
197+
198+
$this->mockAuthTestEndpoint($html);
199+
200+
$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}}');
201+
189202
$html->shouldReceive('post')
190-
->once()
191203
->with('https://slack.com/api/users.info', [], [
192204
'token' => 'Foo',
193205
'user' => 'U0X12345',
@@ -392,6 +404,10 @@ public function it_can_reply_string_messages()
392404
];
393405

394406
$html = m::mock(Curl::class);
407+
408+
$this->mockAuthTestEndpoint($html);
409+
$this->mockUserInfoEndpoint($html);
410+
395411
$html->shouldReceive('post')
396412
->once()
397413
->with('https://slack.com/api/chat.postMessage', [], [
@@ -426,6 +442,9 @@ public function it_can_reply_message_objects()
426442
];
427443

428444
$html = m::mock(Curl::class);
445+
$this->mockAuthTestEndpoint($html);
446+
$this->mockUserInfoEndpoint($html);
447+
429448
$html->shouldReceive('post')
430449
->once()
431450
->with('https://slack.com/api/chat.postMessage', [], [
@@ -460,6 +479,9 @@ public function it_can_reply_message_objects_with_image()
460479
];
461480

462481
$html = m::mock(Curl::class);
482+
$this->mockAuthTestEndpoint($html);
483+
$this->mockUserInfoEndpoint($html);
484+
463485
$html->shouldReceive('post')
464486
->once()
465487
->with('https://slack.com/api/chat.postMessage', [], [
@@ -538,6 +560,9 @@ public function it_can_reply_questions_with_additional_button_parameters()
538560
->addButton(Button::create('Good'));
539561

540562
$html = m::mock(Curl::class);
563+
$this->mockAuthTestEndpoint($html);
564+
$this->mockUserInfoEndpoint($html);
565+
541566
$html->shouldReceive('post')
542567
->once()
543568
->with('https://slack.com/api/chat.postMessage', [], [
@@ -577,6 +602,9 @@ public function it_can_reply_questions()
577602
->addButton(Button::create('Good'));
578603

579604
$html = m::mock(Curl::class);
605+
$this->mockAuthTestEndpoint($html);
606+
$this->mockUserInfoEndpoint($html);
607+
580608
$html->shouldReceive('post')
581609
->once()
582610
->with('https://slack.com/api/chat.postMessage', [], [
@@ -612,6 +640,9 @@ public function it_can_reply_with_additional_parameters()
612640
];
613641

614642
$html = m::mock(Curl::class);
643+
$this->mockAuthTestEndpoint($html);
644+
$this->mockUserInfoEndpoint($html);
645+
615646
$html->shouldReceive('post')
616647
->once()
617648
->with('https://slack.com/api/chat.postMessage', [], [
@@ -658,6 +689,9 @@ public function it_can_reply_in_threads()
658689
];
659690

660691
$html = m::mock(Curl::class);
692+
$this->mockAuthTestEndpoint($html);
693+
$this->mockUserInfoEndpoint($html);
694+
661695
$html->shouldReceive('post')
662696
->once()
663697
->with('https://slack.com/api/chat.postMessage', [], [
@@ -706,6 +740,9 @@ public function it_is_configured()
706740
$request->shouldReceive('getContent')->andReturn('');
707741
$htmlInterface = m::mock(Curl::class);
708742

743+
$this->mockAuthTestEndpoint($htmlInterface);
744+
$this->mockUserInfoEndpoint($htmlInterface);
745+
709746
$driver = new SlackDriver($request, [
710747
'slack' => [
711748
'token' => 'Foo',
@@ -726,4 +763,39 @@ public function it_is_configured()
726763

727764
$this->assertFalse($driver->isConfigured());
728765
}
766+
767+
/**
768+
* Mocks the auth.test endpoint request.
769+
*
770+
* @param $htmlInterface
771+
*/
772+
private function mockAuthTestEndpoint($htmlInterface)
773+
{
774+
$response = new Response('{"ok": true,"url": "https:\/\/myteam.slack.com\/","team": "My Team","user": "cal","team_id": "T12345","user_id": "U0X12345"}');
775+
776+
$htmlInterface->shouldReceive('post')
777+
->once()
778+
->with('https://slack.com/api/auth.test', [], [
779+
'token' => 'Foo',
780+
])
781+
->andReturn($response);
782+
}
783+
784+
/**
785+
* Mocks the users.info endpoint request.
786+
*
787+
* @param $htmlInterface
788+
*/
789+
private function mockUserInfoEndpoint($htmlInterface)
790+
{
791+
$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"}}}');
792+
793+
$htmlInterface->shouldReceive('post')
794+
->once()
795+
->with('https://slack.com/api/users.info', [], [
796+
'token' => 'Foo',
797+
'user' => 'U0X12345',
798+
])
799+
->andReturn($response);
800+
}
729801
}

tests/SlackRTMDriverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function it_detects_bots()
7676

7777
$driver = $this->getDriver([
7878
'user' => 'U0X12345',
79-
'bot_id' => 'foo',
79+
'bot_id' => null,
8080
'text' => 'Hi Julia',
8181
]);
8282
$messages = $driver->getMessages();

0 commit comments

Comments
 (0)