Make the task list command use the getFilePath function
[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 getTelegram()->addCommand(
17 new class extends Command {
18 protected $name = 'searchmovies';
19 protected $description = 'Search through the movie collection';
20
21 public function handle($arguments) {
22 $top5 =(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort((f(field('similarity')),, field('similarity'))));
23 $movies = map(function($movie) use ($arguments) {
24 similar_text($arguments, substr($movie, 0, -7) ?: '', $perc);
25 return ['title' => $movie, 'similarity' => $perc];
26 })(scandir('/mnt/media/Movies'));
27
28 $this->replyWithMessage(
29 [
30 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
31 ]
32 );
33 }
34 }
35 );
36
37 getTelegram()->addCommand(
38 new class extends Command {
39 protected $name = 'chatid';
40 protected $description = 'Get the id for this chat.';
41
42 public function handle($arguments) {
43 $this->replyWithMessage([
44 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
45 ]);
46 }
47 }
48 );
49
50 getTelegram()->addCommand(
51 new class extends Command {
52 protected $name = 'mybills';
53 protected $description = 'List my bills';
54
55 public function handle($arguments) {
56 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
57 $this->replyWithChatAction(['action' => Actions::TYPING]);
58 $this->replyWithMessage([
59 'text' => implode(
60 "\n",
61 array_map(
62 function($bill) {
63 return sprintf(
64 "%s (%s): $%s each due on the %s",
65 $bill['service'],
66 $bill['id'],
67 splitBill($bill['amount']),
68 $bill['due']->format('jS \of M')
69 );
70 },
71 getMessagesFromInbox(
72 getInbox(
73 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
74 ),
75 require 'rules.php',
76 FALSE
77 )
78 ) ?: ['You have no unpaid bills! Nice one.']
79 )
80 ]);
81 }
82 }
83 );
84
85 getTelegram()->addCommand(
86 new class extends Command {
87 protected $name = 'paybill';
88 protected $description = 'Mark a bill as paid';
89
90 public function handle($arguments) {
91 if (!$arguments) {
92 $this->replyWithMessage(['text' => "I need the bill id. The /mybills command lists all your bills with the ids in brackets. Here, I'll show you..."]);
93 $this->triggerCommand('mybills');
94 }
95
96 $messages = array_values(array_filter(
97 getMessagesFromInbox(
98 getInbox(
99 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
100 ),
101 require 'rules.php',
102 FALSE
103 ),
104 function($e) use ($arguments) {
105 return $e['id'] == $arguments;
106 }
107 ));
108
109 if(!$messages || count($messages) !== 1) {
110 $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]);
111 return;
112 }
113
114 imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID);
115 $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]);
116 }
117 }
118
119 );
120
121 getTelegram()->addCommand(
122 new class extends Command {
123 protected $name = 'tasks';
124 protected $description = 'List tasks for this week';
125
126 public function handle($arguments) {
127 $dt = new DateTimeImmutable();
128 $directory = sprintf(
129 "tasks/%s/%s/%s",
130 $dt->format('Y'),
131 $dt->format('F'),
132 $dt->format('W')
133 );
134
135 $mondays = [
136 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
137 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
138 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
139 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
140 ];
141 $currentMonth = (int)(new DateTimeImmutable())->format('m');
142 $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays);
143 $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays);
144 $currentYear = (int)(new DateTimeImmutable())->format('Y');
145 $tasksForTheWeek = getTasksForTheWeek(
146 $currentWeekOfMonth,
147 (int)$dt->format('m'),
148 require 'taskMatrix.php'
149 );
150
151 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
152 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
153
154 $this->replyWithMessage([
155 'text' => ununlines([
156 getString('tasksForTheWeek'),
157 unlines(map(getStringAndCode)(array_diff($tasksForTheWeek, $completedTasks)))
158 ])
159 ]);
160 }
161 }
162 );
163
164 getTelegram()->addCommand(
165 new class extends Command {
166 protected $name = 'completetask';
167 protected $description = 'Mark a task as completed';
168
169 public function handle($arguments) {
170 $dt = new DateTimeImmutable();
171 $mondays = [
172 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
173 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
174 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
175 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
176 ];
177 $closestMonday = closest($dt->format('d'), $mondays);
178
179 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
180
181 $tasksForTheWeek = getTasksForTheWeek(
182 array_search($closestMonday, $mondays),
183 (int)$dt->format('m'),
184 require 'taskMatrix.php'
185 );
186
187 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
188
189 if (!is_dir(dirname($completedTasksFile))) {
190 mkdir(dirname($completedTasksFile), 0777, true);
191 }
192
193 if (in_array($arguments, $completedTasks)) {
194 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
195 return;
196 }
197
198 if (!in_array($arguments, $tasksForTheWeek)) {
199 $this->replyWithMessage(['text' => getString('unknownTask')]);
200 return;
201 }
202
203 file_put_contents(
204 $completedTasksFile,
205 "$arguments\n",
206 FILE_APPEND
207 );
208
209 $this->replyWithMessage(['text' => getString('taskCompleted')]);
210 }
211 }
212 );
213
214 getTelegram()->commandsHandler(true); //must come afterwards because lolzer