-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHipChatDriver.php
160 lines (139 loc) · 4.45 KB
/
HipChatDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace BotMan\Drivers\HipChat;
use BotMan\BotMan\Users\User;
use Illuminate\Support\Collection;
use BotMan\BotMan\Drivers\HttpDriver;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
class HipChatDriver extends HttpDriver
{
const DRIVER_NAME = 'HipChat';
protected $apiURL;
/**
* @param Request $request
*/
public function buildPayload(Request $request)
{
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
$this->event = Collection::make($this->payload->get('item'));
$this->config = Collection::make($this->config->get('hipchat', []));
}
/**
* Determine if the request is for this driver.
*
* @return bool
*/
public function matchesRequest()
{
return ! is_null($this->payload->get('webhook_id')) && $this->payload->get('event') === 'room_message';
}
/**
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message
* @return Answer
*/
public function getConversationAnswer(IncomingMessage $message)
{
return Answer::create($message->getText())->setMessage($message);
}
/**
* Retrieve the chat message.
*
* @return array
*/
public function getMessages()
{
return [
new IncomingMessage($this->event->get('message')['message'], $this->event->get('message')['from']['id'],
$this->event->get('room')['id'], $this->event),
];
}
/**
* @return bool
*/
public function isBot()
{
return false;
}
/**
* @param string|Question|IncomingMessage $message
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @param array $additionalParameters
* @return Response|null
*/
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
$parameters = array_replace_recursive([
'message_format' => 'text',
], $additionalParameters);
/*
* If we send a Question with buttons, ignore
* the text and append the question.
*/
if ($message instanceof Question) {
$parameters['message'] = $message->getText();
} elseif ($message instanceof OutgoingMessage) {
$parameters['message'] = $message->getText();
} else {
$parameters['message'] = $message;
}
$recipient = $matchingMessage->getRecipient() === '' ? $matchingMessage->getSender() : $matchingMessage->getRecipient();
$this->apiURL = Collection::make($this->config->get('urls', []))->filter(function ($url) use (
$recipient
) {
return strstr($url, 'room/'.$recipient.'/notification');
})->first();
return $parameters;
}
/**
* @param mixed $payload
* @return Response
*/
public function sendPayload($payload)
{
$headers = [
'Content-Type:application/json',
];
if (! is_null($this->apiURL)) {
return $this->http->post($this->apiURL, [], $payload, $headers, true);
}
}
/**
* @return bool
*/
public function isConfigured()
{
$urls = $this->config->get('urls');
if (is_array($urls)) {
$urls = array_filter($urls);
}
return ! empty($urls);
}
/**
* Retrieve User information.
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @return User
*/
public function getUser(IncomingMessage $matchingMessage)
{
$payload = $matchingMessage->getPayload();
return new User($payload->get('message')['from']['id'], $payload->get('message')['from']['name'], null,
$payload->get('message')['from']['mention_name']);
}
/**
* Low-level method to perform driver specific API requests.
*
* @param string $endpoint
* @param array $parameters
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @return void
*/
public function sendRequest($endpoint, array $parameters, IncomingMessage $matchingMessage)
{
//
}
}