From: Cameron Ball Date: Fri, 28 Dec 2018 06:04:09 +0000 (+0800) Subject: Add keyboard for task completion X-Git-Url: http://git.cameron1729.xyz/?p=SonOfLokstallBot.git;a=commitdiff_plain;h=200ac7ce310c4a352c946a0893bdc192b29e9be8 Add keyboard for task completion --- diff --git a/.gitignore b/.gitignore index 2007501..87d21ac 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ config.php tasks vendor setWebHook.php +src/setWebHook.php diff --git a/src/purjolok.php b/src/purjolok.php index 7457843..4319b56 100644 --- a/src/purjolok.php +++ b/src/purjolok.php @@ -24,6 +24,71 @@ function getBetween($content,$start){ return ''; } +if ($between = getBetween(reveal($message ?? ''), '[taskid]')) { + $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)); + $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; + $currentWeekOfMonth = array_search($closestMonday, $mondays); + $tasksForTheWeek = getTasksForTheWeek( + $currentWeekOfMonth, + (int)$dt->format('m'), + require 'taskMatrix.php' + ); + + $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id'); + + if (!is_dir(dirname($completedTasksFile))) { + mkdir(dirname($completedTasksFile), 0777, true); + } + + if (in_array($between, $completedTasks)) { + getTelegram()->sendMessage([ + 'chat_id' => $chatid, + 'text' => getString('taskAlreadyCompleted') + ]); + return; + } + + if (!in_array($between, $tasksForTheWeek)) { + getTelegram()->sendMessage([ + 'chat_id' => $chatid, + 'text' => getString('unknownTask') + ]); + return; + } + + file_put_contents( + $completedTasksFile, + "$between\n", + FILE_APPEND + ); + + getTelegram()->sendMessage([ + 'chat_id' => $chatid, + 'text' => getString('taskCompleted'), + 'reply_markup' => json_encode(['remove_keyboard' => true]) + ]); + + getTelegram()->sendSticker([ + 'chat_id' => $chatid, + 'sticker' => 'CAADBQADOgAD__7RBxFhadMBV3g5Ag' + ]); + + if (!array_diff($completedTasks, $tasksForTheWeek)) { + getTelegram()->sendMessage([ + 'chat_id' => $chatid, + 'text' => getString('heyTasksAllCompleted') + ]); + } +} + if ($between = getBetween(reveal($message ?? ''), '[billid]')) { $messages = array_values(array_filter( getMessagesFromInbox( @@ -203,6 +268,13 @@ getTelegram()->addCommand( $completedTasksFile = getFilePathForWeek($currentYear, $currentMonth, $currentWeekOfMonth); $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : []; + if (!array_diff($tasksForTheWeek, $completedTasks)) { + $this->replyWithMessage([ + 'text' => getString('tasksAllCompleted') + ]); + return; + } + $this->replyWithMessage([ 'text' => ununlines([ getString('tasksForTheWeek'), @@ -238,27 +310,31 @@ getTelegram()->addCommand( $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')]); + if (!array_diff($completedTasks, $tasksForTheWeek)) { + $this->replyWithMessage([ + 'text' => getString('tasksAllCompleted') + ]); return; } - if (!in_array($arguments, $tasksForTheWeek)) { - $this->replyWithMessage(['text' => getString('unknownTask')]); - return; - } + $tasks = array_values(map(function($task) { + return sprintf('%s%s', hide('[taskid]' . $task . '[taskid]'), getString($task)); + })(array_diff($tasksForTheWeek, $completedTasks))); + $buttons = partition((int)ceil(count($tasks)/3), $tasks); + $reply_markup = getTelegram()->replyKeyboardMarkup([ + 'keyboard' => $buttons, + 'resize_keyboard' => true, + 'one_time_keyboard' => true, + 'selective' => true + ]); - file_put_contents( - $completedTasksFile, - "$arguments\n", - FILE_APPEND + $this->replyWithMessage( + [ + 'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSenderId($this->getUpdate()) . '), which task did you finish?', + 'parse_mode' => 'markdown', + 'reply_markup' => $reply_markup + ] ); - - $this->replyWithMessage(['text' => getString('taskCompleted')]); } } ); diff --git a/src/strings.php b/src/strings.php index b9471c4..62635b4 100644 --- a/src/strings.php +++ b/src/strings.php @@ -58,8 +58,10 @@ return [ 'thisMonthThereAre' => 'This month there are %d tasks that need completing (as well as the regular tasks). I\'ll try my best to space them out over the coming weeks. As a heads up, here they are:', 'tasksForTheWeek' => 'Here are the incomplete tasks for this week...', 'taskAlreadyCompleted' => 'Hmm... It looks as though that task was already done this week.', - 'taskCompleted' => 'Marked as completed, nice one!', + 'taskCompleted' => 'Marked as completed!', 'unknownTask' => 'That doesn\'t look like a task I asked you to do this week... Use the listtasks command to see all the tasks.', + 'tasksAllCompleted' => 'I have nothing to show you, you\'ve completed all the tasks for this week! Epic.', + 'heyTasksAllCompleted' => 'Hey! That was the last task for this week. Good going!', 'rangehoodfilters' => 'Clean the rangehood filters in the kitchen', 'poolwater' => 'Check the level of the pool, and fill if needed',