5
5
*
6
6
* TelegramBot is an unofficial library for use the Telegram APIs for bot.
7
7
*
8
- * @version 1.0.1
8
+ * @version 1.0.2
9
9
* @author ParanoiaSystem
10
10
*/
11
11
12
12
namespace Telegram ;
13
13
14
14
class TelegramBot
15
15
{
16
+
16
17
function __construct ($ token , $ username ){
17
18
define ('TOKEN ' , $ token );
18
19
define ('USERNAME ' , $ username );
@@ -28,11 +29,11 @@ function getMe()
28
29
function sendMessage ($ chat_id , $ text , $ disable_web_page_preview = false , $ reply_to_message_id = false , $ reply_markup = false ){
29
30
$ arrayPost = array ('chat_id ' => $ chat_id , 'text ' => $ text );
30
31
if ($ reply_to_message_id )
31
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
32
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
32
33
if ($ disable_web_page_preview )
33
- array_push_assoc ($ arrayPost , 'disable_web_page_preview ' , $ disable_web_page_preview );
34
+ $ this -> array_push_assoc ($ arrayPost , 'disable_web_page_preview ' , $ disable_web_page_preview );
34
35
if ($ reply_markup )
35
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
36
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
36
37
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendMessage " , $ arrayPost );
37
38
return $ jsonResponse ;
38
39
}
@@ -44,69 +45,69 @@ function forwardMessage($chat_id, $from_chat_id, $message_id){
44
45
}
45
46
46
47
function sendPhoto ($ chat_id , $ photo , $ caption = false , $ reply_to_message_id = false , $ reply_markup = false ){
47
- $ arrayPost = array ('chat_id ' => $ chat_id , 'photo ' => $ photo );
48
+ $ arrayPost = array ('chat_id ' => $ chat_id , 'photo ' => $ photo );
48
49
if ($ caption )
49
- array_push_assoc ($ arrayPost , 'caption ' , $ caption );
50
+ $ this -> array_push_assoc ($ arrayPost , 'caption ' , $ caption );
50
51
if ($ reply_to_message_id )
51
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
52
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
52
53
if ($ reply_markup )
53
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
54
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
54
55
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendPhoto " , $ arrayPost );
55
56
return $ jsonResponse ;
56
57
}
57
58
58
59
function sendAudio ($ chat_id , $ audio , $ duration = 0 , $ reply_to_message_id = false , $ reply_markup = false ){
59
60
$ arrayPost = array ('chat_id ' => $ chat_id , 'audio ' => $ audio );
60
61
if ($ duration > 0 )
61
- array_push_assoc ($ arrayPost , 'duration ' , $ duration );
62
+ $ this -> array_push_assoc ($ arrayPost , 'duration ' , $ duration );
62
63
if ($ reply_to_message_id )
63
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
64
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
64
65
if ($ reply_markup )
65
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
66
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
66
67
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendAudio " , $ arrayPost );
67
68
return $ jsonResponse ;
68
69
}
69
70
70
71
function sendDocument ($ chat_id , $ document , $ reply_to_message_id = false , $ reply_markup = false ){
71
72
$ arrayPost = array ('chat_id ' => $ chat_id , 'document ' => $ document );
72
73
if ($ reply_to_message_id )
73
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
74
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
74
75
if ($ reply_markup )
75
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
76
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
76
77
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendDocument " , $ arrayPost );
77
78
return $ jsonResponse ;
78
79
}
79
80
80
81
function sendSticker ($ chat_id , $ sticker , $ reply_to_message_id = false , $ reply_markup = false ){
81
82
$ arrayPost = array ('chat_id ' => $ chat_id , 'sticker ' => $ sticker );
82
83
if ($ reply_to_message_id )
83
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
84
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
84
85
if ($ reply_markup )
85
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
86
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
86
87
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendSticker " , $ arrayPost );
87
88
return $ jsonResponse ;
88
89
}
89
90
90
91
function sendVideo ($ chat_id , $ video , $ duration = 0 , $ caption = false , $ reply_to_message_id = false , $ reply_markup = false ){
91
92
$ arrayPost = array ('chat_id ' => $ chat_id , 'video ' => $ video );
92
93
if ($ duration > 0 )
93
- array_push_assoc ($ arrayPost , 'duration ' , $ duration );
94
+ $ this -> array_push_assoc ($ arrayPost , 'duration ' , $ duration );
94
95
if ($ caption )
95
- array_push_assoc ($ arrayPost , 'caption ' , $ caption );
96
+ $ this -> array_push_assoc ($ arrayPost , 'caption ' , $ caption );
96
97
if ($ reply_to_message_id )
97
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
98
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
98
99
if ($ reply_markup )
99
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
100
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
100
101
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendVideo " , $ arrayPost );
101
102
return $ jsonResponse ;
102
103
}
103
104
104
105
function sendLocation ($ chat_id , $ latitude , $ longitude , $ reply_to_message_id = false , $ reply_markup = false ){
105
106
$ arrayPost = array ('chat_id ' => $ chat_id , 'latitude ' => $ latitude , 'longitude ' => $ longitude );
106
107
if ($ reply_to_message_id )
107
- array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
108
+ $ this -> array_push_assoc ($ arrayPost , 'reply_to_message_id ' , $ reply_to_message_id );
108
109
if ($ reply_markup )
109
- array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
110
+ $ this -> array_push_assoc ($ arrayPost , 'reply_markup ' , $ reply_markup );
110
111
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/sendLocation " , $ arrayPost );
111
112
return $ jsonResponse ;
112
113
}
@@ -120,9 +121,9 @@ function sendChatAction($chat_id, $action){
120
121
function getUserProfilePhotos ($ user_id , $ offset = false , $ limit = false ){
121
122
$ arrayPost = array ('user_id ' => $ user_id );
122
123
if ($ offset )
123
- array_push_assoc ($ arrayPost , 'offset ' , $ offset );
124
+ $ this -> array_push_assoc ($ arrayPost , 'offset ' , $ offset );
124
125
if ($ limit )
125
- array_push_assoc ($ arrayPost , 'limit ' , $ limit );
126
+ $ this -> array_push_assoc ($ arrayPost , 'limit ' , $ limit );
126
127
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/getUserProfilePhotos " , $ arrayPost );
127
128
return $ jsonResponse ;
128
129
}
@@ -131,19 +132,19 @@ function getUpdates($offset = false, $limit = false, $timeout = false)
131
132
{
132
133
$ arrayPost = array ();
133
134
if ($ offset )
134
- array_push_assoc ($ arrayPost , 'offset ' , $ offset );
135
+ $ this -> array_push_assoc ($ arrayPost , 'offset ' , $ offset );
135
136
if ($ limit )
136
- array_push_assoc ($ arrayPost , 'limit ' , $ limit );
137
+ $ this -> array_push_assoc ($ arrayPost , 'limit ' , $ limit );
137
138
if ($ timeout )
138
- array_push_assoc ($ arrayPost , 'timeout ' , $ timeout );
139
+ $ this -> array_push_assoc ($ arrayPost , 'timeout ' , $ timeout );
139
140
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/getUpdates " , $ arrayPost );
140
141
return $ jsonResponse ;
141
142
}
142
143
143
144
function setWebhook ($ url = false ){
144
145
$ arrayPost = array ();
145
146
if ($ url )
146
- array_push_assoc ($ arrayPost , 'url ' , $ url );
147
+ $ this -> array_push_assoc ($ arrayPost , 'url ' , $ url );
147
148
$ jsonResponse = $ this ->curl_call ("https://api.telegram.org/bot " . TOKEN . "/setWebhook " , $ arrayPost );
148
149
return $ jsonResponse ;
149
150
}
@@ -157,22 +158,43 @@ function test($json)
157
158
return false ;
158
159
}
159
160
160
- function array_push_assoc (&$ array , $ key , $ value ){
161
+ private function array_push_assoc (&$ array , $ key , $ value ){
161
162
$ array [$ key ] = $ value ;
162
163
}
163
164
164
- function curl_call ($ url , $ post =array ()){
165
- $ ch = curl_init ($ url );
166
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
167
- curl_setopt ($ ch , CURLOPT_USERAGENT , "ParanoiaBot 1.0 " );
168
- curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
169
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
170
- if (count ($ post )) {
171
- curl_setopt ($ ch , CURLOPT_POST , count ($ post ));
172
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ post ));
173
- }
174
- $ r = curl_exec ($ ch );
175
- curl_close ($ ch );
176
- return $ r ;
165
+ private function curl_call ($ url , $ post =array (), $ headers =array ()){
166
+ $ attachments = ['photo ' , 'sticker ' , 'audio ' , 'document ' , 'video ' ];
167
+ $ ch = curl_init ($ url );
168
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
169
+ curl_setopt ($ ch , CURLOPT_USERAGENT , "ParanoiaBot 1.0 " );
170
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
171
+ if (count ($ post )) {
172
+ curl_setopt ($ ch , CURLOPT_POST , true );
173
+
174
+ foreach ($ attachments as $ attachment ){
175
+ if (isset ($ post [$ attachment ])){
176
+ $ post [$ attachment ] = $ this ->curlFile ($ post [$ attachment ]);
177
+ break ;
178
+ }
179
+ }
180
+
181
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ post );
182
+ }
183
+ $ r = curl_exec ($ ch );
184
+ curl_close ($ ch );
185
+ return $ r ;
177
186
}
187
+
188
+ private function curlFile ($ path ){
189
+ if (is_array ($ path ))
190
+ return $ path ['file_id ' ];
191
+
192
+ $ realPath = realpath ($ path );
193
+
194
+ if (class_exists ('CURLFile ' ))
195
+ return new \CURLFile ($ realPath );
196
+
197
+ return '@ ' . $ realPath ;
198
+ }
199
+
178
200
}
0 commit comments