Use telegram keyboard for selecting the bill to pay
authorCameron Ball <cameron@cameron1729.xyz>
Wed, 19 Dec 2018 09:15:49 +0000 (17:15 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Fri, 28 Dec 2018 06:27:20 +0000 (14:27 +0800)
common.php
purjolok.php

index f07c970..7967015 100644 (file)
@@ -328,6 +328,36 @@ function closest($n, $list) {
     return array_values($a)[0];
 }
 
+function reveal($str) {
+    $lastBytes = array_filter(
+        (unpack('C*', $str)),
+        function($key) {
+            return $key % 4 == 0;
+        },
+        ARRAY_FILTER_USE_KEY
+    );
+
+    $penultimateBytes = array_filter(
+        (unpack('C*', $str)),
+        function($key) {
+            return ($key + 1) % 4 == 0;
+        },
+        ARRAY_FILTER_USE_KEY
+    );
+
+    return implode('', zipWith(function($byte, $prevByte) {
+        return chr($byte - ($prevByte == 133 ? 64 : 128));
+    }, $lastBytes, $penultimateBytes));
+}
+
+function hide($str) {
+    $charBytes = unpack('C*', $str);
+    return pack(...array_merge(['C*'], array_merge(...map(function($charByte) {
+        $magic = [243, 160, $charByte < 65 ? 132 : 133];
+        return array_merge($magic, [$charByte + ($charByte < 65 ? 128 : 64)]);
+    })($charBytes))));
+}
+
 function getMessagesFromInbox($inbox, array $rules, $unseenOnly = true) {
     return array_filter(
         array_map(
index 48f360b..f85ebca 100644 (file)
@@ -13,6 +13,46 @@ if(!canChatWith(getTelegram()->getWebhookUpdates())) {
     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!"
+    ]);
+}
+
 getTelegram()->addCommand(
     new class extends Command {
         protected $name = 'searchmovies';
@@ -61,9 +101,8 @@ getTelegram()->addCommand(
                     array_map(
                         function($bill) {
                             return sprintf(
-                                "%s (%s): $%s each due on the %s",
+                                "%s: $%s each due on the %s",
                                 $bill['service'],
-                                $bill['id'],
                                 splitBill($bill['amount']),
                                 $bill['due']->format('jS \of M')
                             );
@@ -89,30 +128,32 @@ getTelegram()->addCommand(
 
         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(
+                $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
-                ),
-                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;
+                )))];
+
+                $reply_markup = getTelegram()->replyKeyboardMarkup([
+                    'keyboard' => $buttons,
+                    'resize_keyboard' => true,
+                    'one_time_keyboard' => true,
+                    'remove_keyboard' => true,
+                    'selective' => true
+                ]);
+
+                $this->replyWithMessage(
+                    [
+                        'text' => '[' . getMessageSenderDisplayName($this->getUpdate()) . '](tg://user?id=' . getMessageSender($this->getUpdate()) . '), which bill did you want to pay?',
+                        'parse_mode' => 'markdown',
+                        'reply_markup' => $reply_markup
+                    ]
+                );
             }
-
-            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!"]);
         }
     }