@@ -152,46 +152,20 @@ private static function getStatusMessage(User $sender, $config)
152
152
153
153
$ doors = json_decode ($ doors ->getBody ()->getContents (), true , 512 , JSON_THROW_ON_ERROR );
154
154
155
- $ icons = [
156
- 'locked ' => '🔒 ' ,
157
- 'unlocked ' => '🔓 ' ,
158
- 'unknown ' => '❓ ' ,
159
- 'open ' => '🚪 ' ,
160
- ];
161
-
162
- $ markup = [
163
- 'label ' => static fn (string $ text ) => new InlineKeyboardButton ([
164
- 'text ' => $ text ,
165
- 'callback_data ' => static ::buildCbData (),
166
- ]),
167
- 'lock ' => static fn (string $ doorId ) => new InlineKeyboardButton ([
168
- 'text ' => $ icons ['locked ' ] . ' Lock ' ,
169
- 'callback_data ' => static ::buildCbData ([
170
- 'action ' => 'lock ' ,
171
- 'doorId ' => $ doorId ,
172
- ]),
173
- ]),
174
- 'open ' => static fn (string $ doorId ) => new InlineKeyboardButton ([
175
- 'text ' => $ icons ['open ' ] . ' Open ' ,
176
- 'callback_data ' => static ::buildCbData ([
177
- 'action ' => 'open ' ,
178
- 'doorId ' => $ doorId ,
179
- ]),
180
- ]),
181
- 'unlock ' => static fn (string $ doorId ) => new InlineKeyboardButton ([
182
- 'text ' => $ icons ['unlocked ' ] . ' Unlock ' ,
183
- 'callback_data ' => static ::buildCbData ([
184
- 'action ' => 'unlock ' ,
185
- 'doorId ' => $ doorId ,
186
- ]),
187
- ]),
188
- ];
189
-
190
155
$ kb = new InlineKeyboard ([
191
156
'inline_keyboard ' => array_merge (...array_map (static fn (array $ door ) => [[
192
- $ markup ['label ' ]($ door ['name ' ])
157
+ new InlineKeyboardButton ([
158
+ 'text ' => $ door ['name ' ],
159
+ 'callback_data ' => static ::buildCbData (),
160
+ ]),
193
161
], array_map (static fn (string $ action ) =>
194
- $ markup [$ action ]($ door ['id ' ]),
162
+ new InlineKeyboardButton ([
163
+ 'text ' => static ::getLabel ($ action ),
164
+ 'callback_data ' => static ::buildCbData ([
165
+ 'action ' => $ action ,
166
+ 'doorId ' => $ door ['id ' ],
167
+ ]),
168
+ ]),
195
169
$ door ['supported_actions ' ])], $ doors )),
196
170
]);
197
171
@@ -201,6 +175,24 @@ private static function getStatusMessage(User $sender, $config)
201
175
];
202
176
}
203
177
178
+ private static function getLabel (string $ action ) {
179
+ $ icons = [
180
+ 'lock ' => '🔒 ' ,
181
+ 'unlock ' => '🔓 ' ,
182
+ 'open ' => '🚪 ' ,
183
+ ];
184
+
185
+ $ separators = " \t\r\n\f\v-_ " ;
186
+ $ description = ucwords ($ action , $ separators );
187
+ $ baseAction = explode ($ separators , $ action )[0 ];
188
+
189
+ if (!array_key_exists ($ baseAction , $ icons )) {
190
+ return $ description ;
191
+ }
192
+
193
+ return $ icons [$ baseAction ] . ' ' . $ description ;
194
+ }
195
+
204
196
private static function getState (User $ sender )
205
197
{
206
198
return file_get_contents (static ::getFilename ($ sender , 'state ' ));
@@ -344,13 +336,12 @@ public static function processCallback(User $sender, Message $message, array $da
344
336
345
337
$ action = $ data ['action ' ];
346
338
347
- if (!in_array ( $ action , [ ' open ' , 'lock ' , 'unlock ' ] )) {
339
+ if (!ctype_alnum ( str_replace ([ ' - ' , '_ ' ] , '' , $ action ) )) {
348
340
return [
349
341
'text ' => 'Unknown command ' . $ action ,
350
342
];
351
343
}
352
344
353
- // action is open, lock or unlock
354
345
if (!array_key_exists ('doorId ' , $ data )) {
355
346
return [
356
347
'text ' => 'No door ID provided ' ,
@@ -408,7 +399,7 @@ public function execute()
408
399
409
400
[
410
401
$ action ,
411
- $ firstArgument ,
402
+ $ doorId ,
412
403
] = explode (' ' , $ text );
413
404
414
405
// Self deauth
@@ -421,7 +412,7 @@ public function execute()
421
412
]));
422
413
}
423
414
424
- if (!in_array ( $ action , [ ' open ' , 'lock ' , 'unlock ' ] )) {
415
+ if (!ctype_alnum ( str_replace ([ ' - ' , '_ ' ] , '' , $ action ) )) {
425
416
return Request::sendMessage (array_merge ($ replyData , [
426
417
'text ' => 'Invalid command ' ,
427
418
]));
@@ -435,7 +426,7 @@ public function execute()
435
426
436
427
$ replyData ['reply_to_message_id ' ] = $ chat_id < 0 ? $ response ->getResult ()->getMessageId () : null ;
437
428
438
- $ result = static ::executeCommand ($ sender , $ action , $ firstArgument , $ config );
429
+ $ result = static ::executeCommand ($ sender , $ action , $ doorId , $ config );
439
430
440
431
if ($ result !== true ) {
441
432
return Request::sendMessage (array_merge ($ replyData , $ result ));
0 commit comments