7457843053195198e857510d83b56181ba6c1b98
[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 if (!$buttons[0]) {
147 $this->replyWithMessage([
148 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
149 'reply_markup' => json_encode(['remove_keyboard' => true])
150 ]);
151 return;
152 }
153
154 $reply_markup = getTelegram()->replyKeyboardMarkup([
155 'keyboard' => $buttons,
156 'resize_keyboard' => true,
157 'one_time_keyboard' => true,
158 'selective' => true
159 ]);
160
161 $this->replyWithMessage(
162 [
163 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
164 'parse_mode' => 'markdown',
165 'reply_markup' => $reply_markup
166 ]
167 );
168 }
169 }
170
171 );
172
173 getTelegram()->addCommand(
174 new class extends Command {
175 protected $name = 'tasks';
176 protected $description = 'List tasks for this week';
177
178 public function handle($arguments) {
179 $dt = new DateTimeImmutable();
180 $directory = sprintf(
181 "tasks/%s/%s/%s",
182 $dt->format('Y'),
183 $dt->format('F'),
184 $dt->format('W')
185 );
186
187 $mondays = [
188 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
189 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
190 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
191 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
192 ];
193 $currentMonth = (int)(new DateTimeImmutable())->format('m');
194 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
195 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
196 $currentYear = (int)(new DateTimeImmutable())->format('Y');
197 $tasksForTheWeek = getTasksForTheWeek(
198 $currentWeekOfMonth,
199 (int)$dt->format('m'),
200 require 'taskMatrix.php'
201 );
202
203 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
204 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
205
206 $this->replyWithMessage([
207 'text' => ununlines([
208 getString('tasksForTheWeek'),
209 unlines(map(getStringAndCode)(array_diff($tasksForTheWeek, $completedTasks)))
210 ])
211 ]);
212 }
213 }
214 );
215
216 getTelegram()->addCommand(
217 new class extends Command {
218 protected $name = 'completetask';
219 protected $description = 'Mark a task as completed';
220
221 public function handle($arguments) {
222 $dt = new DateTimeImmutable();
223 $mondays = [
224 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
225 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
226 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
227 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
228 ];
229 $closestMonday = closest($dt->format('d'), $mondays);
230
231 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
232
233 $tasksForTheWeek = getTasksForTheWeek(
234 array_search($closestMonday, $mondays),
235 (int)$dt->format('m'),
236 require 'taskMatrix.php'
237 );
238
239 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
240
241 if (!is_dir(dirname($completedTasksFile))) {
242 mkdir(dirname($completedTasksFile), 0777, true);
243 }
244
245 if (in_array($arguments, $completedTasks)) {
246 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
247 return;
248 }
249
250 if (!in_array($arguments, $tasksForTheWeek)) {
251 $this->replyWithMessage(['text' => getString('unknownTask')]);
252 return;
253 }
254
255 file_put_contents(
256 $completedTasksFile,
257 "$arguments\n",
258 FILE_APPEND
259 );
260
261 $this->replyWithMessage(['text' => getString('taskCompleted')]);
262 }
263 }
264 );
265
266 getTelegram()->commandsHandler(true); //must come afterwards because lolzer