/** * Check the supplied parameters are valid * * @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event * @return bool */ protected function validateParams(Event $event) { return count($event->getCustomParams()) > 0 ? true : false; }
/** * Command to search Bigstock * * @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleBigstockCommand(Event $event, Queue $queue) { $this->getLogger()->info('[Bigstock] received a new command'); $params = $event->getCustomParams(); if (count($params) < 1) { $this->getLogger()->debug('[Bigstock] did not detect any custom params, returning help method'); $this->handleBigstockHelp($event, $queue); return; } $this->getLogger()->info('[Bigstock] performing search with params', ['params' => $params]); $search_request = "http://api.bigstockphoto.com/2/{$this->accountId}/search?"; $search_request .= http_build_query(['q' => implode(' ', $params), 'limit' => 10, 'thumb_size' => 'large_thumb,small_thumb']); $request = new Request(['url' => $search_request, 'resolveCallback' => function (Response $response) use($event, $queue) { $code = $response->getStatusCode(); $stream = $response->getBody(); $data = json_decode($stream->getContents(), true); if ($code !== 200) { $this->getLogger()->notice('[Bigstock] API responded with error', ['code' => $code, 'message' => $data['message']]); $queue->ircPrivmsg($event->getSource(), 'Sorry, no images were found that match your query'); return; } $this->getLogger()->info('[Bigstock] API successful return', ['items' => $data['data']['paging']['items'], 'total' => $data['data']['paging']['total_items']]); $image_key = array_rand($data['data']['images']); $image = $data['data']['images'][$image_key]; $image['url'] = "http://www.bigstockphoto.com/image-{$image['id']}"; $this->emitShorteningEvents($image['url'])->then(function ($shortUrl) use($image, $event, $queue) { $image['url_short'] = $shortUrl; $message = $this->formatter->format($image); $this->getLogger()->info('[Bigstock] responding with successful url shortening', ['message' => $message]); $queue->ircPrivmsg($event->getSource(), $message); }, function () use($image, $event, $queue) { $message = $this->formatter->format($image); $this->getLogger()->info('[Bigstock] responding with failed url shortening', ['message' => $message]); $queue->ircPrivmsg($event->getSource(), $message); }); }, 'rejectCallback' => function ($data, $headers, $code) use($event, $queue) { $this->getLogger()->notice('[Bigstock] API failed to respond'); $queue->ircPrivmsg($event->getSource(), 'Sorry, there was a problem communicating with the API'); }]); $this->getEventEmitter()->emit('http.request', [$request]); }
/** * Handles help command calls * * @param \Phergie\Irc\Plugin\React\Command\CommandEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function helpCommand(CommandEventInterface $event, EventQueueInterface $queue) { $command = strpos($event->getCustomCommand(), '.help') !== false ? substr($event->getCustomCommand(), 0, -5) : $event->getCustomParams()[0]; $this->helpMessages([$queue, 'irc' . $event->getCommand()], $event->getSource(), $command); }