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 ''; }
public function getText() : string { if (!isset($this->text)) { $this->text = ltrim(substr(parent::getText(), strlen($this->commandName) + 2)); } return $this->text; }
/** * 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 { $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 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)); }