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