In this video how you can use
the setInterval() function
to schedule tweets from a bot in node
we already have working from
the previous video an example
that makes a status ,post to the twitter
api and a callback to tell us whether
it did'nt work or work
So now we need to do this
what if I want this particular program
to do this every five minutes or
once an hour
how would I add that to the program
other thing I will show you in this video
is how you can have a bot tweet based on
a particular event
So, the example I am using in this vedio
is anytime someone follows me I will tweet
back and say like you know thank you
for following me or something like
that. There are lot of other kinds of
events, eventually at some point I wanna
look if someone tweets mentions me
with an image I'm gonna process that image
send the image back but,
we get to that in future vedios
In this one we are gonna look at
scheduling the tweets and the twitter api
stream to have tweets happen based on
a given event. Okay
So,the good news is
we already have this,this is all
the code that I need
for posting a tweet
tweeting a tweet.Okay
The first thing I'm gonna do
wrap this in a function and
I am gonna call this function
'tweetIt' apparently everything has to
start with 'tw' in this example. So, I'm
gonna all of this code and
I am going to indent this a little bit
I don't have my sublime settings,
this four space indent I can't
I have to tolerate this for the video
probably gonna fix that for the future
two spaces two spaces Iprefer
but you shouldn't be like that
you should welcome any kind of spaces
for your indentation
But the point is I have this now inner
function so anytime I would have
call this function
If I call that function boom its gonna
post that tweet. One thing I have
an issue of like could run this right now
I am gonna say 'node bot2.js' I'm feeling
I might get an error
'Something went wrong'. So, why it send
something went wrong is just run this
example I made the same exact
tweet two times in a row
so twitter doesn't like that
twitter is like that's a duplicate status
I could have got better error message
down here to look at the error what the
error actually is,but I have done this
before so I remember I have done this
before I know that was an error
It doesn't make sense to have twitter bot
to post the same tweet every so often.
There is actually a page twitter's website
I will provide a link to it that gives
you kind of rules that you should follow
bots are allowed but there are certain
kinds of do's and don't 's in terms of
what Twitter allows for your bot but
one thing that I can do really quickly is
I could just say let's make this a
'Random Number Bot'
So I could say in my tweetIt function
I can say 'var r' is a random number
between 0 and 100 and may be I'm gonna
use floor function so it doesn't have
a decimal point. So I'm getting 0 5
integer or whole number
I tweet I could say the status is a
'here is random number' plus r plus
'codingrainbow from node js'
Hopefully that not more than 140 character
I think that's fine. So now atleast
each tweet would be different because
I am getting a random number.
So, let's run this again
and see 'It Worked!' and
now if go to the browser and
look at my account page Refresh
see 'here is random number 30'
forgot to put a space between the number
and the hash symbol. So, my super
important hashtag is gone. So, let me add
that space back in and you can see
there we go, this is a bot a bot that
tweets a random number I mean the point
of me making these videos is that
to show how to do it so it's up to you
how you come up with bunch of creative
or interesting ideas and I try to include
some links to interesting bots examples
and ideas might help you guys started
in this video description but the real
key here for now is this now happening
just once what if I want this happen every
so often the way I can do that
is by using the function in Javascript
called 'setInterval()' and it is a
function that triggers this particular
function 'tweetIt' every how some number
of milliseconds. So what I could say is a
1000 milliseconds would be 1 sec.
1000 times 10 would be 10seconds
1000 times 60 would be 60 seconds
minute times 5 would be 5 minutes
times 60 would be once in hour.
It's up to you how often you want your
bot to run again read that Twitter page
that explains that like if you are posting
tweets like every 10 ms, probably gonna
shutdown your account .I imagine I don't
know all the parameter all of them top
in my head but all purpose is right now
if we get away with like tweeting every
20 seconds just briefly so we can at
least see the response and one thing
I also do is actually when my bot starts
explicitly called tweetIt once because
this first time it's gonna run is only
20 seconds in. So I have to think
of something for 20 seconds while
we are waiting but let's run the bot
and say 'node bot.js' so it worked
it did tweet something probably would
make sense do something better in console
like print out the tweet that I actually
tweeted. I'm rambling
This probably gonna take me 20 seconds
to go back over here refresh this page
and you can see got the number 18
from node.js and now I'm gonna go back
to node and wait here ' It worked!'
and another one that was 20 seconds
come back here and hit refresh
and look I got the number 72 now
I'm gonna quickly go here and
hit 'Ctrl + c' which is gonna stop
this thing from running and I don't wanna
tweet too often from this account
right now but you can see this is it
all of these videos all of this like
this setting up an account working
with node it boils down to actually
really quite easy which is just about
having a single function that posts a
tweet and single function 'setInterval()'
schedules it. Of course the real work
for you is what goes here like why you are
making a bot Is it to be try some playful
experience Is it to do something practical
Is it to make a political statement or to
provoke some thought there are lots of
different kinds of creative reasons why
you might want to make a bot. I think it's
kind of form of expression that is really
I am so tired of making these videos for
hours. I'm excited to see those who you
are may be the first time what kind
of things you might make please share
those if you end up making something
in the comments that would be helpful
and interesting to me. So ,now let's look
at one more aspect of this
If you remember that these are the three
kinds of thing that you can do with
Twitter API you can search the Twitter API
So one of the things you might do is like
search for all the tweets with the given
term and I could make up a tweet from
those tweets and send them back
with post. But, the thing we haven't seen
is this ' stream ' so how do you set up
a stream. So, first of all, there are
couple of things there are different
kind of streams and the stream I'm gonna
demonstrate you is the stream called
' user ' stream the reason why I wanna
setup a user stream is I want to get an
event for certain kind of actions
If someone favourite one of my tweets
if someone follows me, If someone
retweets me, someone ' @ ' replies me
So you can get an event. Let's start with
just the follow event which is one of the
easier ones to work with. Now
I don't have actually code for this
of the top of my head I'm gonna just kind
of grab it from a here for a second
which I prefer to like to type it out but
just gonna do this from my previous
example. So, I'm gonna leave this down
here this particular interval is gonna go
as well I am gonna add something to this
particular bot. So, I'm gonna add these
2 particular lines to the code
Setup a stream a user stream remember
T is a reference to my connection to the
API with twit package