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