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