public function init() { $config = $this->getConfig() + $this->defaults; if (empty($config['api_key'])) { throw new \Exception('Unable to locate google_api_key in bot configuration.'); } // Let's not flood a channel and limit results to 5 if ($config['result_limit'] > 5) { $config['result_limit'] = 5; } $this->bot->onChannel('/^!(?:yt|youtube)?\\s([\\w\\s]+)/i', function (Event $event) use($config) { $keywords = $event->getMatches(); $channel = $event->getRequest()->getSource(); $youtube = new Youtube(array('key' => $config['api_key'])); $results = $youtube->search($keywords[0], $config['result_limit']); if ($results) { $i = 1; foreach ($results as $video) { $message = sprintf('%s. %s - https://www.youtube.com/watch?v=%s', $i++, $video->snippet->title, $video->id->videoId); $event->addResponse(Response::msg($channel, $message)); } } else { $event->addResponse(Response::msg($channel, 'No results found.')); } }); }
/** * Perform the IDENTIFY conversation with NickServ. * * Note that this behavior has been tuned to wait a few seconds after * the IDENTIFY conversation, to give the server a chance to acknowledge it. * * @return void */ public function identify() { if (array_key_exists('password', $this->config) && $this->config['password']) { $this->send(Response::msg('NickServ', 'IDENTIFY' . ' ' . $this->config['username'] . ' ' . $this->config['password'])); sleep(20); } }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!php(doc)? (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $client = new Client(); $crawler = $client->request('GET', sprintf('http://www.php.net/%s', str_replace('_', '-', $match))); if ($crawler->filter('.refnamediv h1.refname')->count() !== 0) { $function = $crawler->filter('.refnamediv h1.refname')->first()->text(); $description = $crawler->filter('.refnamediv span.dc-title')->first()->text(); $version = $crawler->filter('.refnamediv p.verinfo')->first()->text(); $synopsis = $crawler->filter('.methodsynopsis')->first()->text(); $synopsis = preg_replace('/\\s+/', ' ', $synopsis); $synopsis = preg_replace('/(\\r\\n|\\n|\\r)/m', ' ', $synopsis); $synopsis = trim($synopsis); $event->addResponse(Response::msg($request->getSource(), sprintf('%s - %s %s', $function, $description, $version))); $event->addResponse(Response::msg($request->getSource(), sprintf('Synopsis: %s', $synopsis))); } else { $suggestion = $crawler->filter('#quickref_functions li a b')->first()->text(); $event->addResponse(Response::msg($request->getSource(), sprintf('Could not find the requested PHP function. Did you mean: %s?', $suggestion))); } }); }
public function init() { $lines = file(__DIR__ . '/data/nutella.txt'); $this->bot->onChannel('/^!(nutella)(.*)/i', function (Event $event) use($lines) { $channel = $event->getRequest()->getSource(); $event->addResponse(Response::msg($channel, $lines[array_rand($lines)])); }); }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!(wtc|cm)/i', function (Event $event) use($that) { $request = $event->getRequest(); $data = $that->getCommitMessage(); $event->addResponse(Response::msg($request->getSource(), $data)); }); }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!r(ot)?13 (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $event->addResponse(Response::msg($request->getSource(), str_rot13(trim($match)))); }); }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!m(otivate)? (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $event->addResponse(Response::msg($request->getSource(), sprintf('You\'re doing good work, %s!', trim($match)))); }); }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!(eightball|8|8ball) (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $event->addResponse(Response::msg($request->getSource(), $that->responses[base_convert(strtolower($match), 36, 10) % count($that->responses)])); }); }
/** * This plugin is made of two parts: the command to save messages, and the listener to deliver messages. * * !msg <recipient> <message> - saves a message for the recipient * Example Usage: * !msg irc-buddy Call me whenever you get this * * NOTE: if the command is sent via private message, the message will be delivered via private message as well. */ public function init() { $bot = $this->bot; /** The messages to deliver publicly to users */ $public = array(); /** The messages to deliver privately to users */ $private = array(); // Saves messages $this->bot->onMessages('/^!msg\\s(\\S+)\\s(.*)/', function (Event $event) use($bot, &$private, &$public) { $matches = $event->getMatches(); $recipient = $matches[0]; $sender = $event->getRequest()->getSendingUser(); $msg = $matches[1]; if ($event->getRequest()->isPrivateMessage()) { $queue =& $private; } else { $queue =& $public; } if (!isset($queue[$recipient])) { $queue[$recipient] = array(); } $queue[$recipient][$msg] = $sender; $event->addResponse(Response::msg($event->getRequest()->getSource(), "Message for {$recipient} saved successfully.")); }); // Delivers messages $this->bot->onJoin(function (Event $event) use($bot, &$public, &$private) { $joiner = $event->getRequest()->getSendingUser(); $channel = $event->getRequest()->getSource(); // Retrieve public messages if (in_array($joiner, array_keys($public))) { foreach ($public[$joiner] as $message => $sender) { if ($joiner == $sender) { $msg = "{$joiner}: you left yourself a message - {$message}"; } else { $msg = "{$joiner}: {$sender} left you a message - {$message}"; } $event->addResponse(Response::msg($channel, $msg)); unset($public[$joiner][$message]); } } // Retrieve private messages if (in_array($joiner, array_keys($private))) { foreach ($private[$joiner] as $message => $sender) { if ($joiner == $sender) { $msg = "You left yourself a message - {$message}"; } else { $msg = "{$sender} left you a message - {$message}"; } $event->addResponse(Response::msg($joiner, $msg)); unset($private[$joiner][$message]); } } }); }
public function init() { $this->bot->onChannel($this->urlRegex, function (Event $event) { $essence = Essence::instance(array('Http' => new HttpNative())); if (preg_match_all($this->urlRegex, $event->getRequest()->getMessage(), $matches) !== false) { $medias = $essence->embedAll($matches['url']); foreach ($medias as $url => $info) { if ($info && $info->has('title')) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $info->title . ' -> ' . $url)); } } } }); }
/** * Get a random movie quote from * * @param string $source Movie source to get the quote from * * @return string the commit message */ public function getMovieQuote($source, $event) { $httpClient = new Client('http://www.randquotes.com/'); $httpRequest = $httpClient->get('/' . $source); $httpResponse = $httpRequest->send(); if ($httpResponse->isSuccessful() === true) { $quoteLines = explode("\n", $httpResponse->getBody(true)); foreach ($quoteLines as $quoteLine) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $quoteLine)); } } else { $event->addResponse(Response::msg($event->getRequest()->getSource(), 'Sorry! I was unable to scrape the greatest movie quote ever :(')); } }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $file = file_get_contents(__DIR__ . '/PeterGriffinPlugin/quotes.txt'); $this->quotes = explode("\n%\n", $file); $this->bot->onMessages('/^!(pt|petergriffin)(.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $quoteLines = explode("\n", $this->getQuote($match)); foreach ($quoteLines as $quoteLine) { $event->addResponse(Response::msg($request->getSource(), $quoteLine)); } }); }
/** * Adds !quit, !join, and !leave commands to the bot: * * !quit <quit message> -- Tells the bot to quit the IRC server * Example usage: * !quit ...aaand boom goes the dynamite. * * !join <channels> * Example usage: * !join #example-room * !join #example-room1 #example-room2 * * !leave <channels> -- Tells the bot to leave channels * Example usage: * !leave #example-room * !leave #example-room1 #example-room2 * * !say <channel> <msg> -- Tells the bot to send a message to the given channel * Example usage: * !say #example-room Look I can talk. * * These commands only work via private message and only if the issuer * is in the ops array in the bot's configuration. */ public function init() { $bot = $this->bot; $config = $bot->getConfig(); // Allow the bot to join rooms $this->bot->onPrivateMessage("/^!join(.*)/", function (Event $event) use($config, $bot) { $matches = $event->getMatches(); $user = $event->getRequest()->getSendingUser(); $rooms = explode(' ', $matches[0]); if ($bot->isAdmin($user)) { $event->addResponse(Response::join(implode(',', $rooms))); } else { $event->addResponse(Response::msg($user, "You're not the boss of me.")); } }); // Allow the bot to leave rooms $this->bot->onPrivateMessage("/^!leave(.*)/", function (Event $event) use($config, $bot) { $matches = $event->getMatches(); $user = $event->getRequest()->getSendingUser(); $rooms = explode(' ', $matches[0]); if ($bot->isAdmin($user)) { $event->addResponse(Response::leave(implode(',', $rooms))); } else { $event->addResponse(Response::msg($user, "You're not the boss of me.")); } }); // Echo things into channels $this->bot->onPrivateMessage("/^!say ([#&][^,\\s]{0,200}) (.+)/", function (Event $event) use($config, $bot) { $matches = $event->getMatches(); $user = $event->getRequest()->getSendingUser(); if ($bot->isAdmin($user)) { $event->addResponse(Response::msg($matches[0], $matches[1])); } else { $event->addResponse(Response::msg($user, "You're not the boss of me.")); } }); // Quit gracefully $this->bot->onPrivateMessage("/^!quit(.*)/", function (Event $event) use($config, $bot) { $matches = $event->getMatches(); $user = $event->getRequest()->getSendingUser(); $msg = $matches[0] ? trim($matches[0]) : 'Later, kids.'; if ($bot->isAdmin($user)) { $event->addResponse(Response::quit($msg)); } else { $event->addResponse(Response::msg($user, "You're not the boss of me.")); } }); }
/** * Listens to channel messages and lets everyone know who owes what to * the "swear jar". */ public function init() { $swears = array('fu+ck', 'sh+i+t', 'cu+nt', 'co+ck'); $swear_jar = array(); // Don't say bad words, kids. $re = '/' . implode('|', $swears) . '/i'; $this->bot->onChannel($re, function (Event $event) use(&$swear_jar) { $cost = 0.25; $who = $event->getRequest()->getSendingUser(); if (!isset($swear_jar[$who])) { $swear_jar[$who] = 0; } $price = $swear_jar[$who] += $cost; $event->addResponse(Response::msg($event->getRequest()->getSource(), sprintf("Mind your tongue {$who}! Now you owe \$%.2f to the swear jar.", $price))); }); }
/** * Init the plugin and start listening to messages */ public function init() { $this->addResponses(); $config = $this->bot->getConfig(); // detects someone speaking to the bot $responses = $this->responses; $address_re = "/(^{$config['nick']}(.+)|(.+){$config['nick']}[!.?]*)\$/i"; $this->bot->onChannel($address_re, function (Event $event) use($responses) { $matches = $event->getMatches(); $message = $matches[1] ? $matches[1] : $matches[2]; foreach ($responses as $regex => $function) { if (preg_match($regex, $message, $matches)) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $function($matches))); } } }); }
/** * Initializing the plugin's behavior * * @return void */ public function init() { $that = $this; $this->bot->onMessages('/^!b(ase)?36 e(ncode)?( me)? (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $match = trim($match); $event->addResponse(Response::msg($request->getSource(), sprintf('Base36 encoded "%s" = "%s"', $match, base_convert($match, 10, 36)))); }); $this->bot->onMessages('/^!b(ase)?36 d(ecode)?( me)? (.*)/i', function (Event $event) use($that) { $request = $event->getRequest(); $matches = $event->getMatches(); $match = array_pop($matches); $match = trim($match); $event->addResponse(Response::msg($request->getSource(), sprintf('Base36 decoded "%s" = "%s"', $match, base_convert($match, 10, 36)))); }); }
/** * Initialize the plugin. */ public function init() { // Ugh...PHP 5.3, you're killin' me. $that = $this; // Get me an image! $this->bot->onChannel('/^!(?:img|image) (.+)$/i', function (Event $event) use($that) { $matches = $event->getMatches(); if ($img = $that->getImage(trim($matches[0]), false)) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $img)); } }); // Get me a gif! $this->bot->onChannel('/^!gif (.+)$/i', function (Event $event) use($that) { $matches = $event->getMatches(); if ($img = $that->getImage(trim($matches[0]), true)) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $img)); } }); }
public function main() { $this->bot = new Philip($this->config); $factory = new \ChatterBotFactory(); $clever = $factory->create(\ChatterBotType::PANDORABOTS, 'b0dafd24ee35a477'); $session = $clever->createSession(); $this->bot->onMessages('/ai(.*)/u', function (Event $event) use(&$clever, &$session) { $matches = $event->getMatches(); $message = $matches[0]; $response = ""; try { $response = $session->think($message); } catch (\Exception $e) { $response = "I can't reach the Cloud. I don't know how to answer"; $session = $clever->createSession(); } $event->addResponse(Response::msg($event->getRequest()->getSource(), $response)); }); $this->bot->run(); }
public function init() { $config = $this->getConfig() + $this->defaults; // Let's not flood a channel and limit results to 5 if ($config['result_limit'] > 5) { $config['result_limit'] = 5; } $this->bot->onChannel('/^!g(?:oogle)?\\s([\\w\\s]+)/i', function (Event $event) use($config) { $keywords = $event->getMatches(); $channel = $event->getRequest()->getSource(); try { $client = new Client(); $crawler = $client->request('GET', 'http://www.google.com' . ($config['prefix'] ? '.' . $config['prefix'] : '') . '/search?q=' . urlencode($keywords[0])); $spell = $crawler->filter('span.spell'); if ($spell->count() > 0) { $event->addResponse(Response::msg($channel, $spell->text() . ' "' . $crawler->filter('a.spell')->text() . '"')); } $results = $crawler->filter('#ires h3 a')->reduce(function (Crawler $node, $i) { return preg_match('@\\/url\\?q=(.*)&sa@i', $node->link()->getUri()) !== 0; })->each(function ($node) { if (preg_match('@\\/url\\?q=(.*)&sa@i', $node->link()->getUri(), $matches) !== 0) { return array('title' => $node->text(), 'url' => $matches[1]); } }); if (count($results) > 0) { for ($i = 0; $i < $config['result_limit']; $i++) { $message = sprintf('%s. %s - %s', $i + 1, $results[$i]['title'], $results[$i]['url']); $event->addResponse(Response::msg($channel, $message)); } } else { $event->addResponse(Response::msg($channel, 'No results found.')); } } catch (\Guzzle\Http\Exception\CurlException $e) { $event->addResponse(Response::msg($channel, $e->getMessage())); } }); }
public function testClass() { $this->string(TestedClass::pong($host = uniqid()))->isEqualTo('PONG :' . $host)->string(TestedClass::quit($message = uniqid()))->isEqualTo('QUIT :' . $message)->string(TestedClass::join($channel = '#' . uniqid()))->isEqualTo('JOIN ' . $channel)->string(TestedClass::leave($channel))->isEqualTo('PART ' . $channel)->string(TestedClass::msg($who = uniqid(), $message))->isEqualTo('PRIVMSG ' . $who . ' :' . $message)->string(TestedClass::notice($channel, $message))->isEqualTo('NOTICE ' . $channel . ' :' . $message)->string(TestedClass::action($channel, $message))->isEqualTo('PRIVMSG ' . $channel . ' :' . "ACTION " . $message . "")->string(TestedClass::user($nick = uniqid(), $name = uniqid()))->isEqualTo('USER ' . $nick . ' 8 * :' . $name)->string(TestedClass::nick($nick))->isEqualTo('NICK :' . $nick)->string(TestedClass::pass($pass = uniqid()))->isEqualTo('PASS ' . $pass); }
/** * Loads default event handlers for basic IRC commands that won't be * unique to each bot. */ private function addDefaultHandlers() { $log = $this->log; // When the server PINGs us, just respond with PONG and the server's host $this->onPing(function (Event $event) { $event->addResponse(Response::pong($event->getRequest()->getMessage())); }); // If an ERROR message is encountered, just log it for now. $this->onError(function (Event $event) use($log) { $log->debug("--- ERROR: {$event->getRequest()->getMessage()}"); }); $plugins =& $this->plugins; $this->onMessages('/^!help\\s*$/', function (Event $event) use(&$plugins) { foreach ($plugins as $plugin) { $messages = $plugin->displayHelp($event); // Surely there's a better way to do this... <sadface> if (is_array($messages)) { foreach ($messages as $message) { $event->addResponse(Response::msg($event->getRequest()->getSendingUser(), $message)); } } else { $event->addResponse(Response::msg($event->getRequest()->getSendingUser(), $messages)); } } }); }
public function help() { $channel = $this->event->getRequest()->getSource(); $lines = array('This is OftBot - a 1, 4, 24 IRC bot.', ' OftBot receives commands of the form \'@oftbot <command>\'.', ' OftBot accepts these commands: suggest, killgame, join, leave, kick, start, roll, keep (also available as k), help, and status.', ' For more information, see https://github.com/triplepoint/OftBot .'); foreach ($lines as $line) { $this->event->addResponse(Response::notice($channel, $line)); } }
/** * Add an error message to the response. * * @param \Philip\IRC\Event $event The event to add a response to * @param string $msg The error message to send * * @return void */ public function addErrorMsg($event, $msg) { $event->addResponse(Response::msg($event->getRequest()->getSource(), $msg)); }