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 key as6d5f4a96s8f4as65df
Consumer secret asdf7a9847tq34523654df6g4
Access token sdf45as68f7re8gha65df4vbas8das6f8as6df87
Access token secret asdf45as8df74a89g5a6s4g-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


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.. ๐Ÿ™‚


$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..

Permanent link to this article: http://www.prasannasp.net/how-to-create-a-twitter-bot/


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


    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

    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?

  9. Daniel

    Brilliant! Took a little tweaking, and building 2 Twitter apps, since the first one didn’t take, but otherwise working beautifully manually and via chron job. I updated the bot to search & tweet a specific phrase when finding keywords, which is also working like a charm. Thanks P!!!

  10. Sean Smyth

    Hey this works but I’m interested in having it constantly searching.

    A cron Job is ok but ideally this would happen in real time.

    I’ve a small team of people who I’d love to run a contant search on their accounts. If any of them tweet with a certain hashtag, I’d love to be able to post their tweet on the company account automatically.

    GroupTweet offer what I’m asking but they’ve limits. I’m basically wanting to build my own GroupTweet both as a coding exercise and I’m cheap and don’t want to pay for it ๐Ÿ˜›

    Is this possible using this method and this technology? or should I be looking in another direction?

    Any advice would be greatly appreciated.

  11. pybe

    Is there away to have does not include in the search?

    my account is looping itself and retweeting its retweets.

    I would like to have search includes “term” but not include “other term”.

    Any pointers on how to achieve this would be helpful.


    1. Prasanna SP

      Try this,

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

      where twitterhandle is your bot’s twitter username.

  12. Sandeep


    Its showing me “Success! Check your twitter bot for retweets!”

    How to know, its retweeting or not. I open my twitter but not showing anything.


    1. takonien


      same problem here. ๐Ÿ™

    2. Prasanna SP

      It is not fully compatible with Twitter API 1.1. I didn’t get enough time to look into this issue. I’ll fix it when I get some free time.

      1. Sandeep

        Dude fix it ASAP.

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.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.