forked from TelegramBot/Api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotan.php
More file actions
91 lines (75 loc) · 1.95 KB
/
Botan.php
File metadata and controls
91 lines (75 loc) · 1.95 KB
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
<?php
namespace TelegramBot\Api;
use TelegramBot\Api\Types\Message;
class Botan
{
/**
* @var string Tracker url
*/
const BASE_URL = 'https://api.botan.io/track';
/**
* CURL object
*
* @var
*/
protected $curl;
/**
* Yandex AppMetrica application api_key
*
* @var string
*/
protected $token;
/**
* Botan constructor
*
* @param string $token
*
* @throws \Exception
*/
public function __construct($token)
{
if (!function_exists('curl_version')) {
throw new Exception('CURL not installed');
}
if (empty($token) || !is_string($token)) {
throw new InvalidArgumentException('Token should be a string');
}
$this->token = $token;
$this->curl = curl_init();
}
/**
* Event tracking
*
* @param \TelegramBot\Api\Types\Message $message
* @param string $eventName
*
* @throws \TelegramBot\Api\Exception
* @throws \TelegramBot\Api\HttpException
*/
public function track(Message $message, $eventName = 'Message')
{
$uid = $message->getFrom()->getId();
$options = [
CURLOPT_URL => self::BASE_URL . "?token={$this->token}&uid={$uid}&name={$eventName}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => $message->toJson()
];
curl_setopt_array($this->curl, $options);
$result = BotApi::jsonValidate(curl_exec($this->curl), true);
BotApi::curlValidate($this->curl);
if ($result['status'] !== 'accepted') {
throw new Exception('Error Processing Request');
}
}
/**
* Destructor. Close curl
*/
public function __destruct()
{
$this->curl && curl_close($this->curl);
}
}