Create The Ultimate Twitter Bot With Python In 30 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to this Python tutorial and today you'll learn how to code the ultimate Twitter bot by the end of the video you'll know how to create a bot that auto-replies to tags and comments Auto retweets or likes any tweet Auto replies to use your DMS or even Auto follows a user afterwards all show you how to deploy the bye to an online virtual server so it'll run 24/7 before we begin don't forget to hit the like and subscribe button to keep me pushing content and now let's create this bot okay so first things first you're going to need two Twitter accounts the first account will be the bot that you see here and the second account will be to test the functionality of the first account so once you have two accounts created we can go and get the developer Twitter API so let's open up a new tab here and simply type in developer Twitter API we will click on the link that says developer Twitter comm make sure you are signed in to your bot account up in the top right you can check and we will hit apply they apply for developer account these are all the reasons you would need Twitter developer tools and we will select making a bot now we can go ahead and fill out all this information make sure you have a phone number and email associated with the account then we will just simply select our country and we will type in the name of our ba account so we'll just say Twitter bot it next now here we just need to describe what we are going to do with the bot so we can go ahead and just copy and paste this code I already have here you can write whatever you want it doesn't really matter just say for educational reasons or whatever and we can ahead know here and the features of our app we will need to describe so let's go ahead and just copy and paste some more code in here and here not code sorry tax and we all had no right here and no right here and we will hit next now this is a summary of our account it looks good so we will Hale okay and we will submit our application now you just need to confirm your email and once your email has been confirmed you will probably come to a page that looks just like this applications under review it shouldn't take long at all sometimes it's instant but once it's done we're going to go up to this app's tab here and once we get on the screen we can go up to the top right and select create an app we can can start filling out this information I'm just going to call my app name tutorial bot for this description here we can just go ahead and use the same one that we use in the previous prompt and for the website URL go to your bot profile URL give that a copy and paste it into here we're going to need to copy and paste this description again because we will also use it right here and once that is all good we will hit create and it looks like this app names in you so I'll just add a 33 right here okay so now this is our new app and the next thing that we're interested in is the keys and tokens now this is our API key and the API secret key we're going to need these as well as the other two that are generated right here this is our access token as well as access token secret so these are very sensitive values here and you're going to want to store them in a word file or something like that I'm going to regenerate mine after this video is complete so we are concerned again with API secret key the API key as well as these now we also want to check the permissions and make sure that we have read and write here next we can go ahead and create the folder that all of our project files will be stored in this will include our tweet I files in the twee' pi is the main library that's going to be used to build twitter bots so go ahead and open up file explorer and go to the location that you want to create your app in I'm going to create a new folder here and I will call this tutorial bot 33 now once this folder is created we're all set to go we can go ahead and exit this open up your favorite text editor in my case it is a vs code it's a very nice text editor I highly recommend it you might be using atom or something like that that is absolutely fine but what we need to do here is go to file and select open folder and we're just going to open up the folder that we just created alright so before we start coding we are going to need to check the version of python that we're running as well as install twe PI into the folder that we've created so open up powershell or command prompt and you're just going to navigate to the folder that we have just created once you're at the folder you're going to go ahead and say pip install tweet PI now I already have mine installed but you should see a success message if you don't there's another command that you can run which is PIP 3 install - - user twe PI and that should work as well now we also have to track the version of python that we're running so we can type python - version and we need a python version above 3 so if you don't have three you can just quickly run over to google chrome and search for download Python 3 go click the first link select download follow the prompts from there it is very easy so now back in vs code after we have our tweet I installed that we can go ahead and create a new file and we will call this file bot PI all right so let's import we PI here and we'll get started on creating our first program that's going to update our Twitter status once we have tweet I imported let's go ahead and give that a run and we'll see that we have no errors here so that means we're all good to go let's head over now to the tweet I documentation and this is just going to be over at Doc's tweet pi org and we are going to select o Oh auth one a authentication here we are going to copy and paste some of this code to get our app working and it will be this code right here so we will copy this and paste this back into vs code now we need to assign these variables to our keys that we created in the developer Twitter API so go ahead and copy and paste all four of your keys into vs code here I'm just going to erase these and make these into variables all I did here was essentially just create variables consumer key this is our API key our API key secret this is our access token and our access token secret and make sure those are all copy and pasted properly and now let's start updating our status we can go back to the documentation that we have here and simply copy this code here and paste it back into vs code now I will just change the message here Twitter Bob reporting in live and when we run this code it should update our status it looks like I have a small error here and again basically what we're doing here is just using the Twitter authentication system to create this API object and that's going to allow us to utilize all the functionalities we need which is basically reading and writing data and this update status method here is just one of many and there's other ones like retweet like fetch data and so on so let's run this code and see if we get a tweet and we see that we have no errors here so that's really good and let's go back over to our Twitter bot and see if this tweet was created Twitter bot reporting in live so it's working perfectly now before we write some code that's going to allow us to auto reply auto like auto retweet et cetera we need to tweet at our bot maybe about three tweets and we'll use the hashtag ultimate bot so let's go over and in my Mozilla I have my other Twitter and let's make some tweets okay so we've made our tweets and now let's go and check this out here in our tweets and replies I have tweeted to the BAA account three tweets two of which are using the hashtag ultimate bot so this will be used for our testing you can make the tweet whatever you like as long as we have the hashtag ultimate bot now let's give the BA the ability to get all the mentions in the timeline so we can then reply to them retweet them like them or whatever back in our by documentation we are going to go to Twitter API wrapper right here and we are going to search for the method mentions timeline so this is going to be the method that we will use API mentions timeline and we will go back here and we can write this following code okay so this will return us all the mentions in our BOTS timeline which should be the three tweets that we just tweeted at our bot so let's give this a run alright so here we have the three tweets that we just tweeted this is all the information that is contained in the tweets and let's just take a quick look at these so each tweet has a unique tweet ID and we're going to be using this ID a lot to do a bunch of different things regarding tweets we also have the text of the tweet right here it's code bot this should do and notice it's returning our tweet and reverse chronological order so our most recent tweets are being output first now we also have other information regarding our hashtag right here we can see the text of our hashtag which is ultimate BOTS now there's a bunch of other information here just information about the account that tweeted you can see things like location username and many other things now let's just get all the information contained in the most recent tweet so we know it's in reverse chronological order so we'll just put tweets a zero and we'll run this now this is the most recent tweet here is again our ID the text right here and of course all the other information that we just saw now let's specifically get the text value of the tweet all we need to do is type dot txt and we will run this and down here we have the text of our tweet now we can write some logic that is going to get all the tweets IDs and their text and then we're gonna check if the tweets contain a particular hashtag or not the way that I would go about doing this is simply write a for loop to cycle through all the tweets and we're gonna grab all the IDS and text here we're just cycling through the tweets that we got in our API mentions timeline and for each tweet we are printing its ID as well as its text now we can run this app and afterwards we'll figure out how to get tweets that contain a specific hashtag before we run let's just get rid of this line right here so these are all the tweets contained in the bots timeline now these are previous tweets here we don't need to be concerned with them we're concerned with the last three tweets that we made right here so we're getting the text as well as the ID of the twee over here the next thing that we need to do is add a filter to check if the tweet contains a particular hashtag or not specifically we are going to look for hashtag ultimate bot and how I would go about doing this is create an if statement and the if statement will check if the hashtag is in tweet tags but we also need to consider the case sensitivity so let's put a tweet X in an entirely lower case and the reason for that is because any version of the hashtag as far as upper casing letters can be written and we want to get all of them okay so this is our if statement here and all we're doing is if the hashtag ultimate bot is in our tweet then we're going to print that tweets ID as well as its text now if we were to run this statement it should return us just these two tweets here that contain ultimate bot so let's try that out and give this a run and we see that our two tweets are printed out perfectly they both contain the hashtag ultimate bot so that's really good now the next thing we need to do is get the bot replying to tweet so basically let me actually control-z this we're gonna keep this print statement in here for a second and I'm just going to comment out this if statement alright so I'm going to give this a run so I can explain this now to get the bot or reply to tweets basically the BAA is going to be running on a virtual server and it's going to be running again and again and again each time it's going to create a new array of tweets because there will be new tweets the further that time goes on so we need to separate the old tweets we've already seen and replied to from the new tweets the problem is we need to somehow store the ID of the last we-we've reply to Sonne next time the program runs it'll stop replying once it sees the ID of the last tweet so say we have a bunch of tweets here the program runs and it runs again but we have two new tweets that contain the hashtag ultimate bot so the bot is going to reply to everything containing ultimate bot but we don't want it to reply to these two tweets here because we've already seen them so the best way to do this is to create a text file and we're going to store the ID of the last tweet that we replied to which would be this one in this case and we'll call this last scene text and we're going to need to read and write to the file so let's create this we will select new file and we will call this a last scene text so this is our new file and now let's create some methods in bot PI that will allow us to read and write to this file and above our for loop we will write this code here okay so here are two methods and this method is doing the reading in this method is doing the writing now inside of the read method we are opening the file and we are specifying our for read then we have our last scene ID and we're reading the value that is inside the file and assigning it to last scene ID then we're closing the file and returning what the last scene ID is now inside of our write method we have the opening of the files when we're specifying w-4 right then we write the last scene ID of the tweet subsequent to that we are closing the file and returning now underneath here I put tweets and the API mentions timeline and that's because we need to pass the ID of the last scene tweet into API mentions timeline and what that will do is essentially only get the tweets after the ID that we passed in to API mentions timeline so for example if we were to put the ID of this tweet right here - our mentions timeline then we would only be returning these two tweets likewise if we were to put in the idea of this tweet here we would only be returning this tweet right here so I hope that makes sense and we will also be using the extended modifier which will allow us to get tweets of all character length as well as tweets with links in them so let's write that really quick okay so it appears that we have forgot who put or specify our filename so up here we can just write file name equals last scene text alright so that looks good now and we can go ahead and comment out this if statement or recom n2n and since we're using the extended mode we need to specify tweet dot full text so right here we are just going to go like this all underscore tacks and that will allow us to read the entirety of the tweets and right here we can also say full text dot lower now this is all good but we do need to actually store the tweets in chronological order as we know right now they're in reverse chronological order where the first tweet and tweets is the most recent so we want that to be the last tweet and all we need to do to do that is say reversed now in order to check if this programs working all we need to do is specify the ID of the first tweet that we sent so if we specify the ID of this tweet right here it's only going to return us this tweet right here because it knows we've already seen and replied to this so to get the tweet ID all you do is click on the tweet and it is right up here so we'll copy this go over to our last scene text and just paste it in there alright so now if we run this and everything goes well then we should only be seeing this tweet here okay so we have a small error here no attribute text so that means that right here we didn't specify full text and actually we can take what is inside of here and just paste it right here and now we can get rid of this line and we will be all set to run this okay so exactly as we thought it returned our last tweet that contained ultimate bot and it did not return the tweet that we specified as the last scene so this is working perfectly okay so let's start replying to all the new tweets and luckily this is very easy if you remember all we needed to do to send a tweet was called the update status method and so we can do that again but we're going to need to pass the tweet ID of the true you want to reply to and we're going to need to put the username of the person in there as well so underneath here we will write the following code okay so here we have created our new tweet and we are concatenated the @ sign with a username too in order to reply to them we also have our reply right here as well as attaching the tweet ID now I also wrote a line to store the ID of the last seen tweet so let's give this a run and see if it works now we need to make sure that this tweet ID right here is inside of our dot or our last scene text file and that is because we don't want to get that tweet or any of the predecessors which are these two tweets right here so now if we create a tweet then we should see upon running the program we will retrieve this tweet right here and then we will Auto reply to it from the bot so let's run this now and we see that we've grabbed our tweet right here and with the tweet ID of Oh two eight nine does auto reply work and now let's go and refresh the page and see if it worked and it's working perfectly as we see here and let's also track the last scene text oh two eight nine all right so we're also storing the last scene ID okay so now the next thing that we can do is put this code here into a function that will allow us to reply every 15 seconds when the bot is running so every 15 seconds the reply function will be called this code will run and will be replying that way and we are also going to go up top and import time up here because we are going to use that to reply every 15 seconds now I'm also going to add in the code to auto like an auto retweet so it should be that every 15 seconds we will be Auto replying retweeting and liking any tweet that is tweeted at us with this hashtag so now we have put like I said all the code into this function called reply and then every two seconds we'll be calling the reply function so I also added the create favorite and retweet function to our tweet so let us get a tweet out and see if this works I'm also going to change the reply here and I will say auto reply like and retweet work okay so let's tweet now and see if this works alright we've created our tweet and I'll remember this is the last we we've seen the ideas stored in our file so we should only be retrieving this tweet here so let's run this and we've got our new tweet is everything working good so let us now exit this and we'll go and refresh and see if it works and we see that we have one retweet one like and we have our reply so this means everything is working great on this end and now let's give this another tweet just to be sure alright and we see that now that I've tweeted again we have immediately got a retweet like and reply so this is really good and we are sure that every two seconds we will be updating I'm going to change it to 15 seconds here just for more practical reasons ok and now that we've created our auto reply like and retweet functionality I'm going to show you how to generate further engagement by doing what we just did to all the tweets that contain a specific hashtag not just the ones that are tweeted at our bot so if we go into the trending section here like hashtag The Bachelor I will show you how to reply to any tweet that is like hashtag the Batchelor hashtag bahan any like trending tweet to generate more engagement the first thing we need to do is create a file called search bot pi and we'll just go here and say add file and we'll call this search bot pi now a lot of the code is going to be the same so we can just go ahead and copy and paste this now we can go ahead and get rid of all the code from filename on down and in order to tweet at any of these training hashtags we need to go to our documentation and we're going to look for the cursor method now here we have ended up on our retweet and also I really encourage you guys to just simply go through all the documentation you can learn a bunch by just in doing that so like dot retweet we see retweet tweet da tree tweeters return - up to a hundred user IDs and so all these have nice descriptions so you can basically do anything that you need like dot friends returns a user's friends ordered in which they were added and here we are looking for the cursor method and now I believe it's going to be inside of cursor tutorials and right here we see for status and twe P dot cursor so we are going to use the twe P dot cursor functionality and we are going to get all those tweets so let's write a little bit of code right here first we are going to specify our hashtag as well as our number of tweets that we want to get so let's say we will specify the hashtag hashtag the hunt okay and we are going to get out of these tweets we are just going to get the first ten here so back in our code we will ride the following alright so this is our new code and here we are using the cursor method to get all of the hashtag items with the specified number of tweets inside of our search bot function we are just cycling through all those tweets and retweeting all of them and now we can also remember we can reply and like as well but I'm just showing this for now and we have inside of our try block and then we have our exception and down here we are simply just calling our function so let us now run this code and it looks like we have retweeted or we are slowly retweeting as the time goes on so this should now just go in a retweet our 10 functions here so we can go over here and look at our code bot and we will refresh this here and it looks like I'm starting to retweet all those so this way you can definitely generate engagement and get more followers by liking retweeting and replying to any hashtag that you would like now let us go and see that our bot can be deployed on a virtual server so this can stay running 24/7 alright so go ahead and open up Google Chrome and we are just going to create a new tab and simply type in Python anywhere comm I'll leave a link down in the description of the video now if you don't have an account quickly just go through and create an account and once you are signed in we are going to make sure that we are on the dashboard here we're just interested in our counsels and files and now we're going to create a new file here and we're just going to follow this directory home slash whatever your user name is here and then it's just going to be bot PI and so we can say create or just new file and we can go ahead and copy and paste this code here from our bot PI we'll go ahead give that a copy and just paste it into here now we can save that and we'll go back now and we are going to create our last scene tags so let's open another file and give the same directory and unless underscore seen dot X and then we can go ahead and copy and paste this code which is simply just our last scene ID and we can hit save and now we'll go back and for our councils we're going to have to get our tweet by installation so let's go to bash so once we write our terminal here we can LS to make sure we're in the right directory and we should see I in the last scene dot X now that we're here let's make sure that we are on Python 3 so we'll type Python 3 version and we have Python 3.8 so that looks really good now the next thing to do is to install tweet pi so let's try tip 3 install - - user tweet by now if you tried to do this without the - user I don't think it will work so we're gonna go with - - user and it looks like we already have it so that's all good to go now let's just type Python bot pie oops type that wrong and now our program should be running so let's go and actually tweet at our bot just make sure it is working and here we can see that is working perfectly we've got our one retweet one like and our auto reply so that is how you can run the bot on a virtual server 24/7 and keep on automating any tests that you would like and further you can just go on and check all the documentation with a Twitter API you can just do a little research I don't want to completely create the entire bot for you so I'm going to leave the next steps up to you guys which include you know auto following or anything like that so I will end the video here thank you guys very much for watching don't forget to hit like and subscribe and we will see you in the next video you
Info
Channel: Certified Recaps
Views: 44,186
Rating: 4.921936 out of 5
Keywords: twitter bot python, python tutorial, python scripts, python automation, automate, automate with python, twitter bot, twitter bots, python twitter, create a twitter bot with python, bot for followers, twitter followers, python bot, create a twitter bot, twitter, python
Id: ewq-91-e2fw
Channel Id: undefined
Length: 29min 52sec (1792 seconds)
Published: Wed Mar 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.