This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Client.php
231 lines (198 loc) · 7.09 KB
/
Client.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* This class will have all the methods needed
* for the communication with the bot
* User: valizada
* Date: 27/08/15
* Time: 17:48
*/
require_once('ParentClass.php');
class Client extends ParentClass
{
private $requestUrl;
private $log;
public function __construct()
{
$this->log = new Logging();
$this->log->lfile('log.txt');
$this->requestUrl = $this->getURL();
}
public function getMe()
{
$url = $this->requestUrl . "getMe";
// TODO: test it
// TODO: add return value
$this->curlMethod($url, null);
}
public function sendMessage($chat_id, $text, $disable_web_page_preview,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendMessage";
$this->log->lwrite("url: " . $url);
$data = array(
'chat_id' => urlencode($chat_id),
'text' => urlencode($text),
'disable_web_page_preview' => urlencode($disable_web_page_preview),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function forwardMessage($chat_id, $from_chat_id, $message_id)
{
$url = $this->requestUrl . "forwardMessage";
$data = array(
'chat_id' => urlencode($chat_id),
'from_chat_id' => urlencode($from_chat_id),
'message_id' => urlencode($message_id)
);
$this->curlMethod($url, $data);
}
public function sendPhoto($chat_id, $photo, $caption,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendPhoto";
$data = array(
'chat_id' => urlencode($chat_id),
'photo' => '@' . $photo,
'caption' => urlencode($caption),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendAudio($chat_id, $audio, $duration, $performer,
$title, $reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendAudio";
$data = array(
'chat_id' => urlencode($chat_id),
'audio' => urlencode($audio),
'duration' => urlencode($duration),
'performer' => urlencode($performer),
'title' => urlencode($title),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendDocument($chat_id, $document,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendDocument";
$data = array(
'chat_id' => urlencode($chat_id),
'document' => urlencode($document),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendSticker($chat_id, $sticker,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendSticker";
$data = array(
'chat_id' => urlencode($chat_id),
'sticker' => urlencode($sticker),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendVideo($chat_id, $video, $duration, $caption,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendVideo";
$data = array(
'chat_id' => urlencode($chat_id),
'video' => urlencode($video),
'duration' => urlencode($duration),
'caption' => urlencode($caption),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendVoice($chat_id, $voice, $duration,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendVoice";
$data = array(
'chat_id' => urlencode($chat_id),
'voice' => urlencode($voice),
'duration' => urlencode($duration),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendLocation($chat_id, $latitude, $longitude,
$reply_to_message_id, $reply_markup)
{
$url = $this->requestUrl . "sendLocation";
$data = array(
'chat_id' => urlencode($chat_id),
'latitude' => urlencode($latitude),
'longitude' => urlencode($longitude),
'reply_to_message_id' => urlencode($reply_to_message_id),
'reply_markup' => urlencode($reply_markup)
);
$this->curlMethod($url, $data);
}
public function sendChatAction($chat_id, $action)
{
$url = $this->requestUrl . "sendChatAction";
$data = array(
'chat_id' => urlencode($chat_id),
'action' => urlencode($action)
);
$this->curlMethod($url, $data);
}
public function getUserProfilePhotos($user_id, $offset, $limit)
{
$url = $this->requestUrl . "getUserProfilePhotos";
$data = array(
'user_id' => urlencode($user_id),
'offset' => urlencode($offset),
'limit' => urlencode($limit)
);
$this->curlMethod($url, $data);
// TODO: add return
}
public function setWebhook($url, $certificate)
{
$requestUrl = $this->requestUrl . "setWebhook";
$data = array(
'url' => $url,
'certificate' => urlencode($certificate)
);
$this->curlMethod($requestUrl, $data);
}
private function curlMethod($url, $fields)
{
//$fields_string = "";
// url-ify the data for the POST
//foreach ($fields as $key => $value) {
// $fields_string .= $key . '=' . $value . '&';
//}
//rtrim($fields_string, '&');
// open connection
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
// set the url
curl_setopt($ch, CURLOPT_URL, $url);
// number of POST vars
curl_setopt($ch, CURLOPT_POST, count($fields));
// POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// To display result of curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute post
$result = curl_exec($ch);
$this->log->lwrite("result curl: " . $result);
echo $result."\n";
// close connection
curl_close($ch);
}
}