Add an HTML parser for buttons! (#123)

This commit is contained in:
Hunter Ashton 2017-05-19 14:34:57 -05:00 committed by Daniil Gentili
parent 3ade409442
commit baaf08af80

View File

@ -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];
}
}