forked from Ardakilic/Telegram-bot-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotHelper.php
320 lines (252 loc) · 9.68 KB
/
BotHelper.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Telegram-PHP-Bot - A Simple PHP app for Telegram Bots
*
* @package Telegram-Bot-PHP
* @version 1.0
* @author Arda Kilicdagi <[email protected]>
* @link https://arda.pw
*/
Class BotHelper
{
private $config;
private $route;
/**
* @param $config
* @param $route
*/
public function __construct($config, $route)
{
$this->config = $config;
$this->route = $route;
}
/**
* This method fetches all the data and the extra info of the response as an array
* @param $bot_config array Bot configuration, this is needed if the driver is set as file
* @param $response_data array this is the data that Telegram sends
* @return array Array of data that will be sent to Telegram
*/
public function fetchMessage($bot_config, $response_data)
{
if ($this->config['source'] == 'file') {
return $this->fetchMessageFromFile($bot_config, $response_data);
}
return $this->fetchMessageFromSQL($response_data);
}
/**
* Sets the endpoint for Bot API due to returned request from fetched response
* @param $type string Type of response
* @return string the Endpoint for Telegram
*/
public function getEndpoint($type)
{
switch ($type) {
case 'text':
return 'sendMessage';
break;
case 'image':
return 'sendPhoto';
break;
case 'sticker':
return 'sendSticker';
break;
case 'video':
return 'sendVideo';
break;
case 'audio':
return 'sendAudio';
break;
default:
return 'sendMessage';
break;
}
}
/**
* If the driver is SQL, this method Sets the parameters that will be sent to Telegram by Guzzle
* @param $data object the fetched data from SQL
* @param $response_data array the response data of Telegram, because we need this data if the message will be as quote
* @return array the array of data
*/
private function setParams($data, $response_data)
{
//Let's set a blank array at first
$out = [];
//All these array keys are based on the available methods:
//https://core.telegram.org/bots/api#available-methods
switch ($data->response_type) {
case 'text':
array_push($out, [
'name' => 'text',
'contents' => $data->response_data,
]);
break;
case 'image':
array_push($out,
[
'name' => 'photo',
'contents' => fopen(__DIR__ . '/assets/photo/' . $data->response_data, 'rb'),
],
[
'name' => 'caption',
'contents' => $data->response_data,
]
);
break;
case 'sticker':
array_push($out, [
'name' => 'sticker',
'contents' => fopen(__DIR__ . '/assets/sticker/' . $data->response_data, 'rb'),
]);
break;
case 'video':
array_push($out, [
'name' => 'video',
'contents' => fopen(__DIR__ . '/assets/video/' . $data->response_data, 'rb'),
]);
break;
case 'audio':
array_push($out, [
'name' => 'audio',
'contents' => fopen(__DIR__ . '/assets/audio/' . $data->response_data, 'rb'),
]);
break;
default:
array_push($out, [
'name' => 'text',
'contents' => $data->response_data,
]);
break;
}
if ($data->as_quote == 'y') {
array_push($out, [
'name' => 'reply_to_message_id',
'contents' => (string)$response_data['message']['message_id'],
]);
}
if ($data->preview_links_if_any == 'y') {
array_push($out, [
'name' => 'disable_web_page_preview',
'contents' => 'true', //fixme bool or string, would it matter?
]);
}
return $out;
}
/**
* If source is set to sql, this method runs to provide message and configuration values
* @param $response_data
* @return array
*/
private function fetchMessageFromSQL($response_data)
{
//This is the output that will be sent to Telegram as an array
$queryArray = [
[
'name' => 'chat_id',
'contents' => (string)$response_data['message']['chat']['id'],
],
];
//The Raw Request
$botRequest = $response_data['message']['text'];
$message = $this->config['default_fallback_response'];
//This is the name of the bot
$bot = $this->config['bots'][$this->route];
//The text that user has typed, let's delete the bot name
$userInput = trim(str_replace('@' . $bot, '', $botRequest));
//TODO this will be command feature, in the future
if (stripos($userInput, '/') !== false && stripos($userInput, $bot) === false) {
return false;
}
//First, let's check for multiple word occurences
$data = ORM::for_table('responses')
->where('bot_name', $bot)
->where('pattern', $userInput)
->order_by_expr($this->config['connection'][$this->config['source']]['rand_method'])
->find_one();
if ($data) {
$queryArray = array_merge($queryArray, $this->setParams($data, $response_data));
} else {
//If not found, then let's split it by spaces and trim each exploded word
$userInputArray = array_map('trim', explode(" ", $userInput));
$data = ORM::for_table('responses')
->where('bot_name', $bot)
->where_in('pattern', $userInputArray)
->order_by_expr($this->config['connection'][$this->config['source']]['rand_method'])
->find_one();
if ($data) {
$queryArray = array_merge($queryArray, $this->setParams($data, $response_data));
} else {
//If all fails, let's set the fallback message for text
array_push($queryArray, [
'name' => 'text',
'contents' => $message,
]);
}
}
return [
'type' => $data ? $data->response_type : 'text',
'data' => $queryArray,
];
}
/**
* If source is set to file, this method runs to provide message and configuration values
* @param $bot_config
* @param $response_data
* @return array Array that will be sent to Telegram
*/
private function fetchMessageFromFile($bot_config, $response_data)
{
//This is the output that will be sent to Telegram as an array
$queryArray = [
'chat_id' => (string)$response_data['message']['chat']['id'],
];
$message = $this->config['default_fallback_response'];
if (isset($bot_config['responses'])) {
//The raw request
$botRequest = $response_data['message']['text'];
//prevent the reusage
$botMessages = $bot_config['responses'];
//Set the message, and if responses are not found, fall back to default message
$message = $botMessages['fallback_response'];
//This is the name of the bot
$bot = $this->config['bots'][$this->route];
//The text that user has typed, let's delete the bot name
$userInput = trim(str_replace('@' . $bot, '', $botRequest));
//If user only says "/" it also triggers the webhook, we don't want to return anything for now
//TODO this will be command feature, in the future
if (stripos($userInput, '/') !== false && stripos($userInput, $bot) === false) {
$queryArray['text'] = $message;
return [
'type' => 'text',
'data' => $queryArray,
];
}
//First, let's check for multiple word occurences
if (array_key_exists($userInput, $botMessages)) {
$message = $botMessages[$userInput][array_rand($botMessages[$userInput], 1)];
} else {
//If not found, then let's split it by spaces
$userInputArray = explode(" ", $userInput);
//We will be checking each words one by one, first matched result will base the message
foreach ($userInputArray as $input) {
if (array_key_exists($input, $botMessages)) {
//If there are messages set, fetch a random element
$message = $botMessages[$input][array_rand($botMessages[$input], 1)];
break;
}
}
}
}
//Now let's set the message
$queryArray['text'] = $message;
if ($bot_config['as_reply']) {
$queryArray['reply_to_message_id'] = (string)$response_data['message']['message_id'];
}
if (!$bot_config['preview_links']) {
$queryArray['disable_web_page_preview'] = true;
}
return [
'type' => 'text',
'data' => $queryArray,
];
}
}