-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathBotan.php
104 lines (88 loc) · 2.89 KB
/
Botan.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
<?php
namespace YourProject;
/**
* Class Botan
* @package YourProject
*
* Usage:
*
* private $token = 'token';
*
* public function _incomingMessage($message_json) {
* $messageObj = json_decode($message_json, true);
* $messageData = $messageObj['message'];
*
* $botan = new YourProject\Botan($this->token);
* $botan->track($messageData, 'Start');
* }
*
*/
class Botan {
/**
* @var string Tracker url
*/
protected $template_uri = 'https://api.botan.io/track?token=#TOKEN&uid=#UID&name=#NAME';
/**
* @var string Url shortener url
*/
protected $shortener_uri = 'https://api.botan.io/s/?token=#TOKEN&user_ids=#UID&url=#URL';
/**
* @var string Yandex AppMetrica application api_key
*/
protected $token;
function __construct($token) {
if (empty($token) || !is_string($token)) {
throw new \Exception('Token should be a string', 2);
}
$this->token = $token;
}
public function track($message, $event_name = 'Message') {
$uid = $message['from']['id'];
$url = str_replace(
['#TOKEN', '#UID', '#NAME'],
[$this->token, $uid, urlencode($event_name)],
$this->template_uri
);
$result = $this->request($url, $message);
if ($result['status'] !== 'accepted') {
throw new \Exception('Error Processing Request', 1);
}
}
public function shortenUrl($url, $user_id) {
$request_url = str_replace(
['#TOKEN', '#UID', '#URL'],
[$this->token, $user_id, urlencode($url)],
$this->shortener_uri
);
$response = file_get_contents($request_url);
return $response === false ? $url : $response;
}
function getHTTPResponseCode($headers){
$matches = [];
$res = preg_match_all('/[\w]+\/\d+\.\d+ (\d+) [\w]+/', $headers[0], $matches);
if ($res < 1)
throw new \Exception('Incorrect response headers');
$code = intval($matches[1][0]);
return $code;
}
protected function request($url, $body) {
$options = [
'http' => [
'header' => 'Content-Type: application/json',
'method' => 'POST',
'content' => json_encode($body)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false)
throw new \Exception('Error Processing Request', 1);
$HTTPCode = $this->getHTTPResponseCode($http_response_header);
if ($HTTPCode !== 200)
throw new \Exception("Bad HTTP responce code: $HTTPCode".print_r($http_response_header, true));
$responseData = json_decode($response, true);
if ($responseData === false)
throw new \Exception('JSON decode error');
return $responseData;
}
}