Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class twitterbot {
- var $connection;
- var $blacklist = array();
- var $m = null;
- var $db = null;
- var $ignorelist = null;
- var $botid = null;
- /**
- * If any array element is in the string it returns true
- *
- * If the string contains a word/string of characters that is in the array it returns true
- *
- * @param array $inArray array of words to check for
- * @param string $inString the string to check for the words in
- * @return bool
- * @author http://stackoverflow.com/a/10137763/3349060
- */
- function instring($inArray, $inString){
- if(is_array($inArray)){
- foreach($inArray as $e){
- if(strpos($inString, $e)!==false)
- return true;
- }
- return false;
- }else{
- return (strpos( $inString , $inArray )!==false);
- }
- }
- /**
- * Sets up the bot with the twitter account
- *
- * Authenticates with twitter using https://github.com/abraham/twitteroauth
- *
- * @param string $consumerkey Consumer key for Twitter Application
- * @param string $consumersecret Consumer secret for Twitter Application
- * @param string $secretkey Secret key for Twitter Application
- * @param string $secretsecret Secret Secret for Twitter Application
- * @return TwitterOAuth
- */
- function connect($consumerkey, $consumersecret, $secretkey, $secretsecret){
- global $ignorelist;
- $this->connection = new TwitterOAuth($consumerkey, $consumersecret, $secretkey, $secretsecret);
- $botid = $this->connection->get('account/verify_credentials')->id_str;
- $m = new MongoClient();
- $db = $m->botsignorelist;
- $ignorelist = $db->$botid;
- return $this->connection;
- }
- /**
- * Adds user to blacklist
- *
- * @param string $userids
- * @return array
- */
- function addblacklist($userids){
- $this->blacklist = array_merge($this->blacklist, $userids);
- return $this->blacklist;
- }
- /**
- * Tweets the supplied msg
- *
- * @param string $msg
- * @param string $userid
- * @return array
- */
- function tweet($msg, $userid = "00000"){
- if(!$this->hasbeenignored($userid)){
- $tweet = $this->connection->post('statuses/update', array("status" => $msg));
- return $tweet;
- }
- }
- /**
- * Replys to with $msg to the supplied tweet
- *
- * @param string $msg
- * @param string $to
- * @param string $userid
- * @return array
- */
- function reply($msg, $to, $userid = "00000"){
- if(!$this->hasbeenignored($userid)){
- $reply = $this->connection->post('statuses/update', array("status" => $msg, "in_reply_to_status_id" => $to));
- return $reply;
- }
- }
- /**
- * Retweets the supplied tweet
- *
- * @param string $id
- * @param string $userid
- * @return array
- */
- function retweet($id, $userid = "00000"){
- if(!$this->hasbeenignored($userid)){
- $retweet = $this->connection->post('statuses/retweet/'.$id);
- }
- return $retweet;
- }
- /**
- * Favorites the supplied tweet
- *
- * @param string $id
- * @param string $userid
- * @return array
- */
- function favorite($id, $userid = "00000"){
- if(!$this->hasbeenignored($userid)){
- $favorite = $this->connection->post('favorites/create', array("id" => $id));
- }
- return $favorite;
- }
- /**
- * Searches twitter with the supplied search query
- *
- * @param string $query
- * @param int $count
- * @param string $lang
- * @param string $resulttype
- * @return array
- */
- function search($query, $count = 60, $lang = "en", $resulttype = "recent"){
- $search = $this->connection->get('search/tweets', array('q' => $query, 'count' => $count, 'lang' => $lang, 'result_type' => $resulttype));
- return $search;
- }
- /**
- * Runs the supplied function with the supplied tweets
- *
- * This is used to run the bot. You create a function to do what your bot needs to do and this function will call that function with the tweets
- *
- * @param string $function
- * @param array $tweets
- * @return mixed
- */
- function action($function, $tweets){
- return $function($tweets);
- }
- /**
- * Follow the supplied user
- *
- * @param string $id
- * @return array
- */
- function follow($id){
- if(!$this->hasbeenignored($id)){
- $follow = $this->connection->post('friendships/create', array("user_id" => $id));
- }
- return $follow;
- }
- /**
- * Checks if user is in the blacklist
- *
- * @param string $id
- * @return bool
- */
- function inblacklist($id){
- if(in_array($id, $this->blacklist)){
- return true;
- }
- else{
- return false;
- }
- return $follow;
- }
- /**
- * Checks if the user has been ignored
- *
- * @param string $userid
- * @return bool
- */
- function hasbeenignored($userid){
- global $ignorelist;
- $ignoreedsearch = $ignorelist->find(array("user" => $userid));
- $ignoreedsearch = iterator_to_array($ignoreedsearch);
- if(empty($ignoreedsearch)){
- return false;
- }
- else{
- return true;
- }
- }
- /**
- * Checks the last 200 mentions to see if they want to be ignored
- *
- * @param array $words
- * @param string $thereply
- */
- function checkignore($words, $thereply){
- global $ignorelist;
- $mentions = $this->connection->get('statuses/mentions_timeline', array('count' => '200'));
- foreach($mentions as $mention){
- if($this->instring($words, strtolower($mention->text)) && !$this->hasbeenignored($mention->user->id_str)){
- $reply = $this->reply("@".$mention->user->screen_name.", ".$thereply, $mention->id_str, $mention->user->id_str);
- $ignorelist->insert(array("user" => $mention->user->id_str));
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement