-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
65 lines (58 loc) · 1.73 KB
/
api.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
<?php
/**
* @package Slack
*/
namespace Plugin\Slack;
class Api extends \Prefab
{
const BASE_URL = 'https://slack.com/api/';
/**
* Call an API method
*
* @param string $method
* @param array $data
* @return \StdClass
*/
public function call($method, array $data = [])
{
$f3 = \Base::instance();
$url = self::BASE_URL . $method;
$options = [
'http' => [
'header' => "Content-Type: application/x-www-form-urlencoded\r\nAuthorization: Bearer " . $f3->get(Base::CONFIG_KEY_ACCESS_TOKEN) . "\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
];
if ($f3->get('DEBUG')) {
$log = new \Log("slack.log");
$log->write("[API] Calling {$method}: " . http_build_query($data));
}
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response);
if (isset($result->ok) && $result->ok === false) {
$log = new \Log("slack.log");
$log->write("[API] Error response: " . $response);
} elseif ($f3->get('DEBUG') && isset($result->warning)) {
$log->write("[API] Warning: " . $result->warning);
}
return $result;
}
/**
* Unfurl a chat message
*
* @param string $channel
* @param string $ts
* @param array $unfurls
* @return \StdClass
*/
public function chat_unfurl($channel, $ts, array $unfurls)
{
return $this->call('chat.unfurl', [
'channel' => $channel,
'ts' => $ts,
'unfurls' => json_encode($unfurls),
]);
}
}