-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTG.php
More file actions
112 lines (99 loc) · 3.01 KB
/
Copy pathTG.php
File metadata and controls
112 lines (99 loc) · 3.01 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
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
class TG
{
public $token = "";
public $url = "";
public function __construct($token)
{
$this->token = $token;
$this->url = "https://api.telegram.org/" . $token;
}
public function send($id, $message, $keyboard = [], $keyboard_opt = [])
{
if (empty($keyboard_opt)) {
$keyboard_opt[0] = "inline_keyboard"; // inline_keyboard keyboard
$keyboard_opt[1] = true;
$keyboard_opt[2] = true;
}
$kbOptions = [
$keyboard_opt[0] => $keyboard,
"one_time_keyboard" => $keyboard_opt[1],
"resize_keyboard" => $keyboard_opt[2],
];
$data = [
"chat_id" => $id,
"text" => $message,
"parse_mode" => "HTML",
"disable_web_page_preview" => true,
"reply_markup" => json_encode($kbOptions),
];
return $this->request("sendMessage", $data);
}
public function editMessageText($id, $m_id, $m_text, $kb = [])
{
$data = [
"chat_id" => $id,
"message_id" => $m_id,
"parse_mode" => "HTML",
"text" => $m_text,
];
if (!empty($kb)) {
$data["reply_markup"] = json_encode([
"inline_keyboard" => $kb,
]);
}
$this->request("editMessageText", $data);
}
public function editMessageReplyMarkup($id, $m_id, $kb)
{
$data = [
"chat_id" => $id,
"message_id" => $m_id,
"reply_markup" => json_encode([
"inline_keyboard" => $kb,
]),
];
$this->request("editMessageReplyMarkup", $data);
}
public function answerCallbackQuery($cb_id, $message, $show_alert = "false")
{
$data = [
"callback_query_id" => $cb_id,
"text" => $message,
"show_alert" => $show_alert,
];
$this->request("answerCallbackQuery", $data);
}
public function sendChatAction($id, $action = "typing")
{
$data = ["chat_id" => $id, "action" => $action];
$this->request("sendChatAction", $data);
}
public function getInlineKeyBoard($data)
{
$inlineKeyboard = ["inline_keyboard" => $data];
return json_encode($inlineKeyboard);
}
private function getKeyBoard($data)
{
$keyboard = [
"keyboard" => $data,
"one_time_keyboard" => false,
"resize_keyboard" => true,
];
return json_encode($keyboard);
}
public function request($method, $data = [])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url . "/" . $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$out = json_decode(curl_exec($curl), true);
curl_close($curl);
return $out;
}
}
?>