52e4a7535598b8694b1e12cdcf7e96d642dc0cb8
[SonOfLokstallBot.git] / src / purjolok.php
1 <?php declare(strict_types=1);
2
3 require_once('common.php');
4
5 use Telegram\Bot\Actions;
6 use Telegram\Bot\Commands\Command;
7
8 if(getTelegram()->getWebHookUpdates()->get('message') && !canChatWith(getTelegram()->getWebhookUpdates())) {
9 getTelegram()->sendMessage([
10 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
11 'text' => "Sorry, Dad says I can't talk to you."
12 ]);
13 exit(0);
14 }
15
16 $message = getTelegram()->getWebHookUpdates()->get('message') ? getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
17
18 if ($between = between(reveal($message ?? ''), '[taskid]')) {
19 $dt = new DateTimeImmutable();
20 $mondays = [
21 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
22 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
23 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
24 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
25 ];
26 $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
27 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
28 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
29 $tasksForTheWeek = getTasksForTheWeek(
30 $currentWeekOfMonth,
31 (int)$dt->format('m'),
32 require 'taskMatrix.php'
33 );
34
35 $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
36
37 if (!is_dir(dirname($completedTasksFile))) {
38 mkdir(dirname($completedTasksFile), 0777, true);
39 }
40
41 if (in_array($between, $completedTasks)) {
42 getTelegram()->sendMessage([
43 'chat_id' => $chatid,
44 'text' => getString('taskAlreadyCompleted')
45 ]);
46 return;
47 }
48
49 if (!in_array($between, $tasksForTheWeek)) {
50 getTelegram()->sendMessage([
51 'chat_id' => $chatid,
52 'text' => getString('unknownTask')
53 ]);
54 return;
55 }
56
57 file_put_contents(
58 $completedTasksFile,
59 "$between\n",
60 FILE_APPEND
61 );
62
63 getTelegram()->sendMessage([
64 'chat_id' => $chatid,
65 'text' => getString('taskCompleted'),
66 'reply_markup' => json_encode(['remove_keyboard' => true])
67 ]);
68
69 getTelegram()->sendSticker([
70 'chat_id' => $chatid,
71 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
72 ]);
73
74 if (!array_diff($tasksForTheWeek, $completedTasks)) {
75 getTelegram()->sendMessage([
76 'chat_id' => $chatid,
77 'text' => getString('heyTasksAllCompleted')
78 ]);
79 }
80 }
81
82 if ($between = between(reveal($message ?? ''), '[billid]')) {
83 $messages = array_values(array_filter(
84 getMessagesFromInbox(
85 getInbox(
86 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'
87 ),
88 require 'rules.php',
89 FALSE
90 ),
91 function($e) use ($between) {
92 return $e['id'] == $between;
93 }
94 ));
95
96 if(!$messages || count($messages) !== 1) {
97 getTelegram()->sendMessage([
98 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
99 'text' => "That doesn't look like a valid id. Use /mybills to list your bills."
100 ]);
101 return;
102 }
103
104 imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID);
105 getTelegram()->sendMessage([
106 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
107 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill.",
108 'reply_markup' => json_encode(['remove_keyboard' => true])
109 ]);
110
111 getTelegram()->sendSticker([
112 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
113 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
114 ]);
115 }
116
117 getTelegram()->addCommand(
118 new class extends Command {
119 protected $name = 'searchmovies';
120 protected $description = 'Search through the movie collection';
121
122 public function handle($arguments) {
123 $top5 =(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort((f(field('similarity')),, field('similarity'))));
124 $movies = map(function($movie) use ($arguments) {
125 similar_text($arguments, substr($movie, 0, -7) ?: '', $perc);
126 return ['title' => $movie, 'similarity' => $perc];
127 })(scandir('/mnt/media/Movies'));
128
129 $this->replyWithMessage(
130 [
131 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
132 ]
133 );
134 }
135 }
136 );
137
138 getTelegram()->addCommand(
139 new class extends Command {
140 protected $name = 'chatid';
141 protected $description = 'Get the id for this chat.';
142
143 public function handle($arguments) {
144 $this->replyWithMessage([
145 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
146 ]);
147 }
148 }
149 );
150
151 getTelegram()->addCommand(
152 new class extends Command {
153 protected $name = 'mybills';
154 protected $description = 'List my bills';
155
156 public function handle($arguments) {
157 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
158 $this->replyWithChatAction(['action' => Actions::TYPING]);
159 $this->replyWithMessage([
160 'text' => implode(
161 "\n",
162 array_map(
163 function($bill) {
164 return sprintf(
165 "%s: $%s each due on the %s",
166 $bill['service'],
167 splitBill($bill['amount']),
168 $bill['due']->format('jS \of M')
169 );
170 },
171 getMessagesFromInbox(
172 getInbox(
173 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
174 ),
175 require 'rules.php',
176 FALSE
177 )
178 ) ?: ['You have no unpaid bills! Nice one.']
179 )
180 ]);
181 }
182 }
183 );
184
185 getTelegram()->addCommand(
186 new class extends Command {
187 protected $name = 'paybill';
188 protected $description = 'Mark a bill as paid';
189
190 public function handle($arguments) {
191 $buttons = [array_values(map(function($bill) {
192 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
193 })(getMessagesFromInbox(
194 getInbox(
195 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
196 ),
197 require 'rules.php',
198 FALSE
199 )))];
200
201 if (!$buttons[0]) {
202 $this->replyWithMessage([
203 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
204 'reply_markup' => json_encode(['remove_keyboard' => true])
205 ]);
206 return;
207 }
208
209 $reply_markup = getTelegram()->replyKeyboardMarkup([
210 'keyboard' => $buttons,
211 'resize_keyboard' => true,
212 'one_time_keyboard' => true,
213 'selective' => true
214 ]);
215
216 $this->replyWithMessage(
217 [
218 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
219 'parse_mode' => 'markdown',
220 'reply_markup' => $reply_markup
221 ]
222 );
223 }
224 }
225
226 );
227
228 getTelegram()->addCommand(
229 new class extends Command {
230 protected $name = 'tasks';
231 protected $description = 'List tasks for this week';
232
233 public function handle($arguments) {
234 $dt = new DateTimeImmutable();
235 $directory = sprintf(
236 "tasks/%s/%s/%s",
237 $dt->format('Y'),
238 $dt->format('F'),
239 $dt->format('W')
240 );
241
242 $mondays = [
243 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
244 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
245 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
246 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
247 ];
248 $currentMonth = (int)(new DateTimeImmutable())->format('m');
249 $currentWeekOfMonth = closestIndex((new DateTimeImmutable())->format('d'), $mondays);
250 $currentYear = (int)(new DateTimeImmutable())->format('Y');
251 $tasksForTheWeek = getTasksForTheWeek(
252 $currentWeekOfMonth,
253 (int)$dt->format('m'),
254 require 'taskMatrix.php'
255 );
256
257 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
258 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
259
260 if (!array_diff($tasksForTheWeek, $completedTasks)) {
261 $this->replyWithMessage([
262 'text' => getString('tasksAllCompleted')
263 ]);
264 return;
265 }
266
267 $this->replyWithMessage([
268 'text' => ununlines([
269 getString('tasksForTheWeek'),
270 unlines(map(getStringAndCode)(array_diff($tasksForTheWeek, $completedTasks)))
271 ])
272 ]);
273 }
274 }
275 );
276
277 getTelegram()->addCommand(
278 new class extends Command {
279 protected $name = 'completetask';
280 protected $description = 'Mark a task as completed';
281
282 public function handle($arguments) {
283 $dt = new DateTimeImmutable();
284 $mondays = [
285 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
286 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
287 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
288 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
289 ];
290 $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
291
292 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
293
294 $tasksForTheWeek = getTasksForTheWeek(
295 $currentWeekOfMonth,
296 (int)$dt->format('m'),
297 require 'taskMatrix.php'
298 );
299
300 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
301
302 if (!array_diff($tasksForTheWeek, $completedTasks)) {
303 $this->replyWithMessage([
304 'text' => getString('tasksAllCompleted')
305 ]);
306 return;
307 }
308
309 $tasks = array_values(map(function($task) {
310 return sprintf('%s%s', hide('[taskid]' . $task . '[taskid]'), getString($task));
311 })(array_diff($tasksForTheWeek, $completedTasks)));
312 $buttons = partition((int)ceil(count($tasks)/3), $tasks);
313 $reply_markup = getTelegram()->replyKeyboardMarkup([
314 'keyboard' => $buttons,
315 'resize_keyboard' => true,
316 'one_time_keyboard' => true,
317 'selective' => true
318 ]);
319
320 $this->replyWithMessage(
321 [
322 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which task did you finish?',
323 'parse_mode' => 'markdown',
324 'reply_markup' => $reply_markup
325 ]
326 );
327 }
328 }
329 );
330
331 getTelegram()->commandsHandler(true); //must come afterwards because lolzer