Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter autoresponder bot

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

Modified 2013-06-13 - Twitter API 1.0 discontinued, modified to use Twitter API 1.1

#!/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();
?>
<?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';
// Variables
private $_replies = array();
private $_oauth;
private $_screenName;
// 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, '"... ';
$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)
{
// Ensure we don't reply to ourself!
if ($tweet->user->screen_name == $this->_screenName)
continue;
$this->sendReply($tweet, $reply);
sleep(1);
}
}
file_put_contents('since_id', $max_id);
echo '========= ', date('Y-m-d g:i:s A'), " - Finished =========\n";
}
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);
}
catch (OAuthException $ex)
{
echo 'ERROR: ' . $ex->lastResponse;
die();
}
}
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)
{
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);
}
}
?>
@bers99

This comment has been minimized.

Show comment
Hide comment
@bers99

bers99 Aug 29, 2011

How to do it without Pecl OAuth? I'm php newbie... sorry... )

bers99 commented Aug 29, 2011

How to do it without Pecl OAuth? I'm php newbie... sorry... )

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Aug 30, 2011

Find a PHP library that does OAuth (take a look at http://oauth.net/code/) and replace all $this->_oauth calls with calls to the new library. You only need to worry about __construct and sendReply, the other functions aren't used normally.

Owner

Daniel15 commented Aug 30, 2011

Find a PHP library that does OAuth (take a look at http://oauth.net/code/) and replace all $this->_oauth calls with calls to the new library. You only need to worry about __construct and sendReply, the other functions aren't used normally.

@bers99

This comment has been minimized.

Show comment
Hide comment
@bers99

bers99 Sep 1, 2011

I installed ubuntu, php5, apache2
Correctcly installed pecl oauth (i saw it in my phpinfo - all work fine)
But get an error :
PHP Fatal error: Class 'OAuth' not found in /var/www/twbot/twitterautoreply.php on line 22

Maybe class have another name? Can u send your oauth classes?

bers99 commented Sep 1, 2011

I installed ubuntu, php5, apache2
Correctcly installed pecl oauth (i saw it in my phpinfo - all work fine)
But get an error :
PHP Fatal error: Class 'OAuth' not found in /var/www/twbot/twitterautoreply.php on line 22

Maybe class have another name? Can u send your oauth classes?

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Sep 1, 2011

Hm, very odd! Did you restart your web server after installing the PECL OAuth extension? Maybe try the example in the PHP OAuth documentation (http://au.php.net/manual/en/oauth.examples.php) and see if that works?

Owner

Daniel15 commented Sep 1, 2011

Hm, very odd! Did you restart your web server after installing the PECL OAuth extension? Maybe try the example in the PHP OAuth documentation (http://au.php.net/manual/en/oauth.examples.php) and see if that works?

@bers99

This comment has been minimized.

Show comment
Hide comment
@bers99

bers99 Sep 1, 2011

I need to manually set this parameters ?
// Consumer key and consumer secret
$twitter = new TwitterAutoReply('_', '__');
// Token and secret
$twitter->setToken('
****-
_
_', '_**************8');

bers99 commented Sep 1, 2011

I need to manually set this parameters ?
// Consumer key and consumer secret
$twitter = new TwitterAutoReply('_', '__');
// Token and secret
$twitter->setToken('
****-
_
_', '_**************8');

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Sep 1, 2011

Yes you do. Create an application at https://dev.twitter.com/apps and you'll get all the parameters. Here's a screenshot of where all the values go:

This will post under the user you log in as when you create the app. To post with a different user, set the token and secret to empty values (in the setToken call), and run the script. It'll prompt you on how to authenticate.

Owner

Daniel15 commented Sep 1, 2011

Yes you do. Create an application at https://dev.twitter.com/apps and you'll get all the parameters. Here's a screenshot of where all the values go:

This will post under the user you log in as when you create the app. To post with a different user, set the token and secret to empty values (in the setToken call), and run the script. It'll prompt you on how to authenticate.

@bers99

This comment has been minimized.

Show comment
Hide comment
@bers99

bers99 Sep 2, 2011

I restarted my server, but same error about OAuth class... Dont know what to do
And same error with example

bers99 commented Sep 2, 2011

I restarted my server, but same error about OAuth class... Dont know what to do
And same error with example

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Sep 4, 2011

Hmm. Not too sure then. Sounds like some sort of configuration issue.
You could try a pure PHP OAuth implementation. I don't have time to implement that, unfortunately.

Owner

Daniel15 commented Sep 4, 2011

Hmm. Not too sure then. Sounds like some sort of configuration issue.
You could try a pure PHP OAuth implementation. I don't have time to implement that, unfortunately.

@Mestodonte

This comment has been minimized.

Show comment
Hide comment
@Mestodonte

Mestodonte Jan 29, 2012

I modified the code is now?

I modified the code is now?

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Mar 8, 2012

Owner

Daniel15 commented Mar 8, 2012

@rlamb2

This comment has been minimized.

Show comment
Hide comment
@rlamb2

rlamb2 Mar 16, 2012

Hey Daniel,

Great work! Very easy to read and by following the comments here I was able to implement an alternative to PECL OAuth since my Dreamhost account doesn't seem to have it available.

I swapped it out for tmhOAuth by downloading the source files (found here https://github.com/themattharris/tmhOAuth), including them at the top:

require_once('tmhUtilities.php');
require_once('tmhOAuth.php');

and swapping out the following in Daniels twitterautoreply.php:

in the __construct delete everything and paste the following:

$this->_oauth = new tmhOAuth(array(
'consumer_key' => '',
'consumer_secret' => '',
'user_token' => '',
'user_secret' => '',
'curl_ssl_verifypeer' => false,
));

and instead of using the fetch command (lines 79 & 89) you'll need to re-order the variables and feed it to the request command in tmhoauth. So at line 79 (of the source, will be different if you added the above code) switch this:

$this->_oauth->fetch(self::UPDATE_URL, array(
'status' => '@' . $tweet->from_user . ' ' . $reply,
'in_reply_to_status_id' => $tweet->id_str,
), OAUTH_HTTP_METHOD_POST);

with this:

$this->_oauth->request('POST',$this->_oauth->url('1/statuses/update'), array(
'status' => '@' . $tweet->from_user . ' ' . $reply,
'in_reply_to_status_id' => $tweet->id_str,
));

and at line 89 switch this:

$response = $this->_oauth->fetch(self::VERIFY_URL, array(), OAUTH_HTTP_METHOD_GET);

with this:

$response = $this->_oauth->request('GET',$this->_oauth->url('1/statuses/update'), array());

Hope this helps someone! Feel free to follow me @russ152 if you have any questions, suggestions.

Thanks again Daniel!

rlamb2 commented Mar 16, 2012

Hey Daniel,

Great work! Very easy to read and by following the comments here I was able to implement an alternative to PECL OAuth since my Dreamhost account doesn't seem to have it available.

I swapped it out for tmhOAuth by downloading the source files (found here https://github.com/themattharris/tmhOAuth), including them at the top:

require_once('tmhUtilities.php');
require_once('tmhOAuth.php');

and swapping out the following in Daniels twitterautoreply.php:

in the __construct delete everything and paste the following:

$this->_oauth = new tmhOAuth(array(
'consumer_key' => '',
'consumer_secret' => '',
'user_token' => '',
'user_secret' => '',
'curl_ssl_verifypeer' => false,
));

and instead of using the fetch command (lines 79 & 89) you'll need to re-order the variables and feed it to the request command in tmhoauth. So at line 79 (of the source, will be different if you added the above code) switch this:

$this->_oauth->fetch(self::UPDATE_URL, array(
'status' => '@' . $tweet->from_user . ' ' . $reply,
'in_reply_to_status_id' => $tweet->id_str,
), OAUTH_HTTP_METHOD_POST);

with this:

$this->_oauth->request('POST',$this->_oauth->url('1/statuses/update'), array(
'status' => '@' . $tweet->from_user . ' ' . $reply,
'in_reply_to_status_id' => $tweet->id_str,
));

and at line 89 switch this:

$response = $this->_oauth->fetch(self::VERIFY_URL, array(), OAUTH_HTTP_METHOD_GET);

with this:

$response = $this->_oauth->request('GET',$this->_oauth->url('1/statuses/update'), array());

Hope this helps someone! Feel free to follow me @russ152 if you have any questions, suggestions.

Thanks again Daniel!

@rlamb2

This comment has been minimized.

Show comment
Hide comment
@rlamb2

rlamb2 Jul 11, 2012

rlamb2 commented Jul 11, 2012

@madebyreformat

This comment has been minimized.

Show comment
Hide comment
@madebyreformat

madebyreformat Nov 29, 2012

I've got this running as an auto reply to anyone that has mentioned a specific account but it keeps replying to same ones over and over. Anyone got an idea how i can stop this?

I've got this running as an auto reply to anyone that has mentioned a specific account but it keeps replying to same ones over and over. Anyone got an idea how i can stop this?

@jamesinealing

This comment has been minimized.

Show comment
Hide comment
@jamesinealing

jamesinealing Dec 13, 2012

@BenPalmer have you checked that it is successfully created the file 'since_id'? And the contents?

Anyway, the reason I was here was to thank Daniel for posting this - it just gave me a few leads to get something very similar up using Twitter-Async. I also dropped a conditional ... if (!$tweet->in_reply_to_status_id) ... into the innermost foreach loop so that it didn't auto-reply to @replies, only to brand new tweets. Nice and simple.

@BenPalmer have you checked that it is successfully created the file 'since_id'? And the contents?

Anyway, the reason I was here was to thank Daniel for posting this - it just gave me a few leads to get something very similar up using Twitter-Async. I also dropped a conditional ... if (!$tweet->in_reply_to_status_id) ... into the innermost foreach loop so that it didn't auto-reply to @replies, only to brand new tweets. Nice and simple.

@iskigow

This comment has been minimized.

Show comment
Hide comment
@iskigow

iskigow Feb 1, 2013

Pretty cool!! A year ago I forked and implemented a method to match regexp. Today I change the method 'sendReply' of my fork to match your one.

This bot still work?! Did you test lately?

iskigow commented Feb 1, 2013

Pretty cool!! A year ago I forked and implemented a method to match regexp. Today I change the method 'sendReply' of my fork to match your one.

This bot still work?! Did you test lately?

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Mar 24, 2013

@iskigow Yes, the bot is still functioning :) See http://twitter.com/DBZNappa

Owner

Daniel15 commented Mar 24, 2013

@iskigow Yes, the bot is still functioning :) See http://twitter.com/DBZNappa

@jasonclemons

This comment has been minimized.

Show comment
Hide comment
@jasonclemons

jasonclemons Apr 3, 2013

Hey Daniel, just an FYI that Twitter is deprecating v1 of the API very soon. Will need to update the URLs to fit.

Hey Daniel, just an FYI that Twitter is deprecating v1 of the API very soon. Will need to update the URLs to fit.

@nobitargh

This comment has been minimized.

Show comment
Hide comment
@nobitargh

nobitargh May 24, 2013

11 days to update the API 1.1 URL, right?

11 days to update the API 1.1 URL, right?

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Jun 17, 2013

Sorry about the delay @jasonclemons and @nobitargh, I updated the bot for v1.1 of the API a few days ago.

Owner

Daniel15 commented Jun 17, 2013

Sorry about the delay @jasonclemons and @nobitargh, I updated the bot for v1.1 of the API a few days ago.

@billywall

This comment has been minimized.

Show comment
Hide comment
@billywall

billywall Jun 29, 2013

Daniel, do you know any way to enable multiple bots simultaneously instead of one? In the case here, each bot wheel separately. Is there any way when the 1000 replyes are exhausted, another bot take?

Daniel, do you know any way to enable multiple bots simultaneously instead of one? In the case here, each bot wheel separately. Is there any way when the 1000 replyes are exhausted, another bot take?

@susomena

This comment has been minimized.

Show comment
Hide comment
@susomena

susomena Jul 1, 2013

Hi, Daniel, how do you manage to to avoid your account being suspended?

susomena commented Jul 1, 2013

Hi, Daniel, how do you manage to to avoid your account being suspended?

@GloriaHughes

This comment has been minimized.

Show comment
Hide comment
@GloriaHughes

GloriaHughes Sep 24, 2013

Per your instructions, I did everything correctly but... I'm getting a PHP: Error parsing on line 3:
1./usr/bin/php
2.<?php
3.date_default_timezone_set('Australia/Melbourne');
4. require('TwitterAutoReply.php');

???

It's the "date_default_timezone_set('Australia/Melbourne');"

Help!

Per your instructions, I did everything correctly but... I'm getting a PHP: Error parsing on line 3:
1./usr/bin/php
2.<?php
3.date_default_timezone_set('Australia/Melbourne');
4. require('TwitterAutoReply.php');

???

It's the "date_default_timezone_set('Australia/Melbourne');"

Help!

@Turkzilla

This comment has been minimized.

Show comment
Hide comment
@Turkzilla

Turkzilla Oct 7, 2013

ERROR: {"errors":[{"code":86,"message":"This method requires a POST."}]}

ERROR: {"errors":[{"code":86,"message":"This method requires a POST."}]}

@sluxzer

This comment has been minimized.

Show comment
Hide comment
@sluxzer

sluxzer Oct 18, 2013

helo sir, i wanna ask,
whether it should be i upload to host? just it,right?

and where must i changes?

sluxzer commented Oct 18, 2013

helo sir, i wanna ask,
whether it should be i upload to host? just it,right?

and where must i changes?

@devmnnit

This comment has been minimized.

Show comment
Hide comment
@devmnnit

devmnnit Jan 8, 2014

Most of time i am getting this error
ERROR: {"errors":[{"code":187,"message":"Status is a duplicate."}]}
How i can ignore this..??

devmnnit commented Jan 8, 2014

Most of time i am getting this error
ERROR: {"errors":[{"code":187,"message":"Status is a duplicate."}]}
How i can ignore this..??

@Xilver266

This comment has been minimized.

Show comment
Hide comment
@Xilver266

Xilver266 Jan 9, 2014

I get this errors :S
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 123

Warning: fread() expects parameter 1 to be resource, string given in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 123
PIN Number:
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 126

Warning: fread() expects parameter 1 to be resource, string given in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 126

Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php:130 Stack trace: #0 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php(130): OAuth->getAccessToken('https://twitter...', '', '') #1 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php(53): TwitterAutoReply->doAuth() #2 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\example.php(12): TwitterAutoReply->run() #3 {main} thrown in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 130

I get this errors :S
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 123

Warning: fread() expects parameter 1 to be resource, string given in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 123
PIN Number:
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 126

Warning: fread() expects parameter 1 to be resource, string given in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 126

Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php:130 Stack trace: #0 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php(130): OAuth->getAccessToken('https://twitter...', '', '') #1 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php(53): TwitterAutoReply->doAuth() #2 C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\example.php(12): TwitterAutoReply->run() #3 {main} thrown in C:\Users\USER\Dropbox\Documentos\Desarrollo Web\xampp\htdocs\twitterbot\TwitterAutoReply.php on line 130

@devmnnit

This comment has been minimized.

Show comment
Hide comment
@devmnnit

devmnnit Oct 4, 2014

how i can do this with Twitter streaming API's.......I want to use streaming API's. Please suggest me..???

devmnnit commented Oct 4, 2014

how i can do this with Twitter streaming API's.......I want to use streaming API's. Please suggest me..???

@lionx

This comment has been minimized.

Show comment
Hide comment
@lionx

lionx Oct 24, 2014

Hi Daniel..

Can it also work for shared hosting like Hawkhost? I don't know exactly if it has something like PECL OAuth or SSH Access.. THanks in advance..

lionx commented Oct 24, 2014

Hi Daniel..

Can it also work for shared hosting like Hawkhost? I don't know exactly if it has something like PECL OAuth or SSH Access.. THanks in advance..

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Oct 28, 2014

@lionx - I'm not sure. Ask them if they can install PECL OAuth or use a modified version of this script that uses a different OAuth library.

Owner

Daniel15 commented Oct 28, 2014

@lionx - I'm not sure. Ask them if they can install PECL OAuth or use a modified version of this script that uses a different OAuth library.

@sarawutbody

This comment has been minimized.

Show comment
Hide comment
@sarawutbody

sarawutbody Jan 30, 2015

Hi Daniel.. @Daniel15

!/usr/bin/php

Fatal error: Class 'OAuth' not found in C:\xampp\htdocs\Twitter-Keyword-AutoReply-master\TwitterAutoReply.php on line 24

Hi Daniel.. @Daniel15

!/usr/bin/php

Fatal error: Class 'OAuth' not found in C:\xampp\htdocs\Twitter-Keyword-AutoReply-master\TwitterAutoReply.php on line 24

@hsanb

This comment has been minimized.

Show comment
Hide comment
@hsanb

hsanb Mar 6, 2015

how can i set up please !

hsanb commented Mar 6, 2015

how can i set up please !

@TheSecretCandy

This comment has been minimized.

Show comment
Hide comment
@TheSecretCandy

TheSecretCandy Apr 10, 2015

How do i use this? what do i need to do this 24/7 even if my pc is off or is it for a webhost? does someone have a youtube tutorial? i want this very much!

How do i use this? what do i need to do this 24/7 even if my pc is off or is it for a webhost? does someone have a youtube tutorial? i want this very much!

@KristianQ

This comment has been minimized.

Show comment
Hide comment
@KristianQ

KristianQ Jul 21, 2015

Does not seem to run.

Does not seem to run.

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Aug 12, 2015

@Vanqaqi What errors are you encountering?

@TheSecretCandy You need a server you can upload the script onto. A webhost is fine if they have SSH access.

Owner

Daniel15 commented Aug 12, 2015

@Vanqaqi What errors are you encountering?

@TheSecretCandy You need a server you can upload the script onto. A webhost is fine if they have SSH access.

@ceremonials

This comment has been minimized.

Show comment
Hide comment
@ceremonials

ceremonials Aug 14, 2015

hi! is there a way to set this up so it replies with a specific tweet automatically to users I follow, regardless of WHAT they tweet?

hi! is there a way to set this up so it replies with a specific tweet automatically to users I follow, regardless of WHAT they tweet?

@AmgedOsman

This comment has been minimized.

Show comment
Hide comment
@AmgedOsman

AmgedOsman Feb 25, 2016

So here's my contribution
Make sure it's not a retweet
Make sure it's not a reply

                  ` // 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;

            //@Amged Edits
            // Ensure it's not a retweet
            $pos = strpos($tweet->text, 'RT');
            if ( $pos === 0 )
            continue;

            //@Amged Edits
            //Ensure it's not a reply to a conversation
            if ($tweet->in_reply_to_status_id)
            continue;

            //@Amged Edits - Debug
            /*
            echo "<pre>";
            echo print_r($tweet);
            echo "</pre><br /> ---------------------<br />";
            */
            $this->sendReply($tweet, $reply);
            sleep(1);

        }` 

So here's my contribution
Make sure it's not a retweet
Make sure it's not a reply

                  ` // 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;

            //@Amged Edits
            // Ensure it's not a retweet
            $pos = strpos($tweet->text, 'RT');
            if ( $pos === 0 )
            continue;

            //@Amged Edits
            //Ensure it's not a reply to a conversation
            if ($tweet->in_reply_to_status_id)
            continue;

            //@Amged Edits - Debug
            /*
            echo "<pre>";
            echo print_r($tweet);
            echo "</pre><br /> ---------------------<br />";
            */
            $this->sendReply($tweet, $reply);
            sleep(1);

        }` 
@agusrevival

This comment has been minimized.

Show comment
Hide comment
@agusrevival

agusrevival Jan 9, 2017

how to use in laravel ?

how to use in laravel ?

@Silverstonery

This comment has been minimized.

Show comment
Hide comment
@Silverstonery

Silverstonery Apr 29, 2017

@OmgedOsman Good edits. I've made some major edits to your modified version. Allows for keyword filtering. If keyword is present tweet will be ignored.
$badstrings is a string array.
Usage: $twitter->addReply('Search for', 'Reply with', $badstrings)
Filtering isn't necessary, this will work too: $twitter->addReply('Search for', 'Reply with')

<?php
// Twitter autoreply
// By Daniel15 <http://dan.cx/>
// Modified 2013-06-13 to support Twitter API 1.1
//Modified 2017-04-29 to allow for advanced keyword filtering(@Silverstonery)
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';
	
	// Variables
	private $_replies = array();
	private $_oauth;
	private $_screenName;
	private $_badstrings = null; //@Silverstronery Edits: Declare Bad strings variable
	// 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, $bad)
	{
		$this->_replies[$term] = $reply;
		$this->_badstrings = $bad;
	}
	
	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, '"... ';
			$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)
        {

            // Ensure we don't reply to ourself!
            if ($tweet->user->screen_name == $this->_screenName)
                continue;

            //@Amged Edits
            // Ensure it's not a retweet
            $pos = strpos($tweet->text, 'RT');
            if ( $pos === 0 ){
            continue;
            }
            
            //@Silverstonery Edits
            //Filter through tweets
            $good = true;
            if($this->_badstrings != null){
                foreach ($this->_badstrings as $badstring) {
                    //if (strstr($tweet->text, $badstring)) { //Alternate version(Difference in speed)
                    if (strpos($tweet->text, $badstring) !== false) {
                        $good = false; 
                    }
                }
            }
            
            if(!$good){
                continue;
            }
            
            //@Amged Edits - Debug
            /*
            echo "<pre>";
            echo print_r($tweet);
            echo "</pre><br /> ---------------------<br />";
            */
            $this->sendReply($tweet, $reply);
            sleep(8); //@Silverstonery edit. Prevent bot from getting disabled, increases speed because 5 minute bot timouts from twitter take longer to happen than 7 more seconds of waiting

        }
		}
		
		file_put_contents('since_id', $max_id);
		echo '========= ', date('Y-m-d g:i:s A'), " - Finished =========\n";
	}
	
	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);
		}
		catch (OAuthException $ex) 
		{
				echo 'ERROR: ' . $ex->lastResponse;
				die();
		}
	}
	
	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)
		{
			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);
	}
}
?>

