Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
I must say this was great to do and a nice little introduction to node.js and npm.
I got to doing this from finding it on GitHub I think I was looking for the Twitter icon in bootstrap whilst working on my Random Quote Generator I stumbled across the twitter-bot-bootstrap (as you do) so after stalling with my progress on the FreeCodeCamp(đ„) zipline for a Wikipedia viewer I decided to give the Twitter bot a try and managed to create it and add it to Heroku đ
This app didnât work as well as expected though đ, so I then decided to take a look at @amanhimselfâs Twitter bot he had created for the #100DaysOfCode challenge, which I just happen to be taking part in.
So with his great documentation here and here I managed to cobble together my own Twitter-bot which I use on my @ScottDevTweets Twitter account
If youâr wondering about the post name check here.
Technologies used
Preamble out of the way, now I can walk through what was used.
Cloud 9
I love this development environment, I soon became a fan of c9 when trying to install Ruby on my windows machine and then again after installing Node.js on my computer for the first time.
Node.js
I already had this installed on my c9 environment so I just had to npm install --save twit then I was up and running
Heroku
I didnât really get what Heroku was until I started this project but once Iâd read the guides it was quite straightforward and the Heroku CLI was already installed on c9 i checked with heroku --version before attempting to install it.
GitHub
You can deploy straight from your c9 environment to Heroku and there are loads of other integration tools you can use which I havenât had time to look into yet. Itâs good practice to have your code on a repository somewhere and GitHub is widely used.
You will need to set up a Twitter application from the Twitter dev portal
All of the information above is in addition to the guides given by Aman the stuff Iâm going to go through now my experience with deploying to Heroku, you may want to host the app somewhere different.
My Approach
Use node bot.js to test locally, in @amanhimselfâs example it will tweet and favourite straight away then go on a timer.
Deploy to Heroku git push heroku master
This is after I have added the changes to Git with a commit message and pushed the changes to my repo, hereâs the terminal commands:
$ git add . $ git commit -m 'my detailed commit message' $ git push origin master $ git push heroku master
The final command will build the app on Heroku for you with output like this:
I know this isnât standard practice for GitHub and I am in the process of understanding how to branch and use Pull Requests so bear with me đ
The first gotcha for me was the Heroku Procfile I couldnât understand why it kept timing out, this was because it was trying to run the default web process instead of the worker process which is whats needed for the bot.
After getting the bare bones set up on part 1 I then got to playing around with the timings and the query strings used by the bot, this is where things started to go a bit off-piste
For quite a while I couldnât understand why I was getting so many errors, with a whole lot of console.log(var) throughout the code I came to the conclusion that that the query string I was using:
var queryString = '#100DaysOfCode, #GitHub, #VSCode';
You can get the logs from Heroku when you app is running by typing:
This will give you your logs back from Heroku so you can add all the debug lines you want in there to determine whats going on.
I was getting the errors pictured so I started to play with the query string and just using one value at a time which didnât cause any errors so I decided to pass one value at a time by adding the Gist below.
This worked, for a while then the same errors started creeping in, I then noticed that the retweet and favoriteTweet were on a timer but using the same random text from the queryString which was about as much use as the previous piece of code as it was a one time initialisation [or initialization] so the same string was being used multiple times causing the errors.
So after some head scratching and playing around with the code, [and tweeting @amanhimself numerous times] Aman suggested that I use the npm package unique-random-array to return a random string value from the string list.
So I added the var in:
var uniqueRandomArray = require('unique-random-array');
Built the query string:
var queryString = uniqueRandomArray([ '100daysofcode', 'freecodecamp', 'github', 'vscode', 'visual studio code', 'nodejs', 'node.js', 'vuejs', 'vue.js', 'inferno_js', 'inferno.js', 'jekyll' ]);
Then just replaced the queryString string with the queryString() method.
Some other things to note that I discovered when playing around with the bot is the retweet and favoriteTweet function params was the result_type: options, hereâs my comments copy pasted from the Twitter dev portal:
// result_type: options, mixed, recent, popular // * mixed : Include both popular and real time results in the response. // * recent : return only the most recent results in the response // * popular : return only the most popular results in the response.
The next stage for this could be to use the unique-random-array package to change that value too.
Thatâs it for now, I still have a weird bug where if I follow someone and then someone else follows me the bot decides to tweet back to itself!
Coming up
More stuff with the .stream() function, like auto follow back and better replies.
Originally published at blog.scottspence.me on January 4, 2017.
Twitter McTwitBot was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.