/** * Create a suitable message to bother people about W3C. * @param Message $message * @return string */ private function createReply(Message $message) : string { $return = 'W3Schools should not be trusted as a reliable resource. [Click here to read why](http://www.w3fools.com/).'; $categories = $this->dissectCategoriesFromString($message->getText()); foreach ($categories as $category) { $return .= ' ' . self::W3S_CATEGORY_RESPONSES[$category]; } return $return; }
private function getResponse(Message $message) : string { foreach ($this->patterns as $pattern => $response) { if (preg_match('/' . $pattern . '/iu', $this->normalizeText($message->getText())) === 1) { return $this->buildResponse($pattern, $response, $message->getText()); } } return ''; }
private function getResponse(Message $message) : string { $bestMatchPercentage = 0; $bestMatchResponse = null; foreach ($this->matches as $insult => $response) { $matchPercentage = $this->getMatchingPercentage($insult, $message->getText()); if ($matchPercentage > $bestMatchPercentage) { $bestMatchPercentage = $matchPercentage; $bestMatchResponse = $response; } } if ($bestMatchResponse === null) { throw new \LogicException('Could not get a match response!'); } return (string) $bestMatchResponse; }
public function getText() : string { if (!isset($this->text)) { $this->text = ltrim(substr(parent::getText(), strlen($this->commandName) + 2)); } return $this->text; }
public function handleMessage(Message $message) { if (!(yield from $this->isDadGreetEnabled($message->getRoom()))) { return; } if (!preg_match('#(?:^|\\s)(?:i\'m|i am)\\s+(.+?)\\s*(?:[.,!]|$)#i', $message->getText(), $match)) { return; } if (random_int(1, yield from $this->getDadGreetFrequency($message->getRoom())) !== 1) { return; } $fullName = strtoupper(substr($match[1], 0, 1)) . substr($match[1], 1); $reply = sprintf('Hello %s. I am %s.', $fullName, $message->getRoom()->getSession()->getUser()->getName()); if (preg_match('#^(\\S+)\\s+\\S#', $fullName, $match)) { $reply .= sprintf(' Do you mind if I just call you %s?', $match[1]); } (yield $this->chatClient->postReply($message, $reply)); }
/** * @param Message $message * @return Promise[] */ private function invokeHandlersForMessage(Message $message) : array { $promises = []; foreach ($this->messageHandlers as $pluginName => $handler) { if ($this->isPluginEnabledForRoom($pluginName, $message->getRoom())) { $promises[] = $this->invokeCallbackAsPromise($handler, $message); // some callbacks may be synchronous } } return $promises; }
/** * @param PostedMessage|Message $message * @param string $text * @param int $flags * @return Promise */ public function editMessage($message, string $text, int $flags = PostFlags::NONE) : Promise { $text = $this->applyPostFlagsToText($text, $flags); $body = (new FormBody())->addField("text", $text)->addField("fkey", (string) $message->getRoom()->getSession()->getFKey()); $url = $this->urlResolver->getEndpointURL($message->getRoom(), ChatRoomEndpoint::CHATROOM_EDIT_MESSAGE, $message->getId()); $request = (new HttpRequest())->setUri($url)->setMethod("POST")->setBody($body); $action = $this->actionFactory->createEditMessageAction($request, $message->getRoom()); return $this->actionExecutor->enqueue($action); }