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