Saturday, 25th Oct 2014

«

»

Print this Post

how to create a twitter bot

In my previous post I’ve given a php script which can be used to create twitter bots. Here I’m explaining how to create twitter bot using that app. No coding skills are required to create bot using the above script. Before going through this article, please make sure that you’ve,

1. a twitter account for your bot, ;-)

2. a web server with latest version of php installed.

This app works on OAuth, so you’ve to get API key for your bot application. To get twitter API keys, go to http://dev.twitter.com and register your app. (Look at the below table for help on filling up required fields)

Name:Name of your twitter bot application. Eg: My bot
Description:Description of your app. Anything Between 10 and 200 characters.
WebSite:Your website address. You’ll get backlink on every tweet your bot tweets!
Application icon:Upload icon for your app. Not mandatory.
Application type:Read and Write
Callback URL:Leave it blank
Organization name:If any.. Or your name
Organization website:If any.. Or your website

Now save your application and get Consumer key and Consumer secret, and create Access token for your account. Then get your Access token and Access token secret.

For example:

Consumer keyas6d5f4a96s8f4as65df
Consumer secretasdf7a9847tq34523654df6g4
Access tokensdf45as68f7re8gha65df4vbas8das6f8as6df87
Access token secretasdf45as8df74a89g5a6s4g-as5df4as-sad56f4a6s

Now download twitter bot php script from here from GitHub repo and upload it to your web server using ftp. Then edit config.php and fill up required fields. i.e CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET

Eg:

1
2
3
4
define('CONSUMER_KEY', 'as6d5f4a96s8f4as65df');
define('CONSUMER_SECRET', 'asdf7a9847tq34523654df6g4');
define('ACCESS_TOKEN', 'sdf45as68f7re8gha65df4vbas8das6f8as6df87');
define('ACCESS_TOKEN_SECRET', 'asdf45as8df74a89g5a6s4g-as5df4as-sad56f4a6s');

Then replace search key word with the word of your bot’s subject. It retweets all the tweets containing that word. Eg: for a Linux bot replace search key word with Linux. rpp means the number of tweets to grab at once. For an ideal bot, this should be less than 20. Otherwise your account would be suspended.. :)

Example:

$search = $twitter->get('search', array('q' => 'linux', 'rpp' => 15));

Now save the file and try to view that file in your browser. i.e http://yourwebsite/twitterbot/config.php. If you see a blank page and your bot starts to tweet, then it indicates thumbs up ;-). Now set Cron Job for config.php file at the interval of 30 minutes.

/usr/bin/php -q /home/your_username/public_html/twitterbot/config.php

Thats all. Your bot should be working now! :-)

Download twitter bot script: GitHub repo

Follow me on twitter @prasannasp

Short Link:

About the author

Prasanna SP

Student | WordPress Dev | Tech Blogger | Interested in GNU/Linux, FOSS, PHP, Drupal, WordPress, Ethical hacking, Photography, Painting, Literature etc..

23 comments

Skip to comment form

  1. pradi

    why is it not working?

    1. Prasanna SP

      You have to set a cron job to get it work.

  2. pradi

    it worked!

    1. Prasanna SP

      Great! What’s your bot handle?

  3. User

    Hello.

    Thanks for the info. However, when I try to use it I get the following errors:

    Warning: require_once(OAuth.php) [function.require-once]: failed to open stream: No such file or directory in /home/*****/public_html/usefulapp/twitteroauth.php on line 10

    Fatal error: require_once() [function.require]: Failed opening required ‘OAuth.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/*****/public_html/usefulapp/twitteroauth.php on line 10

    I renamed twitterbot to usefulapp.

    Any ideas?

    1. Prasanna SP

      Rename usefulapp to twitterbot, and try again.

      1. User

        My original problem was due to misconfiguration of the Twitter app. I corrected it. But your script still does not work (no error messages, retweets just don’t appear, and I renamed the folder). Other scripts work ok with this Twitter app.

        1. Prasanna SP

          What is the Callback URL? If it is @Anywhere application, add your domain at @Anywhere domains in twitter application settings.

  4. lawsuit loans

    I got problem, when I go this link “http://yourwebsite/twitterbot/config.php” after setting my config.. site said no post.. or no file..can you guide me..

    1. Prasanna SP

      Yes, it says nothing. It just searches twitter for the search term and retweets when you open that file in browser.

  5. Mark Rafferty

    How do I set the account it Rts from? Ids it the account I setup the app using or something else? Thanks

    1. Prasanna SP

      Did you really follow my post? There is no GUI for this program to connect to twitter. You need to get Consumer key, Consumer secret, Access token and Access token secret from dev.twitter.com and enter them in the config.php file. Then set the cron job.

  6. Adam

    I was having trouble with this at first, but then I realized I didn’t follow the directions EXACTLY.

    So I then fixed my settings per the directions but still didn’t work.

    What finally worked for me was deleting the twitter dev application and making a new one. I suggest trying this if you have difficulty.

    Also, make sure your bot can tweet manually before you go to the cron step.

    Thanks for this!

  7. noga

    hi,
    thank you for this script it works well with me
    but i have a question i want to re-tweet specific user’s tweet how can do it? like re-tweet every tweets from @naglaa

  8. Francis Smith

    Warning: require_once(twitteroauth.php) [function.require-once]: failed to open stream: No such file or directory in /home/a1228362/public_html/config.php on line 2

    Fatal error: require_once() [function.require]: Failed opening required ‘twitteroauth.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/a1228362/public_html/config.php on line 2

    getting this error. What is the issue?

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code snippet between <code> and </code>. Comment moderation is in use. Please do not submit your comment twice -- it will appear shortly.