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