-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
288 lines (220 loc) · 8.35 KB
/
helper.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class Helper
{
public $host = '';
public $admin_telegram_user_id = '';
public $customer_telegram_user_id = '';
public $admin_service_port = '';
public $customer_service_port = '';
public $log_file = '';
public function __construct()
{
$this->admin_telegram_user_id = ADMIN_ID;
$this->customer_telegram_user_id = CUSTOMER_ID;
$this->admin_service_port = ADMIN_PORT;
$this->customer_service_port = CUSTOMER_PORT;
$this->host = HOST;
$this->log_file = LOG_PATH;
}
public function traceLog($log_text, $error = false)
{
// create a log channel
$log_text = print_r($log_text, true);
$log = new Logger('name');
$log->pushHandler(new StreamHandler($this->log_file, Logger::WARNING));
if (!$error)
$log->addWarning($log_text);
else
$log->addError($log_text);
}
public function sanitize_chat_topic($chat_topic)
{
$chat_topic = str_replace(' ', '_', $chat_topic);
return str_replace('#', '@', $chat_topic);
}
public function create_group_chat($data)
{
try {
if (!isset($data['chat_topic']))
return false;
$users = '';
$chat_topic = $data['chat_topic'];
$chat_topic = $this->safe_string($chat_topic);
$result['success'] = false;
if (isset($data['telegram_user_ids']))
foreach ($data['telegram_user_ids'] as $user_id)
$users .= ' user#' . $user_id;
/* Addal users to the group*/
$users .= ' user#' . $this->admin_telegram_user_id;
$users .= ' user#' . $this->customer_telegram_user_id;
$command = "create_group_chat \"$chat_topic\"" . $users;
$output = $this->run_command($command, $this->admin_service_port);
if ($output && isset($output->result) && strtolower($output->result) == 'success')
return $output;
return false;
}
catch (Exception $e) {
$this->traceLog($e->getMessage());
}
}
public function safe_string($str)
{
return str_replace(array(
'"',
"\n"
), array(
'\"',
"\\n"
), $str);
}
public function send_message_to_user($user_id, $message, $port)
{
$message = $this->safe_string($message);
return $this->run_command("msg user#" . $user_id . " \"" . $message . "\"", $port);
}
public function chat_info($chat_id, $port)
{
$message = $this->safe_string($message);
return $this->run_command("msg user#" . $user_id . " \"" . $message . "\"", $port);
}
public function chat_add_user($chat_id, $user_id, $port)
{
return $this->run_command("chat_add_user chat#" . $chat_id . " user#" . $user_id, $port);
}
public function send_message_to_group($chat_id, $message, $port)
{
$message = $this->safe_string($message);
return $this->run_command("msg chat#" . $chat_id . " \"" . $message . "\"", $port);
}
public function load_photo($msg_id, $port)
{
return $this->run_command("load_photo $msg_id", $port);
}
public function download_file($data_obj, $port)
{
$download_info = $this->load_photo($data_obj->id, $port);
if ($download_info && isset($download_info->event) && $download_info->event == 'download') {
$file_path = $download_info->result;
if (file_exists($file_path)) {
return $file_path;
}
}
return false;
}
public function send_welcome_message($user_info, $verify = true, $verification_code = null)
{
$welcome_message = "Hi " . $user_info->last_name . ", You've successfully integrated your Telegram Account ...";
if ($verify) {
$welcome_message .= " Verification Code " . $verification_code;
}
return $this->send_message_to_user($user_info->id, $welcome_message, $this->admin_service_port);
}
public function incoming_message($data_obj)
{
if (isset($data_obj->to) && isset($data_obj->from)) {
$to = $data_obj->to;
$from = $data_obj->from;
$file = false;
$message = '';
if (isset($data_obj->text))
$message = $data_obj->text;
$chat_id = $to->id;
$this->traceLog($data_obj);
/*
Here you can write the functions to handle messages from Telegram and
send back to client browser using socket.io/ajax
*/
}
}
public function get_chat_id($chat_subject)
{
try {
$chat_subject = str_replace(' ', '_', $chat_subject);
$chat_subject = str_replace('#', '@', $chat_subject);
$output = $this->run_command('chat_info ' . $chat_subject, $this->admin_service_port);
if ($output && isset($output->type) && $output->type == 'chat') {
return $output->id;
}
}
catch (Exception $e) {
$this->traceLog('get_chat_id error ' . $e->getMessage());
}
}
public function safe_feof($fp, &$start = NULL)
{
$start = microtime(true);
return feof($fp);
}
public function run_command($cmd, $port)
{
try {
$fp = fsockopen($this->host, $port, $errno, $errstr, 10);
$output = '';
$timeout = 30;
$start = NULL;
if ($fp) {
fwrite($fp, $cmd . "\r\n");
while (!$this->safe_feof($fp, $start) && (microtime(true) - $start) < $timeout) {
$output = fread($fp, 2048);
break;
}
fclose($fp);
return $this->sanitize_output($output);
} else {
$this->traceLog('Telegram fsockopen error');
}
}
catch (Exception $e) {
$this->traceLog('run_command ' . $e->getMessage());
}
}
public function add_contact($phone, $first_name, $last_name = '')
{
try {
$output = $this->run_command("add_contact \"$phone\" \"$first_name\" \"$last_name\"", $this->admin_service_port);
return $output;
}
catch (Exception $e) {
$this->traceLog('add_contact ' . $e->getMessage());
}
}
public function send_photo_to_group($chat_id, $file_path, $port)
{
try {
$file_path = PATH_APP . $file_path;
if (file_exists($file_path)) {
$output = $this->run_command("send_photo chat#" . $chat_id . " \"$file_path\"", $port);
return $output;
}
}
catch (Exception $e) {
$this->traceLog('send_photo_to_group ' . $e->getMessage());
}
}
public function send_photo($user_id, $file_path, $port)
{
try {
$file_path = PATH_APP . $file_path;
if (file_exists($file_path)) {
$output = $this->run_command("send_photo user#" . $user_id . " \"$file_path\"", $port);
return $output;
}
}
catch (Exception $e) {
$this->traceLog('send_photo ' . $e->getMessage());
}
}
public function sanitize_output($output)
{
$output = explode("\n", $output);
if (stripos($output[0], "ANSWER") !== false)
unset($output[0]);
$output = implode("\n", $output);
$output = trim($output);
$output = json_decode($output);
return $output;
}
}
?>