-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtelegram.php
More file actions
155 lines (133 loc) · 3.42 KB
/
Copy pathtelegram.php
File metadata and controls
155 lines (133 loc) · 3.42 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* A simple PHP class for telegram bot API.
* @author Egor Egorov <egor@erorrov.ru>
* @license https://raw.githubusercontent.com/erorrov/simple-telegram/master/LICENSE
*/
//namespace tg;
class Telegram{
/**
* Set bot token
* @var string
*/
private $token;
/**
* Stores data of a message (WebHook)
* @var string
*/
private $data = array();
/**
* Debug mode switch
* @var boolean
*/
private $debug = false;
/**
* Constructor
* @param string $token
*/
function __construct($token){
$this->token = $token;
$this->data = $this->getData();
}
/**
* Get an array of current message
* @return array
*/
public function getData(){
return json_decode(file_get_contents("php://input"), true);
}
/**
* Just curl_file_create()
* @return resource
*/
public function loadFile($path){
return curl_file_create($path);
}
/**
* Make an inline button. In fact, useless function but may be useful for beginners to grasp.
* @return array
*/
public function buildInlineButton(array $params){
return $params;
}
/**
* Make a button. In fact, useless function but may be useful for beginners to grasp.
* @return array
*/
public function buildReplyButton(array $params){
return $params;
}
/**
* Make an inline keyboard
* @return array
*/
public function buildInlineKeyboard(array $buttons){
return json_encode(array("inline_keyboard" => $buttons), true);
}
/**
* Make a reply keyboard
* @return array
*/
public function buildReplyKeyboard(array $buttons, array $params = array()){
return json_encode(array("keyboard" => $buttons, $params), true);
}
/**
* Make ForceReply
* @return array
*/
public function buildForceReply(array $params = array()){
return json_encode(array("force_reply" => true, $params), true);
}
/**
* To remove a reply keyboard
* @return array
*/
public function removeReplyKeyboard(array $params = array()){
return json_encode(array("remove_keyboard" => true, $params), true);
}
/**
* Make inline line. In fact, useless function but may be useful for beginners to grasp.
* @return array
*/
public function buildInlineLine(array $params){
return $params;
}
/**
* To build inline query result
* @return array
*/
public function buildInlineQueryResult(array $params){
return json_encode($params);
}
/**
* Debug mode
*/
public function debug($chat_id = 0) {
$this->debug = ($chat_id == 0 ? false : $chat_id);
}
/**
* Make api request
* @return array
*/
public function sendRequest($method, array $params = array(), $debug = false){
$ch = curl_init();
$url = "https://api.telegram.org/bot".$this->token."/".$method;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(!$debug && $this->debug){
$debugparams = array(
"chat_id" => $this->debug,
"text" => "<b>[Debug info]</b>\n<b>Parameters:</b>\n<pre>".print_r($params, true)."</pre>\n<b>Result:</b>\n<pre>".print_r(json_decode($result), true)."</pre>",
"parse_mode" => "HTML"
);
$this->sendRequest("sendMessage", $debugparams, true);
}
return json_decode($result, true);
}
}
?>