15.7: A Bot That Replies - Twitter Bot Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video I want to look at how you can make a Twitter bot that replies to other tweets so in all of the previous bot tutorials that I've made so far the tweets happen on a schedule every 5 minutes every 10 minutes once an hour but something that you can do is actually have a bot participate in the conversation it can look for a hashtag it can look for at mentions of its own account it can tweet with so many favorites its tweet you know whatever you can imagine is possible so let's look at one example of that which is a bot that replies to tweets that are sent to that particular bot so what I'm gonna do first is let's just review for a second the Pete what's the sort of core pieces of the Twitter API itself so there is a get request there is a post request and there is a stream request request this may be the wrong word these are the sort of three commands or method calls or endpoints I think one of these terms that you can execute on the API get is something that you might use for like a one-time search so just give me all the tweets with the hashtag rainbow post is the thing to actually like post a tweet to tweet tweet tweet tweet so that's the thing that you tweet how many times can I say tweet in one video count them something like that now this is a sort of new feature that I want to spend some time looking at in this particular video now we saw it before in an example where I used a user stream to see if somebody followed me but so there are a stream is like a continuous connection to Twitter so anytime a certain kind of event happens I want to execute a function in my code so any time somebody posts a tweet with a certain hashtag or any time so an @ mentions me in the tweeter anytime somebody favorites my tweets I want to connect to a stream there are three kinds of streams user public and I think another one's called like site stream I have no idea what the site stream is somebody who understands that write that in the comments but the public stream is where you want to look have an event tied to just the public timeline of Twitter so look for any time so many tweets with a certain hashtag for example you do that with the public stream the user stream or any events that are tied to a particular user this case that user being me so this is what I want to use if somebody acts mentions me in a tweet and there's a difference between an @ reply in an @ mention which is sort of a subtle thing but kind of important so if I come back over here what I have now is from one of my previous videos I made a follow bot so this particular Twitter bot connects to a user stream and it sets up a callback called followed for a follow event so this function right here will execute any time somebody follows me but I'm going to change this I want to change it's still a user stream but I want the event to be a tweet event and I don't know it could call I don't know what to call this followed made sense but I'll just call it like tweet event so now I now anytime there is a tweet event I want this function to trigger now the tricky thing here is a tweet event for a particular user encompasses a ton of things so what I need to do is figure out was this actually a tweet that was sent in reply to me so how do I do that now one thing I could do is I could go find the Twitter API website look in the developer documentation because I need to look at all the metadata in this event message variable like that's all the stuff associated with the tweet who sent it what time did they sent it what was the text of their tweet were there any hashtags in the tweet all that kind of stuff so I need to look at the documentation for that the documentation is kind of very stressful it's very hard to look at I post a link to it but here's a little trick that I sometimes do and I'm just gonna pull this code from a previous example not a previous example another example that I have preset I have three lines of code right here that I'm copying over in and look at them let's look at them and examine them so I'm gonna also take this out and put this in and put this in this is sort of extraneous code sorry so what this is doing is this is using a node module called FF file system it allows you to read and write book files from your local hard drive and I'm also taking the what i'm doing here is i'm taking this event message and i'm using json stringify which is a way of taking a javascript object and turning it into a string because what I ultimately want to do is write that out to a file tweet JSON so this way I'm not doing them this is nothing to do with making with the but running the bot itself this is purely for me to figure out like what's all that stuff because I could just look at I could look at the Twitter documentation or I could say console dot log event message but if I say that all of this nonsense is gonna be in the terminal window it's gonna be impossible to wade through so I find writing it to a file is kind of useful so I'm gonna run this particular bot whoops it's actually hard your project I'm gonna run it you can see it says the reply er bot is starting and then if somebody is watching live you can tweet at a twosie ITP but I'm just gonna go do it over from my account now fortunately I kind of already did this earlier so you can see that there is a tweet JSON file this is actually would have just appeared out of thin air when that tweet was sent but so I'm gonna look now in that file and you can see ah there it is now I'm gonna whoop so I'm gonna quickly stop the bot so except although it would be fun to see this live update as other people tweet to me but you can see here now this is all of the metadata associated with that particular tweet you can see the tweet is from me Daniel Schiffman you can see the tweet goes to oh that's my URL you can see that's mine anyway there's all this information about the user that sent that tweet this is a key thing in reply to screen name this was a tweet sent to at a twosie ITP so if I want to only look for tweets that were sent to me then that's what I want to do now by the way this is a difference here is if if the tweet if I if my username is the first thing and the tweet I know it's tricky you're like my username I have two usernames in this situation because I sent it to myself but the bot is a twosie ITP and I should name it coding rainbow bot but and me this like third party person who doesn't exist inside this in this room but is me still to say I know I'm talking about that's the person who's sent to tweet so my to the tweet that Daniel Schiffman I'm talking about ourselves the third person set to me the bot because I have the bot is this that's what I'm looking at the data for it so it was in reply to the bot hoz ITP but if I scroll down here there's something called entities and you can see here is also a list of you our mentions so if that tweet mentioned other people or if the tweet mentioned eight to Z ITP but not is the very first part of the tweet you would see it over here so okay so that was a little bit long-winded but this is the kind of thing now you could you could spend your Sunday afternoon reading this JSON file because there's tons of information in there location all sorts of metadata associated with the tweet but all I really care about is in reply to screen name for the moment so what I want to do is go back to my code and I want I don't need to write that file anymore the file is there and I want to just say first okay like so reply to equals event message dot in reply to screen name so I want to pull that piece of data out and while we're here why don't I also pull the the text of the tweet itself so I'll say that if you was in event message text if you look here right this is the actual text at ages zero and you know what I want I want who it's from user dot screen underscore name so I also want to know who the tweet is from from equals user oh sorry event message dot user screen underscore name so again if reading through JSON files is new to you let me refer you to one of my previous videos about learning the JSON format and how to go through JSON format I'll try to have a link to that I asked me in the comments if I don't but this is the kind of thing you need to do it's detective work about figuring out how to get the data that you want out of a particular JSON file so now all I want to do is say let's say I want to send replies back I only want to send a reply back if this was in fact a tweet that was sent to me and what might I say I might say new tweet equals at whoever this was from thank you for tweeting me coding rainbow so you can see here I'm just creating anytime that somebody sent a tweet to me I'm sending a tweet back to them and then I already have from the previous examples this function that I could call tweet it with some text and it just sends that out so I can now say tweet it the new tweet and I gotta use the same variable name there it is so now let's go back and run this if anybody is watching I'm gonna do this myself but tweaked at a twosie ITP and you know what just let's be a little bit more methodical about this let's do put console dot log reply reply to and then from so we can at least see the tweets who they came who sent to it should be me and who it's from so and you'll notice I'm not putting that inside the if statement so there's gonna be a lot more events than just the events that are particularly pleased to this particular user okay so let me run this it's going so now we have to wait we don't have to wait I'm gonna come over here and tweet it myself again a twosie ITP hello is this working please say it is working and I'm gonna send that tweet and that tweet was sent it worked ah so you see a tweet other people are tweeting and you can see that the tweets are going back so I you know let's look on Twitter just to like see that this is happening so you can see I have you can see here that I'm going into the replies that Benjamin Hobart thank you for doing that I sent to thank you for tweeting at me reply to a particular tweet that Benjamin Hobart sent I don't know why it's not showing up in the conversation I'll go to and you can see you can see here what would you do if I put a swear word in this that's a good question so this opens up I mean that's the perfect question to ask right now so I haven't really done anything other than like essentially like hello world replying bot because I'm just saying thank you for treating me like I'm some sort of like horrible spamming you know marketing robot just to like always say thank you but ultimately what you might want to do is make use of what's in that text variable so if somebody tweets at you perhaps they tweet a few key words you go look those words up in the word Nick API I have a video about the word that KPI and you send them back a poem using those words or you know as an exercise one of my examples which I'll post along with this is I take their tweet and I reverse all the characters and I send it back so that's like the reverse text box basically so I'm you watching this I are a very creative person with lots of ideas I don't have any good ideas right now but you can see how this gives you the framework and what I would like to do in the next video which may be but might might not be told hopefully will be soon as soon as I can get to making it we'll look at what if somebody tweets at me not just text but an image can i download that image process that image and send it back ok so thanks for watching this particular video another addition to the set of Twitter bot tutorials and have a long list of topics that I hope to get to and hope to be able to have a longer session sometimes see where I make more of them I don't know why I'm including this in this video you should have stopped watching it by now and hit stop [Music] you
Info
Channel: The Coding Train
Views: 68,815
Rating: 4.9634576 out of 5
Keywords: Twitterbot, Processing (Programming Language), Node.js (Software), JavaScript (Programming Language), npm, twitter bot, bot, botALLY, tutorial, Education (TV Genre), Tutorial (Media Genre), api, Application Programming Interface (Literature Subject), reply bot
Id: ovOtQxLwSzQ
Channel Id: undefined
Length: 11min 55sec (715 seconds)
Published: Sun Oct 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.