1 | <?php |
||
2 | |||
3 | namespace TelegramBot\Api; |
||
4 | |||
5 | use TelegramBot\Api\Types\Message; |
||
6 | |||
7 | /** |
||
8 | * @deprecated |
||
9 | * @psalm-suppress all |
||
10 | */ |
||
11 | class Botan |
||
12 | { |
||
13 | /** |
||
14 | * @var string Tracker url |
||
15 | */ |
||
16 | const BASE_URL = 'https://api.botan.io/track'; |
||
17 | |||
18 | /** |
||
19 | * CURL object |
||
20 | * |
||
21 | * @var resource |
||
22 | */ |
||
23 | protected $curl; |
||
24 | |||
25 | /** |
||
26 | * Yandex AppMetrica application api_key |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $token; |
||
31 | |||
32 | /** |
||
33 | * Botan constructor |
||
34 | * |
||
35 | * @param string $token |
||
36 | * |
||
37 | 2 | * @throws InvalidArgumentException |
|
38 | */ |
||
39 | 2 | public function __construct($token) |
|
40 | { |
||
41 | if (empty($token)) { |
||
42 | throw new InvalidArgumentException('Token should not be empty'); |
||
43 | 2 | } |
|
44 | |||
45 | $this->token = $token; |
||
46 | $this->curl = curl_init(); |
||
47 | 2 | } |
|
48 | 2 | ||
49 | 2 | /** |
|
50 | * Event tracking |
||
51 | * |
||
52 | * @param \TelegramBot\Api\Types\Message $message |
||
53 | * @param string $eventName |
||
54 | * |
||
55 | * @throws \TelegramBot\Api\Exception |
||
56 | * @throws \TelegramBot\Api\HttpException |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | public function track(Message $message, $eventName = 'Message') |
||
61 | { |
||
62 | $uid = $message->getFrom()->getId(); |
||
63 | |||
64 | $options = [ |
||
65 | CURLOPT_URL => self::BASE_URL . "?token={$this->token}&uid={$uid}&name={$eventName}", |
||
66 | CURLOPT_RETURNTRANSFER => true, |
||
67 | CURLOPT_POST => true, |
||
68 | CURLOPT_HTTPHEADER => [ |
||
69 | 'Content-Type: application/json' |
||
70 | ], |
||
71 | CURLOPT_POSTFIELDS => $message->toJson(), |
||
72 | CURLOPT_TIMEOUT => 5, |
||
73 | ]; |
||
74 | |||
75 | curl_setopt_array($this->curl, $options); |
||
76 | /** @var string $response */ |
||
77 | $response = curl_exec($this->curl); |
||
78 | /** @var array $result */ |
||
79 | $result = BotApi::jsonValidate($response, true); |
||
80 | |||
81 | BotApi::curlValidate($this->curl); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
82 | |||
83 | if ($result['status'] !== 'accepted') { |
||
84 | throw new Exception('Error Processing Request'); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 2 | /** |
|
89 | * Destructor. Close curl |
||
90 | 2 | */ |
|
91 | 2 | public function __destruct() |
|
92 | { |
||
93 | $this->curl && curl_close($this->curl); |
||
94 | } |
||
95 | } |
||
96 |