From baaf08af80acdf323c8e85099468a3ab6a7fba55 Mon Sep 17 00:00:00 2001 From: Hunter Ashton Date: Fri, 19 May 2017 14:34:57 -0500 Subject: [PATCH] Add an HTML parser for buttons! (#123) --- .../MadelineProto/TL/Conversion/BotAPI.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/danog/MadelineProto/TL/Conversion/BotAPI.php b/src/danog/MadelineProto/TL/Conversion/BotAPI.php index 4c0d9f76f..3e0d0176d 100644 --- a/src/danog/MadelineProto/TL/Conversion/BotAPI.php +++ b/src/danog/MadelineProto/TL/Conversion/BotAPI.php @@ -407,6 +407,17 @@ trait BotAPI $href = $node->getAttribute('href'); if (preg_match('|mention:|', $href)) { $entities[] = ['_' => 'inputMessageEntityMentionName', 'offset' => mb_strlen($nmessage), 'length' => mb_strlen($text), 'user_id' => $this->get_info(str_replace('mention:', '', $href))['InputUser']]; + } elseif (preg_match('|buttonurl:|', $href)) { + if (!isset($entities['buttons'])) { + $entities['buttons'] = []; + } + if (preg_match('|:new|', substr($href, -4))) { + $entities['buttons'][] = ['_' => 'keyboardButtonUrl', 'text' => $text, 'url' => str_replace('buttonurl:', '', str_replace(':new', '', $href)), 'new' => true]; + } else { + var_dump(true); + $entities['buttons'][] = ['_' => 'keyboardButtonUrl', 'text' => $text, 'url' => str_replace('buttonurl:', '', $href)]; + } + break; } else { $entities[] = ['_' => 'messageEntityTextUrl', 'offset' => mb_strlen($nmessage), 'length' => mb_strlen($text), 'url' => $href]; } @@ -440,6 +451,10 @@ trait BotAPI foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) { $this->parse_node($node, $arguments['entities'], $nmessage); } + if (isset($arguments['entities']['buttons'])) { + $arguments['reply_markup'] = $this->build_rows($arguments['entities']['buttons']); + unset($arguments['entities']['buttons']); + } unset($arguments['parse_mode']); } catch (\DOMException $e) { } catch (\danog\MadelineProto\Exception $e) { @@ -513,4 +528,33 @@ trait BotAPI return htmlentities($text); } } + + public function build_rows($button_list) + { + $end = false; + $rows = []; + $buttons = []; + $cols = 0; + foreach ($button_list as $button) { + if (isset($button['new'])) { + if (count($buttons) == 0) { + $buttons[] = $button; + } else { + $row = ['_' => 'keyboardButtonRow', 'buttons' => $buttons]; + $rows[] = $row; + $buttons = [$button]; + } + } else { + $buttons[] = $button; + $end = true; + } + } + + if ($end) { + $row = ['_' => 'keyboardButtonRow', 'buttons' => $buttons]; + $rows[] = $row; + } + + return ['_' => 'replyInlineMarkup', 'rows' => $rows]; + } }