1 <?php
declare(strict_types
=1);
3 require_once('common.php');
5 use Telegram\Bot\Actions
;
6 use Telegram\Bot\Commands\Command
;
8 if(getTelegram()->getWebHookUpdates()->get('message') && !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."
16 $message = getTelegram()->getWebHookUpdates()->get('message') ?
getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
18 if ($between = between(reveal($message ??
''), '[taskid]')) {
19 $dt = new DateTimeImmutable();
21 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
22 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
23 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
24 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
26 $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
27 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
28 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
29 $tasksForTheWeek = getTasksForTheWeek(
31 (int)$dt->format('m'),
32 require 'taskMatrix.php'
35 $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
37 if (!is_dir(dirname($completedTasksFile))) {
38 mkdir(dirname($completedTasksFile), 0777, true
);
41 if (in_array($between, $completedTasks)) {
42 getTelegram()->sendMessage([
44 'text' => getString('taskAlreadyCompleted')
49 if (!in_array($between, $tasksForTheWeek)) {
50 getTelegram()->sendMessage([
52 'text' => getString('unknownTask')
63 getTelegram()->sendMessage([
65 'text' => getString('taskCompleted'),
66 'reply_markup' => json_encode(['remove_keyboard' => true
])
69 getTelegram()->sendSticker([
71 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
74 if (!array_diff($tasksForTheWeek, $completedTasks)) {
75 getTelegram()->sendMessage([
77 'text' => getString('heyTasksAllCompleted')
82 if ($between = between(reveal($message ??
''), '[billid]')) {
83 $messages = array_values(array_filter(
86 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'
91 function($e) use ($between) {
92 return $e['id'] == $between;
96 if(!$messages ||
count($messages) !== 1) {
97 getTelegram()->sendMessage([
98 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
99 'text' => "That doesn't look like a valid id. Use /mybills to list your bills."
104 imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID
);
105 getTelegram()->sendMessage([
106 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
107 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill.",
108 'reply_markup' => json_encode(['remove_keyboard' => true
])
111 getTelegram()->sendSticker([
112 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'),
113 'sticker' => 'CAADBQADOwAD__7RB2i3XcCiO8HuAg'
117 getTelegram()->addCommand(
118 new class extends Command
{
119 protected $name = 'searchmovies';
120 protected $description = 'Search through the movie collection';
122 public function handle($arguments) {
123 $top5 = ∘
(unlines
, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘
(∘
f(field('similarity')), ⬄
, field('similarity'))));
124 $movies = map(function($movie) use ($arguments) {
125 similar_text($arguments, substr($movie, 0, -7) ?
: '', $perc);
126 return ['title' => $movie, 'similarity' => $perc];
127 })(scandir('/mnt/media/Movies'));
129 $this->replyWithMessage(
131 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies)
138 getTelegram()->addCommand(
139 new class extends Command
{
140 protected $name = 'chatid';
141 protected $description = 'Get the id for this chat.';
143 public function handle($arguments) {
144 $this->replyWithMessage([
145 'text' => $this->getUpdate()->get('message')->get('chat')->get('id')
151 getTelegram()->addCommand(
152 new class extends Command
{
153 protected $name = 'mybills';
154 protected $description = 'List my bills';
156 public function handle($arguments) {
157 $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]);
158 $this->replyWithChatAction(['action' => Actions
::TYPING
]);
159 $this->replyWithMessage([
165 "%s: $%s each due on the %s",
167 splitBill($bill['amount']),
168 $bill['due']->format('jS \of M')
171 getMessagesFromInbox(
173 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
178 ) ?
: ['You have no unpaid bills! Nice one.']
185 getTelegram()->addCommand(
186 new class extends Command
{
187 protected $name = 'paybill';
188 protected $description = 'Mark a bill as paid';
190 public function handle($arguments) {
191 $buttons = [array_values(map(function($bill) {
192 return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')');
193 })(getMessagesFromInbox(
195 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'
202 $this->replyWithMessage([
203 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)',
204 'reply_markup' => json_encode(['remove_keyboard' => true
])
209 $reply_markup = getTelegram()->replyKeyboardMarkup([
210 'keyboard' => $buttons,
211 'resize_keyboard' => true
,
212 'one_time_keyboard' => true
,
216 $this->replyWithMessage(
218 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?',
219 'parse_mode' => 'markdown',
220 'reply_markup' => $reply_markup
228 getTelegram()->addCommand(
229 new class extends Command
{
230 protected $name = 'tasks';
231 protected $description = 'List tasks for this week';
233 public function handle($arguments) {
234 $dt = new DateTimeImmutable();
235 $directory = sprintf(
243 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
244 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
245 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
246 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
248 $currentMonth = (int)(new DateTimeImmutable())->format('m');
249 $currentWeekOfMonth = closestIndex((new DateTimeImmutable())->format('d'), $mondays);
250 $currentYear = (int)(new DateTimeImmutable())->format('Y');
251 $tasksForTheWeek = getTasksForTheWeek(
253 (int)$dt->format('m'),
254 require 'taskMatrix.php'
257 $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth);
258 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
260 if (!array_diff($tasksForTheWeek, $completedTasks)) {
261 $this->replyWithMessage([
262 'text' => getString('tasksAllCompleted')
267 $this->replyWithMessage([
268 'text' => ununlines([
269 getString('tasksForTheWeek'),
270 unlines(map(getStringAndCode
)(array_diff($tasksForTheWeek, $completedTasks)))
277 getTelegram()->addCommand(
278 new class extends Command
{
279 protected $name = 'completetask';
280 protected $description = 'Mark a task as completed';
282 public function handle($arguments) {
283 $dt = new DateTimeImmutable();
285 (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
286 (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
287 (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
288 (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
290 $currentWeekOfMonth = closestIndex($dt->format('d'), $mondays);
292 $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), $currentWeekOfMonth);
294 $tasksForTheWeek = getTasksForTheWeek(
296 (int)$dt->format('m'),
297 require 'taskMatrix.php'
300 $completedTasks = file_exists($completedTasksFile) ?
lines(trim(file_get_contents($completedTasksFile))) : [];
302 if (!array_diff($tasksForTheWeek, $completedTasks)) {
303 $this->replyWithMessage([
304 'text' => getString('tasksAllCompleted')
309 $tasks = array_values(map(function($task) {
310 return sprintf('%s%s', hide('[taskid]' . $task . '[taskid]'), getString($task));
311 })(array_diff($tasksForTheWeek, $completedTasks)));
312 $buttons = partition((int)ceil(count($tasks)/3), $tasks);
313 $reply_markup = getTelegram()->replyKeyboardMarkup([
314 'keyboard' => $buttons,
315 'resize_keyboard' => true
,
316 'one_time_keyboard' => true
,
320 $this->replyWithMessage(
322 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which task did you finish?',
323 'parse_mode' => 'markdown',
324 'reply_markup' => $reply_markup
331 getTelegram()->commandsHandler(true
); //must come afterwards because lolzer