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); } $message = getTelegram()->getWebHookUpdates()->get('message')->get('text'); function getBetween($content,$start){ $r = explode($start, $content); if (isset($r[1])){ $r = explode($start, $r[1]); return $r[0]; } return ''; } if ($between = getBetween(reveal($message), '[billid]')) { $messages = array_values(array_filter( getMessagesFromInbox( getInbox( 'Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay' ), require 'rules.php', FALSE ), function($e) use ($between) { return $e['id'] == $between; } )); if(!$messages || count($messages) !== 1) { getTelegram()->sendMessage([ 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'), 'text' => "That doesn't look like a valid id. Use /mybills to list your bills." ]); return; } imap_delete(getInbox('Utilities/' . getMessageSender(getTelegram()->getWebhookUpdates()) . ' To Pay'), $messages[0]['uid'], FT_UID); getTelegram()->sendMessage([ 'chat_id' => getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'), 'text' => "I marked " . getMessageSenderDisplayName(getTelegram()->getWebHookUpdates()) . " as having paid the " . strtolower($messages[0]['service']) . " bill, thanks!", 'reply_markup' => json_encode(['remove_keyboard' => true]) ]); } 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 each due on the %s", $bill['service'], 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) { $buttons = [array_values(map(function($bill) { return sprintf('%s%s', hide('[billid]' . $bill['id'] . '[billid]'), $bill['service'] . ' ($' . splitBill($bill['amount']) . ')'); })(getMessagesFromInbox( getInbox( 'Utilities/' . getMessageSender($this->getUpdate()) . ' To Pay' ), require 'rules.php', FALSE )))]; error_log(print_r($buttons, true)); if (!$buttons[0]) { $this->replyWithMessage([ 'text' => getMessageSenderDisplayName($this->getUpdate()). ' doesn\'t have any outstanding bills. Nice :)', 'reply_markup' => json_encode(['remove_keyboard' => true]) ]); return; } $reply_markup = getTelegram()->replyKeyboardMarkup([ 'keyboard' => $buttons, 'resize_keyboard' => true, 'one_time_keyboard' => true, 'selective' => true ]); $this->replyWithMessage( [ 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which bill did you want to pay?', 'parse_mode' => 'markdown', 'reply_markup' => $reply_markup ] ); } } ); 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'), ]; $currentMonth = (int)(new DateTimeImmutable())->format('m'); $currentDayOfMonth = closest((new DateTimeImmutable())->format('d'), $mondays); $currentWeekOfMonth = array_search($currentDayOfMonth, $mondays); $currentYear = (int)(new DateTimeImmutable())->format('Y'); $tasksForTheWeek = getTasksForTheWeek( $currentWeekOfMonth, (int)$dt->format('m'), require 'taskMatrix.php' ); $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth); $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