Silverstonery commented Apr 29, 2017

@OmgedOsman Good edits. I've made some major edits to your modified version. Allows for keyword filtering. If keyword is present tweet will be ignored.
$badstrings is a string array.
Usage: $twitter->addReply('Search for', 'Reply with', $badstrings)
Filtering isn't necessary, this will work too: $twitter->addReply('Search for', 'Reply with')

<?php
// Twitter autoreply
// By Daniel15 <http://dan.cx/>
// Modified 2013-06-13 to support Twitter API 1.1
//Modified 2017-04-29 to allow for advanced keyword filtering(@Silverstonery)
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';
	
	// Variables
	private $_replies = array();
	private $_oauth;
	private $_screenName;
	private $_badstrings = null; //@Silverstronery Edits: Declare Bad strings variable
	// 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, $bad)
	{
		$this->_replies[$term] = $reply;
		$this->_badstrings = $bad;
	}
	
	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, '"... ';
			$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)
        {

            // Ensure we don't reply to ourself!
            if ($tweet->user->screen_name == $this->_screenName)
                continue;

            //@Amged Edits
            // Ensure it's not a retweet
            $pos = strpos($tweet->text, 'RT');
            if ( $pos === 0 ){
            continue;
            }
            
            //@Silverstonery Edits
            //Filter through tweets
            $good = true;
            if($this->_badstrings != null){
                foreach ($this->_badstrings as $badstring) {
                    //if (strstr($tweet->text, $badstring)) { //Alternate version(Difference in speed)
                    if (strpos($tweet->text, $badstring) !== false) {
                        $good = false; 
                    }
                }
            }
            
            if(!$good){
                continue;
            }
            
            //@Amged Edits - Debug
            /*
            echo "<pre>";
            echo print_r($tweet);
            echo "</pre><br /> ---------------------<br />";
            */
            $this->sendReply($tweet, $reply);
            sleep(8); //@Silverstonery edit. Prevent bot from getting disabled, increases speed because 5 minute bot timouts from twitter take longer to happen than 7 more seconds of waiting

        }
		}
		
		file_put_contents('since_id', $max_id);
		echo '========= ', date('Y-m-d g:i:s A'), " - Finished =========\n";
	}
	
	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);
		}
		catch (OAuthException $ex) 
		{
				echo 'ERROR: ' . $ex->lastResponse;
				die();
		}
	}
	
	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)
		{
			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);
	}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment