Better pagination using inline buttons
authorCameron Ball <cameron@moodle.com>
Thu, 9 May 2019 07:34:23 +0000 (15:34 +0800)
committerCameron Ball <cameron@moodle.com>
Thu, 9 May 2019 08:12:34 +0000 (16:12 +0800)
src/gnb.php

index b8af5f5..1f9e155 100644 (file)
@@ -17,9 +17,26 @@ if (getTelegram()->getWebHookUpdates()->detectType() == 'callback_query') {
     $cbq = getTelegram()->getWebHookUpdates()->getCallbackQuery();
     // handling for paginating shit
     $data = $cbq->getData(); // that's gonna have the data attached to the button
+
     if ($data) {
+        list($whatever, $who, $page) = explode('_', $data);
+
+        $chunky = getChunkyScores($who);
+        $pranks = $chunky[$page - 1];
+        $numE = count($pranks);
+        $pranks[$numE-1] .= "\n——————————————";
+
+        $extra = count($chunky) > 1 ? " (page $page of " . count($chunky) . ")" : '';
+
+        $buttons = array_merge(
+            $page != 1 ? [['text' =>  '« Previous page', 'callback_data' => 'scores_' . $who . '_' . ($page - 1)]] : [],
+            $page != count($chunky) ? [['text' =>  'Next page »', 'callback_data' => 'scores_' . $who . '_' . ($page + 1)]] : []
+        );
+
         $keyboard = [
-            'inline_keyboard' => [[['text' =>  $data == 'jeff' ? 'chef' : 'jeff', 'callback_data' => $data == 'jeff' ? 'chef' : 'jeff']]],
+            'inline_keyboard' => [
+                $buttons
+            ],
         ];
 
         $markup = json_encode($keyboard);
@@ -27,7 +44,8 @@ if (getTelegram()->getWebHookUpdates()->detectType() == 'callback_query') {
         getTelegram()->editMessageText([
             'chat_id' => getTelegram()->getWebHookUpdates()->getChat()->getId(),
             'message_id' => $cbq->getMessage()->getMessageId(),
-            'text' => 'my name is ' . $data,
+            'parse_mode' => 'Markdown',
+            'text' => "High scores for $who"."$extra ...\n\n```\n" . unlines($pranks) . "\n```",
             'reply_markup' => $markup
         ]);
     }
@@ -288,12 +306,24 @@ getTelegram()->addCommand(
 
             $extra = count($chunky) > 1 ? " (page $page of " . count($chunky) . ")" : '';
 
+            $buttons = array_merge(
+                $page != 1 ? [['text' =>  '« Previous page', 'callback_data' => 'scores_' . $hsname . '_' . ($page - 1)]] : [],
+                $page != count($chunky) ? [['text' =>  'Next page »', 'callback_data' => 'scores_' . $hsname . '_' . ($page + 1)]] : []
+            );
+
+            $keyboard = [
+                'inline_keyboard' => [
+                    $buttons
+                ],
+            ];
+
             $chatid = getTelegram()->getWebhookUpdates()->get('message')->get('chat')->get('id');
             getTelegram()->sendMessage(
                 [
                     'chat_id' => $chatid,
                     'parse_mode' => 'Markdown',
-                    'text' => "High scores for $hsname"."$extra ...\n\n```\n" . unlines($pranks) . "```"
+                    'text' => "High scores for $hsname"."$extra ...\n\n```\n" . unlines($pranks) . "\n```",
+                    'reply_markup' => $buttons ? json_encode($keyboard) : []
                 ]
             );
         }