public function getFriendsUsernames(TwitterBot $bot) { $response = $bot->getClient()->getFriends(['screen_name' => $bot->getUsername()]); return array_map(function ($friendData) { return $friendData->screen_name; }, $response->users); }
public function behave(TwitterBot $bot) { $client = $bot->getClient(); $username = $bot->getUsername(); $request = []; $lastSeenDmId = $client->getLastSeenDmId(); if ($lastSeenDmId !== null) { $request['since_id'] = $lastSeenDmId; } $lastSeenDmId = null; $dms = $client->getDms($request); $dms = array_reverse($dms); foreach ($dms as $dm) { $response = $this->getResponse($bot, $dm); if ($response !== null) { if (is_array($response)) { $dmRequest = $response; } else { $dmRequest = ['text' => $response]; } $dmRequest['user_id'] = $dm->sender->id_str; $client->dm($dmRequest); } $lastSeenDmId = $dm->id_str; } if ($lastSeenDmId !== null) { $client->setLastSeenDmId($lastSeenDmId); } }
protected function getResponse(TwitterBot $bot, $tweet) { $id = $tweet->id_str; $sender = $tweet->user->screen_name; $targetId = $tweet->in_reply_to_status_id; $text = $tweet->text; $username = $bot->getUsername(); if (strpos($text, '@' . $username) !== 0) { return null; // not a direct mention } $game = SpymasterGame::getByTweetId($targetId); if ($game === null) { return null; // Not in response to a game } if ($game->getFinished()) { return 'Sorry, this game has already finished. DM me "start" to start a new game of your own'; } $text = substr($text, strlen($username) + 2); $text = preg_replace('/[^A-Za-z]+/', ' ', $text); $words = explode(' ', trim($text)); // TODO: validate this shit $game->recordGuess($sender, $words); }
public function behave(TwitterBot $bot) { $username = $bot->getUsername(); $client = $bot->getClient(); $request = []; $lastSeenTweetId = $client->getLastSeenTweetId(); if ($lastSeenTweetId !== null) { $request['since_id'] = $lastSeenTweetId; } $lastSeenTweetId = null; $tweets = $client->getMentions($request); foreach ($tweets as $tweet) { $id = $tweet->id_str; $text = $tweet->text; if (strpos($text, '@' . $username) === 0) { $response = $this->getResponse($bot, $tweet); if ($response !== null) { if (is_string($response)) { $response = ['status' => $response]; } $client->tweet($response); } } $lastSeenTweetId = $id; } if ($lastSeenTweetId !== null) { $client->setLastSeenTweetId($lastSeenTweetId); } }