Forked from Daniel15/1_Twitter autoresponder bot.md
Created
October 17, 2013 03:27
-
-
Save SkillfulHacking/7018834 to your computer and use it in GitHub Desktop.
Revisions
-
Daniel15 revised this gist
Jun 17, 2013 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,7 @@ class TwitterAutoReply // Variables private $_replies = array(); private $_oauth; private $_screenName; // Methods public function __construct($consumer_key, $consumer_secret) @@ -67,6 +68,10 @@ public function run() // Now let's go through the results foreach ($search->statuses as $tweet) { // Ensure we don't reply to ourself! if ($tweet->user->screen_name == $this->_screenName) continue; $this->sendReply($tweet, $reply); sleep(1); } @@ -97,7 +102,9 @@ private function verifyAccountWorks() { try { $this->_oauth->fetch(self::VERIFY_URL, array(), OAUTH_HTTP_METHOD_GET); $response = json_decode($this->_oauth->getLastResponse()); $this->_screenName = $response->screen_name; return true; } catch (Exception $ex) -
Daniel15 revised this gist
Jun 13, 2013 . 3 changed files with 17 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,4 +5,6 @@ This is a very simple Twitter autoresponder bot. It requires PECL OAuth extensio Could be modified to be more advanced (match regular expressions to answer questions, etc.) Questions? See my blog post - http://dan.cx/blog/2011/06/twitter-autoreply-bot-dbznappa Modified 2013-06-13 - Twitter API 1.0 discontinued, modified to use Twitter API 1.1 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,14 @@ <?php // Twitter autoreply // By Daniel15 <http://dan.cx/> // Modified 2013-06-13 to support Twitter API 1.1 class TwitterAutoReply { // Constants const SEARCH_URL = 'https://api.twitter.com/1.1/search/tweets.json?q=%s&since_id=%s'; const UPDATE_URL = 'https://api.twitter.com/1.1/statuses/update.json'; const VERIFY_URL = 'https://api.twitter.com/1.1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; const ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'; const AUTH_URL = 'http://twitter.com/oauth/authorize'; @@ -56,16 +57,18 @@ public function run() foreach ($this->_replies as $term => $reply) { echo 'Performing search for "', $term, '"... '; $this->_oauth->fetch(sprintf(self::SEARCH_URL, urlencode($term), $since_id)); $search = json_decode($this->_oauth->getLastResponse()); echo 'Done, ', count($search->statuses), " results.\n"; // Store the max ID if ($search->search_metadata->max_id_str > $max_id) $max_id = $search->search_metadata->max_id_str; // Now let's go through the results foreach ($search->statuses as $tweet) { $this->sendReply($tweet, $reply); sleep(1); } } @@ -77,9 +80,9 @@ private function sendReply($tweet, $reply) { try { echo '@', $tweet->user->screen_name, ' said: ', $tweet->text, "\n"; $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->user->screen_name . ' ' . $reply, 'in_reply_to_status_id' => $tweet->id_str, ), OAUTH_HTTP_METHOD_POST); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/usr/bin/php <?php date_default_timezone_set('Australia/Melbourne'); require('TwitterAutoReply.php'); // Consumer key and consumer secret $twitter = new TwitterAutoReply('******', '************************'); -
Daniel15 revised this gist
Mar 24, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,4 +3,6 @@ Twitter autoresponder bot By Daniel15 (dan.cx) This is a very simple Twitter autoresponder bot. It requires PECL OAuth extension to be installed (run "pecl install oauth", or if on Windows, grab php-oauth.dll. If using cPanel you can install it via WHM). The authentication is designed for command-line usage, it won't work too well via a web browser. You'll have to sign up for an application on Twitter's site to get the consumer key and secret. Could be modified to be more advanced (match regular expressions to answer questions, etc.) Questions? See my blog post - http://dan.cx/blog/2011/06/twitter-autoreply-bot-dbznappa -
Daniel15 renamed this gist
Dec 16, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Daniel15 revised this gist
Oct 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class TwitterAutoReply { // Constants const SEARCH_URL = 'http://search.twitter.com/search.json?q=%s&since_id=%s'; const UPDATE_URL = 'https://api.twitter.com/1/statuses/update.json'; const VERIFY_URL = 'http://api.twitter.com/1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; -
Daniel15 revised this gist
Oct 13, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ class TwitterAutoReply { // Constants const SEARCH_URL = 'http://search.twitter.com/search.json?q=%s&since_id_str=%s'; const UPDATE_URL = 'https://api.twitter.com/1/statuses/update.json'; const VERIFY_URL = 'http://api.twitter.com/1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; const ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'; -
Daniel15 revised this gist
Oct 13, 2012 . 1 changed file with 14 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class TwitterAutoReply { // Constants const SEARCH_URL = 'http://search.twitter.com/search.json?q=%s&since_id_str=%s'; const UPDATE_URL = 'https://twitter.com/statuses/update.json'; const VERIFY_URL = 'http://api.twitter.com/1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; @@ -75,11 +75,19 @@ public function run() private function sendReply($tweet, $reply) { try { echo '@', $tweet->from_user, ' said: ', $tweet->text, "\n"; $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->from_user . ' ' . $reply, 'in_reply_to_status_id' => $tweet->id_str, ), OAUTH_HTTP_METHOD_POST); } catch (OAuthException $ex) { echo 'ERROR: ' . $ex->lastResponse; die(); } } private function verifyAccountWorks() -
Daniel15 revised this gist
Jun 11, 2011 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,7 +78,6 @@ private function sendReply($tweet, $reply) echo '@', $tweet->from_user, ' said: ', $tweet->text, "\n"; $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->from_user . ' ' . $reply, 'in_reply_to_status_id' => $tweet->id_str, ), OAUTH_HTTP_METHOD_POST); } -
Daniel15 revised this gist
Jun 11, 2011 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,8 +59,8 @@ public function run() $search = json_decode(file_get_contents(sprintf(self::SEARCH_URL, urlencode($term), $since_id))); echo 'Done, ', count($search->results), " results.\n"; // Store the max ID if ($search->max_id_str > $max_id) $max_id = $search->max_id_str; // Now let's go through the results foreach ($search->results as $tweet) @@ -79,7 +79,7 @@ private function sendReply($tweet, $reply) $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->from_user . ' ' . $reply, // Seemed to crash with numbers here... 'in_reply_to_status_id' => $tweet->id_str, ), OAUTH_HTTP_METHOD_POST); } Empty file. -
Daniel15 revised this gist
Feb 17, 2011 . 1 changed file with 117 additions and 117 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,124 +4,124 @@ class TwitterAutoReply { // Constants const SEARCH_URL = 'http://search.twitter.com/search.json?q=%s&since_id=%s'; const UPDATE_URL = 'https://twitter.com/statuses/update.json'; const VERIFY_URL = 'http://api.twitter.com/1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; const ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'; const AUTH_URL = 'http://twitter.com/oauth/authorize'; // Variables private $_replies = array(); private $_oauth; // Methods public function __construct($consumer_key, $consumer_secret) { $this->_oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI); $this->_oauth->disableSSLChecks(); } public function setToken($token, $secret) { $this->_oauth->setToken($token, $secret); } public function addReply($term, $reply) { $this->_replies[$term] = $reply; } public function run() { echo '========= ', date('Y-m-d g:i:s A'), " - Started =========\n"; // Get the last ID we replied to $since_id = @file_get_contents('since_id'); if ($since_id == null) $since_id = 0; // This will store the ID of the last tweet we get $max_id = $since_id; // Verify their Twitter account is valid if (!$this->verifyAccountWorks()) { // Get a request token and show instructions $this->doAuth(); die(); } // Go through each reply foreach ($this->_replies as $term => $reply) { echo 'Performing search for "', $term, '"... '; $search = json_decode(file_get_contents(sprintf(self::SEARCH_URL, urlencode($term), $since_id))); echo 'Done, ', count($search->results), " results.\n"; // Store the max ID if ($search->max_id_str > $max_id) $max_id = $search->max_id_str; // Now let's go through the results foreach ($search->results as $tweet) { $this->sendReply($tweet, $reply); } } file_put_contents('since_id', $max_id); echo '========= ', date('Y-m-d g:i:s A'), " - Finished =========\n"; } private function sendReply($tweet, $reply) { echo '@', $tweet->from_user, ' said: ', $tweet->text, "\n"; $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->from_user . ' ' . $reply, // Seemed to crash with numbers here... 'in_reply_to_status_id' => (string) $tweet->id, ), OAUTH_HTTP_METHOD_POST); } private function verifyAccountWorks() { try { $response = $this->_oauth->fetch(self::VERIFY_URL, array(), OAUTH_HTTP_METHOD_GET); return true; } catch (Exception $ex) { return false; } } private function doAuth() { // First get a request token, and prompt them to go to the URL $request_token_info = $this->_oauth->getRequestToken(self::REQUEST_TOKEN_URL); echo 'Please navigate to the following URL to get an authentication token:', "\n"; echo self::AUTH_URL, '?oauth_token=', $request_token_info['oauth_token'], "\n"; echo 'Once done (and you have a PIN number), press ENTER.'; fread(STDIN, 10); echo 'PIN Number: '; $pin = trim(fread(STDIN, 10)); // Now let's swap that for an access token $this->_oauth->setToken($request_token_info['oauth_token'], $request_token_info['oauth_token_secret']); $access_token_info = $this->_oauth->getAccessToken(self::ACCESS_TOKEN_URL, null, $pin); echo 'Success, ', $access_token_info['screen_name'], ' has authorized the application. Please change your setToken line to something like the following:', "\n"; echo '$twitter->setToken(\'', $access_token_info['oauth_token'], '\', \'', $access_token_info['oauth_token_secret'], '\');'; die(); } public function testTweet() { $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => 'Test from TwitterAutoReply', ), OAUTH_HTTP_METHOD_POST); } } ?> -
Daniel15 revised this gist
Feb 10, 2011 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,8 @@ public function run() $search = json_decode(file_get_contents(sprintf(self::SEARCH_URL, urlencode($term), $since_id))); echo 'Done, ', count($search->results), " results.\n"; // Store the max ID if ($search->max_id_str > $max_id) $max_id = $search->max_id_str; // Now let's go through the results foreach ($search->results as $tweet) -
Daniel15 revised this gist
Feb 10, 2011 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ Twitter autoresponder bot ========================= By Daniel15 (dan.cx) This is a very simple Twitter autoresponder bot. It requires PECL OAuth extension to be installed (run "pecl install oauth", or if on Windows, grab php-oauth.dll. If using cPanel you can install it via WHM). The authentication is designed for command-line usage, it won't work too well via a web browser. You'll have to sign up for an application on Twitter's site to get the consumer key and secret. Could be modified to be more advanced (match regular expressions to answer questions, etc.) Empty file. -
Daniel15 created this gist
Feb 10, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ Twitter autoresponder bot By Daniel15 (dan.cx) ========================= This is a very simple Twitter autoresponder bot. It requires PECL OAuth extension to be installed (run "pecl install oauth", or if on Windows, grab php-oauth.dll. If using cPanel you can install it via WHM). The authentication is designed for command-line usage, it won't work too well via a web browser. You'll have to sign up for an application on Twitter's site to get the consumer key and secret. Could be modified to be more advanced (match regular expressions to answer questions, etc.) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,126 @@ <?php // Twitter autoreply // By Daniel15 <http://dan.cx/> class TwitterAutoReply { // Constants const SEARCH_URL = 'http://search.twitter.com/search.json?q=%s&since_id=%s'; const UPDATE_URL = 'https://twitter.com/statuses/update.json'; const VERIFY_URL = 'http://api.twitter.com/1/account/verify_credentials.json'; const REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'; const ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'; const AUTH_URL = 'http://twitter.com/oauth/authorize'; // Variables private $_replies = array(); private $_oauth; // Methods public function __construct($consumer_key, $consumer_secret) { $this->_oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI); $this->_oauth->disableSSLChecks(); } public function setToken($token, $secret) { $this->_oauth->setToken($token, $secret); } public function addReply($term, $reply) { $this->_replies[$term] = $reply; } public function run() { echo '========= ', date('Y-m-d g:i:s A'), " - Started =========\n"; // Get the last ID we replied to $since_id = @file_get_contents('since_id'); if ($since_id == null) $since_id = 0; // This will store the ID of the last tweet we get $max_id = $since_id; // Verify their Twitter account is valid if (!$this->verifyAccountWorks()) { // Get a request token and show instructions $this->doAuth(); die(); } // Go through each reply foreach ($this->_replies as $term => $reply) { echo 'Performing search for "', $term, '"... '; $search = json_decode(file_get_contents(sprintf(self::SEARCH_URL, urlencode($term), $since_id))); echo 'Done, ', count($search->results), " results.\n"; // Store the max ID $max_id = max($max_id, $search->max_id); // Now let's go through the results foreach ($search->results as $tweet) { $this->sendReply($tweet, $reply); } } file_put_contents('since_id', $max_id); echo '========= ', date('Y-m-d g:i:s A'), " - Finished =========\n"; } private function sendReply($tweet, $reply) { echo '@', $tweet->from_user, ' said: ', $tweet->text, "\n"; $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => '@' . $tweet->from_user . ' ' . $reply, // Seemed to crash with numbers here... 'in_reply_to_status_id' => (string) $tweet->id, ), OAUTH_HTTP_METHOD_POST); } private function verifyAccountWorks() { try { $response = $this->_oauth->fetch(self::VERIFY_URL, array(), OAUTH_HTTP_METHOD_GET); return true; } catch (Exception $ex) { return false; } } private function doAuth() { // First get a request token, and prompt them to go to the URL $request_token_info = $this->_oauth->getRequestToken(self::REQUEST_TOKEN_URL); echo 'Please navigate to the following URL to get an authentication token:', "\n"; echo self::AUTH_URL, '?oauth_token=', $request_token_info['oauth_token'], "\n"; echo 'Once done (and you have a PIN number), press ENTER.'; fread(STDIN, 10); echo 'PIN Number: '; $pin = trim(fread(STDIN, 10)); // Now let's swap that for an access token $this->_oauth->setToken($request_token_info['oauth_token'], $request_token_info['oauth_token_secret']); $access_token_info = $this->_oauth->getAccessToken(self::ACCESS_TOKEN_URL, null, $pin); echo 'Success, ', $access_token_info['screen_name'], ' has authorized the application. Please change your setToken line to something like the following:', "\n"; echo '$twitter->setToken(\'', $access_token_info['oauth_token'], '\', \'', $access_token_info['oauth_token_secret'], '\');'; die(); } public function testTweet() { $this->_oauth->fetch(self::UPDATE_URL, array( 'status' => 'Test from TwitterAutoReply', ), OAUTH_HTTP_METHOD_POST); } } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ #!/usr/bin/php <?php date_default_timezone_set('Australia/Melbourne'); require('twitterautoreply.php'); // Consumer key and consumer secret $twitter = new TwitterAutoReply('******', '************************'); // Token and secret $twitter->setToken('********-*******************', '*****************8'); $twitter->addReply('over 9000', 'WHAT?! NINE THOUSAND?!'); $twitter->addReply('over nine thousand', 'WHAT?! NINE THOUSAND?!'); $twitter->run(); ?>