-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelebot.php
More file actions
98 lines (84 loc) · 2.91 KB
/
Copy pathTelebot.php
File metadata and controls
98 lines (84 loc) · 2.91 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
92
93
94
95
96
97
98
<?php
class Telebot
{
private $update;
private $tasks = [];
public string $token;
public string $apiURL;
public function __construct(string $token)
{
$this->token = $token;
$this->apiURL = "https://api.telegram.org/bot" . $this->token;
}
private function createContext($update)
{
return new class($this->apiURL, $update)
{
public
$apiURL,
$update,
$updateId,
$message,
$messageId,
$from,
$chat,
$chatId,
$date,
$text;
public function __construct($apiURL, $update)
{
$this->apiURL = $apiURL;
$this->update = $update;
$this->updateId = $update->update_id;
if ($update->message != null) {
$this->message = $update->message;
$this->messageId = $update->message->message_id;
$this->from = $update->message->from;
$this->chat = $update->message->chat;
$this->chatId = $update->message->chat->id;
$this->date = $update->message->date;
$this->text = $update->message->text;
}
}
public function replyWithText(string $text, array $options = [])
{
$data["chat_id"] = $this->chatId;
$data["text"] = $text;
if (array_key_exists("reply_to_message_id", $options)) {
$data["reply_to_message_id"] = $options["reply_to_message_id"];
}
if (array_key_exists("parse_mode", $options)) {
if (in_array($options["parse_mode"], ["Markdown", "MarkdownV2", "HTML"])) {
$data["parse_mode"] = $options["parse_mode"];
}
}
$queries = http_build_query($data);
file_get_contents($this->apiURL . "/sendMessage?$queries");
}
};
}
public function command(string $command, callable $callback)
{
$task = [
"args" => [$command, $callback],
"do" => function (string $command, callable $callback) {
if ($this->update == null) return;
$ctx = $this->createContext($this->update);
if ($ctx->message != null) {
if (strpos($ctx->text, "/$command") === 0) {
$callback($ctx);
}
}
}
];
array_push($this->tasks, $task);
}
public function run()
{
$json = file_get_contents('php://input');
$this->update = json_decode($json);
foreach ($this->tasks as $task) {
$task["do"](...$task["args"]);
}
}
}