<?php
/**
* Watches for "tell me about <slug>"
* and "phabot remember <slug> as: <text>"
*
* @group irc
*/
final class PhabricatorBotDocHandler extends PhabricatorBotHandler {
public function receiveMessage(PhabricatorBotMessage $message) {
switch ($message->getCommand()) {
case 'MESSAGE':
$matches = null;
$text = $message->getBody();
$pattern = '@^tell me about (.*)$@';
if (preg_match($pattern, $text, $matches)) {
try {
$result = $this->getConduit()->callMethodSynchronous(
'phriction.info',
array(
'slug' => 'docbot/'.$matches[1],
));
} catch (ConduitClientException $ex) {
phlog($ex);
$result = null;
}
$response = array();
if ($result) {
$content = phutil_split_lines(
$result['content'],
$retain_newlines = false
);
foreach ($content as $line) {
$response = array_merge($response, str_split($line, 400));
if (count($response) >= 3) {
break;
}
}
} else {
$response[] = "Nothing to say about ".$slug;
}
foreach (array_slice($response, 0, 3) as $output) {
$this->replyTo($message, html_entity_decode($output));
}
break;
}
$pattern =
'@'.
$this->getConfig('nick', 'phabot').
' remember '.
'(.*?)'.
' as:'.
'(.*)$'.
'@';
if (preg_match($pattern, $text, $matches)) {
$result = $this->getConduit()->callMethodSynchronous(
'phriction.edit',
array(
'slug' => 'docbot/'.$matches[1],
'content' => $matches[2],
));
$slug = explode('/', trim($result['slug'], '/'));
$output = "Saved as '${slug[1]}' at ${result['uri']}";
$this->replyTo($message, $output);
break;
}
break;
}
}
private function lookupDoc($slug) {
}
}