getWebhookUpdates())) { getTelegram()->sendMessage([ 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'), 'text' => "Sorry, Dad says I can't talk to you." ]); exit(0); } getTelegram()->addCommand( new class extends Command { protected $name = 'searchmovies'; protected $description = 'Search through the movie collection'; public function handle($arguments) { $top5 = ∘(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘(∘f(field('similarity')), ⬄, field('similarity')))); $movies = map(function($movie) use ($arguments) { similar_text($arguments, substr($movie, 0, -7) ?: '', $perc); return ['title' => $movie, 'similarity' => $perc]; })(scandir('/mnt/media/Movies')); $this->replyWithMessage( [ 'text' => "Here are the most similar movies titles I could find...\n\n" . $top5($movies) ] ); } } ); getTelegram()->addCommand( new class extends Command { protected $name = 'chatid'; protected $description = 'Get the id for this chat.'; public function handle($arguments) { $this->replyWithMessage([ 'text' => $this->getUpdate()->get('message')->get('chat')->get('id') ]); } } ); getTelegram()->addCommand( new class extends Command { protected $name = 'mybills'; protected $description = 'List my bills'; public function handle($arguments) { $this->replyWithMessage(['text' => 'Fetching ' . getMessageSenderDisplayName($this->getUpdate()) . "'s unpaid bills. Just a sec ..."]); $this->replyWithChatAction(['action' => Actions::TYPING]); $this->replyWithMessage([ 'text' => implode( "\n", array_map( function($bill) { return sprintf( "%s (%s): $%s each due on the %s", $bill['service'], $bill['id'], splitBill($bill['amount']), $bill['due']->format('jS \of M') ); }, getMessagesFromInbox( getInbox( 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay' ), require 'rules.php', FALSE ) ) ?: ['You have no unpaid bills! Nice one.'] ) ]); } } ); getTelegram()->addCommand( new class extends Command { protected $name = 'paybill'; protected $description = 'Mark a bill as paid'; public function handle($arguments) { if (!$arguments) { $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..."]); $this->triggerCommand('mybills'); } $messages = array_values(array_filter( getMessagesFromInbox( getInbox( 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay' ), require 'rules.php', FALSE ), function($e) use ($arguments) { return $e['id'] == $arguments; } )); if(!$messages || count($messages) !== 1) { $this->replyWithMessage(['text' => "That doesn't look like a valid id. Use /mybills to list your bills."]); return; } imap_delete(getInbox('Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay'), $messages[0]['uid'], FT_UID); $this->replyWithMessage(['text' => "I marked " . getMessageSenderDisplayName($this->getUpdate()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!"]); } } ); getTelegram()->addCommand( new class extends Command { protected $name = 'tasks'; protected $description = 'List tasks for this week'; public function handle($arguments) { $dt = new DateTimeImmutable(); $directory = sprintf( "tasks/%s/%s/%s", $dt->format('Y'), $dt->format('F'), $dt->format('W') ); $mondays = [ (int)(new DateTimeImmutable('first monday of this month'))->format('d'), (int)(new DateTimeImmutable('second monday of this month'))->format('d'), (int)(new DateTimeImmutable('third monday of this month'))->format('d'), (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'), ]; $closestMonday = closest($dt->format('d'), $mondays); $tasksForTheWeek = getTasksForTheWeek( array_search($closestMonday, $mondays), (int)$dt->format('m'), require 'taskMatrix.php' ); $completedTasksFile = "$directory" . "/completed.txt"; $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; $this->replyWithMessage([ 'text' => ununlines([ getString('tasksForTheWeek'), unlines(map(getStringAndCode)(array_diff($tasksForTheWeek, $completedTasks))) ]) ]); } } ); getTelegram()->addCommand( new class extends Command { protected $name = 'completetask'; protected $description = 'Mark a task as completed'; public function handle($arguments) { $dt = new DateTimeImmutable(); $mondays = [ (int)(new DateTimeImmutable('first monday of this month'))->format('d'), (int)(new DateTimeImmutable('second monday of this month'))->format('d'), (int)(new DateTimeImmutable('third monday of this month'))->format('d'), (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'), ]; $closestMonday = closest($dt->format('d'), $mondays); $completedTasksFile = getFilePathForWeek((int)$dt->format('Y'), (int)$dt->format('n'), array_search($closestMonday, $mondays)); $tasksForTheWeek = getTasksForTheWeek( array_search($closestMonday, $mondays), (int)$dt->format('m'), require 'taskMatrix.php' ); $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; if (!is_dir(dirname($completedTasksFile))) { mkdir(dirname($completedTasksFile), 0777, true); } if (in_array($arguments, $completedTasks)) { $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]); return; } if (!in_array($arguments, $tasksForTheWeek)) { $this->replyWithMessage(['text' => getString('unknownTask')]); return; } file_put_contents( $completedTasksFile, "$arguments\n", FILE_APPEND ); $this->replyWithMessage(['text' => getString('taskCompleted')]); } } ); getTelegram()->commandsHandler(true); //must come afterwards because lolzer