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