Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. <?php
  2.  
  3. class twitterbot {
  4.  
  5. var $connection;
  6. var $blacklist = array();
  7.  
  8. var $m = null;
  9. var $db = null;
  10. var $ignorelist = null;
  11. var $botid = null;
  12.  
  13. /**
  14. * If any array element is in the string it returns true
  15. *
  16. * If the string contains a word/string of characters that is in the array it returns true
  17. *
  18. * @param array $inArray array of words to check for
  19. * @param string $inString the string to check for the words in
  20. * @return bool
  21. * @author http://stackoverflow.com/a/10137763/3349060
  22. */
  23. function instring($inArray, $inString){
  24.  
  25. if(is_array($inArray)){
  26. foreach($inArray as $e){
  27. if(strpos($inString, $e)!==false)
  28. return true;
  29. }
  30. return false;
  31. }else{
  32. return (strpos( $inString , $inArray )!==false);
  33. }
  34.  
  35. }
  36.  
  37. /**
  38. * Sets up the bot with the twitter account
  39. *
  40. * Authenticates with twitter using https://github.com/abraham/twitteroauth
  41. *
  42. * @param string $consumerkey Consumer key for Twitter Application
  43. * @param string $consumersecret Consumer secret for Twitter Application
  44. * @param string $secretkey Secret key for Twitter Application
  45. * @param string $secretsecret Secret Secret for Twitter Application
  46. * @return TwitterOAuth
  47. */
  48. function connect($consumerkey, $consumersecret, $secretkey, $secretsecret){
  49. global $ignorelist;
  50.  
  51. $this->connection = new TwitterOAuth($consumerkey, $consumersecret, $secretkey, $secretsecret);
  52. $botid = $this->connection->get('account/verify_credentials')->id_str;
  53.  
  54. $m = new MongoClient();
  55. $db = $m->botsignorelist;
  56. $ignorelist = $db->$botid;
  57.  
  58. return $this->connection;
  59.  
  60. }
  61.  
  62. /**
  63. * Adds user to blacklist
  64. *
  65. * @param string $userids
  66. * @return array
  67. */
  68. function addblacklist($userids){
  69.  
  70. $this->blacklist = array_merge($this->blacklist, $userids);
  71.  
  72. return $this->blacklist;
  73.  
  74. }
  75.  
  76. /**
  77. * Tweets the supplied msg
  78. *
  79. * @param string $msg
  80. * @param string $userid
  81. * @return array
  82. */
  83. function tweet($msg, $userid = "00000"){
  84.  
  85. if(!$this->hasbeenignored($userid)){
  86. $tweet = $this->connection->post('statuses/update', array("status" => $msg));
  87. return $tweet;
  88. }
  89.  
  90. }
  91.  
  92. /**
  93. * Replys to with $msg to the supplied tweet
  94. *
  95. * @param string $msg
  96. * @param string $to
  97. * @param string $userid
  98. * @return array
  99. */
  100. function reply($msg, $to, $userid = "00000"){
  101.  
  102. if(!$this->hasbeenignored($userid)){
  103. $reply = $this->connection->post('statuses/update', array("status" => $msg, "in_reply_to_status_id" => $to));
  104. return $reply;
  105. }
  106.  
  107. }
  108.  
  109. /**
  110. * Retweets the supplied tweet
  111. *
  112. * @param string $id
  113. * @param string $userid
  114. * @return array
  115. */
  116. function retweet($id, $userid = "00000"){
  117.  
  118. if(!$this->hasbeenignored($userid)){
  119. $retweet = $this->connection->post('statuses/retweet/'.$id);
  120. }
  121.  
  122. return $retweet;
  123. }
  124.  
  125. /**
  126. * Favorites the supplied tweet
  127. *
  128. * @param string $id
  129. * @param string $userid
  130. * @return array
  131. */
  132. function favorite($id, $userid = "00000"){
  133.  
  134. if(!$this->hasbeenignored($userid)){
  135. $favorite = $this->connection->post('favorites/create', array("id" => $id));
  136. }
  137.  
  138. return $favorite;
  139. }
  140.  
  141. /**
  142. * Searches twitter with the supplied search query
  143. *
  144. * @param string $query
  145. * @param int $count
  146. * @param string $lang
  147. * @param string $resulttype
  148. * @return array
  149. */
  150. function search($query, $count = 60, $lang = "en", $resulttype = "recent"){
  151.  
  152. $search = $this->connection->get('search/tweets', array('q' => $query, 'count' => $count, 'lang' => $lang, 'result_type' => $resulttype));
  153.  
  154. return $search;
  155. }
  156.  
  157. /**
  158. * Runs the supplied function with the supplied tweets
  159. *
  160. * 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
  161. *
  162. * @param string $function
  163. * @param array $tweets
  164. * @return mixed
  165. */
  166. function action($function, $tweets){
  167.  
  168. return $function($tweets);
  169.  
  170. }
  171.  
  172. /**
  173. * Follow the supplied user
  174. *
  175. * @param string $id
  176. * @return array
  177. */
  178. function follow($id){
  179.  
  180. if(!$this->hasbeenignored($id)){
  181. $follow = $this->connection->post('friendships/create', array("user_id" => $id));
  182. }
  183.  
  184. return $follow;
  185.  
  186. }
  187.  
  188. /**
  189. * Checks if user is in the blacklist
  190. *
  191. * @param string $id
  192. * @return bool
  193. */
  194. function inblacklist($id){
  195.  
  196. if(in_array($id, $this->blacklist)){
  197. return true;
  198. }
  199. else{
  200. return false;
  201. }
  202.  
  203. return $follow;
  204.  
  205. }
  206.  
  207. /**
  208. * Checks if the user has been ignored
  209. *
  210. * @param string $userid
  211. * @return bool
  212. */
  213. function hasbeenignored($userid){
  214. global $ignorelist;
  215.  
  216. $ignoreedsearch = $ignorelist->find(array("user" => $userid));
  217. $ignoreedsearch = iterator_to_array($ignoreedsearch);
  218.  
  219. if(empty($ignoreedsearch)){
  220. return false;
  221. }
  222. else{
  223. return true;
  224. }
  225. }
  226.  
  227. /**
  228. * Checks the last 200 mentions to see if they want to be ignored
  229. *
  230. * @param array $words
  231. * @param string $thereply
  232. */
  233. function checkignore($words, $thereply){
  234. global $ignorelist;
  235.  
  236. $mentions = $this->connection->get('statuses/mentions_timeline', array('count' => '200'));
  237.  
  238. foreach($mentions as $mention){
  239. if($this->instring($words, strtolower($mention->text)) && !$this->hasbeenignored($mention->user->id_str)){
  240. $reply = $this->reply("@".$mention->user->screen_name.", ".$thereply, $mention->id_str, $mention->user->id_str);
  241. $ignorelist->insert(array("user" => $mention->user->id_str));
  242. }
  243.  
  244. }
  245.  
  246. }
  247.  
  248. }
  249.  
  250. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement