public function testSearch() { $limit = rand(3, 10); $response = $this->youtube->search('Android', $limit); $this->assertEquals($limit, count($response)); $this->assertEquals('youtube#searchResult', $response[0]->kind); }
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.')); } }); }