loading...

How to build a simple Twitter bot in 17 lines of code

Omar Sinan on January 11, 2018

A few months ago I created this twitter bot that retweets anything from a specified list of users. And guess what? It was only 17 lines of code. Th... [Read Full]
markdown guide
 
 

Excellent article! Twit is a great package, really versatile. A while back I wrote a bot that retweets web dev jobs, forgot about it for a while and just checked in on it and it has more than 1,300 tweets and a little more than 50 followers. It's also just a few lines of code. Wrote about it back then at dev.to/benhayehudi/how-i-created-t....

 

That's awesome! Just checked out your article, I really liked how comprehensive it is.

 
 

Nice article. Worth being aware of Twitter's automation rules, though, which you agreed to follow when you acknowledged the developer agreement.

help.twitter.com/en/rules-and-poli...

Also note that the new Account Activity API provides callbacks (webhooks) rather than requiring a constant connection, which is a more modern way of handling these kinds of scenarios. I'm not certain whether the twit node module supports this yet.

 

So if I want this to be a Serverless app, I'd need to run it periodically to poll twitter for new tweets. Right?

Is there anything in the Twitter API or (any other service) that callbacks my app URL upon new tweets from a list of users?

 

From what I can tell, Twit.stream() keeps an active connection to the Twitter API, so there's no polling required to get information. In this case, a handler is being attached to the tweet event, which should stay active as long as the app is running.

 

Thanks Andre, so yes. It's not callback based. Serverless fail :(

 

Great tutorial!

If you're following along, make sure not to check in your Auth keys or other secrets to Github.

You could use the dotenv package to keep secrets in environment variables.

 
 

Enjoyed reading this from a code point of view. :) Only thing I felt like mentioning: Given the fact that online services offer this for $15, you might miss some costs that go beyond just writing code. Heroku hosting for running this thing all time, maintaineance, debugging, making sure it's up and running - if you really want to use this for anything meaningful, I doubt your solution will be much cheaper than $15 per month. ;)

 

Fun read 😍

Just note that apps.twitter.com has been sunsetted. You can manage any of your existing/new apps in all of the same ways through developer.twitter.com/en/apps

 

Great tutorial. You could add error management

 

Thank you! I’ll look into error management in the next tutorial hopefully :-)

 

Loved the article and especially the title 👍

If the title was How to write a simple twitter bot, I'd have turned away.
But 17 lines of code really caught my attention 😝

 

You just got featured in 7 must reads from last week!!! Congrats on great post! :) Looking forward part to with Heroku

 

Didn’t even realize, thank you! :-)

 

Omar, that's awesome use of twit package. I just would like to add one more thing to your article is that even we can create Lambda function in AWS just for this and it would be pretty simple instead of spinning a Heroku stack.

Definitely its a matter of choice but, I would prefer to go with Lambda function as it does not require any kind of state management.

 

And remember to put those secrets into Environment Variables before you upload code to Github!

 

Hi !

I have proudly done it ! I have launched my own stock exchange news feed (rather than subscribe to all of that accounts on my personal Twitter account). Is it possible to have unlimited Retweet posts ?

While I'm trading, that really helps me, I would be so grateful if we can find a issue to this problem.

Thanks a lot.
Pierre

PS: I'm french, I apologize in advance if there are some mistakes...

 

This is very useful and simple explanation, thanks!

 

I'm glad you found it useful! :-)

 

Hey Omar great job, hoping you can help but I am getting a 401 error. Copied and pasted your code directly to play around with.

 

Thanks for the article. qq: How do I increase the retweet count ?

 
 

Hi, did you write the next post about Heroku ? I would be interested :)

 

In all honestly you could have done it all in one line but readability and all... hah. Nice tut!

 

Very very good very good good.

 

Wow, funny and informative tutorial!

Kinda a dumb question but, what exactly is those "users ids", are their @'s or usernames? If not, how can I get this information?

 

Open up CMD or Terminal and cd to a new directory for your twitter bot and execute the command, how ???which application?

 

Very good and well explained article!

 

how would I go about making it reply to the tweet instead of retweeting it?

Code of Conduct Report abuse