|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace DyarWeb; |
|
5
|
|
|
|
|
6
|
|
|
use DyarWeb\BotException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Base |
|
10
|
|
|
*/ |
|
11
|
|
|
class Base |
|
12
|
|
|
{ |
|
13
|
|
|
private const BASE_URL = 'https://api.telegram.org'; |
|
14
|
|
|
private const BOT_URL = '/bot'; |
|
15
|
|
|
private const FILE_URL = '/file'; |
|
16
|
|
|
|
|
17
|
|
|
private string $token; |
|
18
|
|
|
private static string $baseURL; |
|
19
|
|
|
private static string $baseFileURL; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Base constructor. |
|
23
|
|
|
* @throws BotException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->token = getenv('TOKEN'); |
|
28
|
|
|
if (!$this->token) { |
|
29
|
|
|
throw new BotException("Token can\'t be empty"); |
|
30
|
|
|
} |
|
31
|
|
|
self::$baseURL = self::BASE_URL . self::BOT_URL . $this->token . '/'; |
|
32
|
|
|
self::$baseFileURL = self::BASE_URL . self::FILE_URL . self::BOT_URL . $this->token . '/'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $method |
|
37
|
|
|
* @param array $params |
|
38
|
|
|
* @return object |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function sendRequest($method, $params) |
|
41
|
|
|
{ |
|
42
|
|
|
$res = file_get_contents(self::$baseURL . $method . '?' . http_build_query($params)); |
|
43
|
|
|
if ($res) { |
|
44
|
|
|
return json_decode($res); |
|
45
|
|
|
} else { |
|
46
|
|
|
return $res; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Use this method to receive incoming updates using long polling. |
|
52
|
|
|
* |
|
53
|
|
|
* @link https://core.telegram.org/bots/api#getupdates |
|
54
|
|
|
* |
|
55
|
|
|
* @param int|null $offset |
|
56
|
|
|
* @param int|null $limit |
|
57
|
|
|
* @param int|null $timeout |
|
58
|
|
|
* |
|
59
|
|
|
* @return object |
|
60
|
|
|
*/ |
|
61
|
|
|
public function pollUpdates($offset = null, $timeout = null, $limit = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$params = compact('offset', 'limit', 'timeout'); |
|
64
|
|
|
return static::sendRequest('getUpdates', $params); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns webhook updates sent by Telegram. |
|
69
|
|
|
* Works only if you set a webhook. |
|
70
|
|
|
* |
|
71
|
|
|
* @return object |
|
72
|
|
|
* |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getWebhookUpdates() |
|
75
|
|
|
{ |
|
76
|
|
|
return json_decode(file_get_contents('php://input')); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|