5cab7de16c3cacbaf96014ae703019862fd05831
[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 $closestMonday = closest($dt->format('d'), $mondays);
142
143 $tasksForTheWeek = getTasksForTheWeek(
144 array_search($closestMonday, $mondays),
145 (int)$dt->format('m'),
146 require 'taskMatrix.php'
147 );
148
149 $completedTasksFile = "$directory" . "/completed.txt";
150 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
151
152 $this->replyWithMessage([
153 'text' => ununlines([
154 getString('tasksForTheWeek'),
155 unlines(map(getStringAndCode)(array_diff($tasksForTheWeek, $completedTasks)))
156 ])
157 ]);
158 }
159 }
160 );
161
162 getTelegram()->addCommand(
163 new class extends Command {
164 protected $name = 'completetask';
165 protected $description = 'Mark a task as completed';
166
167 public function handle($arguments) {
168 $dt = new DateTimeImmutable();
169 $mondays = [
170 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
171 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
172 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
173 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
174 ];
175 $closestMonday = closest($dt->format('d'), $mondays);
176
177 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays));
178
179 $tasksForTheWeek = getTasksForTheWeek(
180 array_search($closestMonday, $mondays),
181 (int)$dt->format('m'),
182 require 'taskMatrix.php'
183 );
184
185 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
186
187 if (!is_dir(dirname($completedTasksFile))) {
188 mkdir(dirname($completedTasksFile), 0777, true);
189 }
190
191 if (in_array($arguments, $completedTasks)) {
192 $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
193 return;
194 }
195
196 if (!in_array($arguments, $tasksForTheWeek)) {
197 $this->replyWithMessage(['text' => getString('unknownTask')]);
198 return;
199 }
200
201 file_put_contents(
202 $completedTasksFile,
203 "$arguments\n",
204 FILE_APPEND
205 );
206
207 $this->replyWithMessage(['text' => getString('taskCompleted')]);
208 }
209 }
210 );
211
212 getTelegram()->commandsHandler(true); //must come afterwards because lolzer