 My name is Soha and me and actually we will be talking about how we can build bots on social media that can help to raise awareness on in the internet. So a bit of background about me, I'm currently a student at NTU and doing my master's by research. I'm doing my master's by research and my research is mostly on deep learning and pneumomorphic hardware. And I'm also course instructor for an edge computing course on Udacity. Hi everyone, I'm Anjali and I'm a graduate student at NUS, studying management of technology. Prior to that I was a software engineer in Semander and today we are going to build bots to raise awareness on the endangered animals on social media. So do you recognize this picture? Well, this is of the two of the only white giraffes on earth. They were slaughtered by tortures on 10th March 2020 in Kenya. And most of the people say that this happened due to the lack of awareness. And how can we as tech enthusiasts, what can we do to contribute towards the conservation of the endangered species? Well, we can create bots and raise the awareness through social media because it has a long reach. So has any of you here ever built a bot? Show of hands please. Okay, no worries. So today we will learn how to create bots on social media. Why are we choosing social media as a platform is because of its reach. So the Reddit users are an average of 1.6 billion users on a monthly average. And 56% of the Reddit users are the young adults between the age of 18 to 29 whom we are targeting. Twitter has more reach because it has 152 million daily active users on an average base. So that is why we are choosing social media as a platform to spread the awareness. We did some statistics and we found out on the internet that most of the commonly mentioned animals on social media are dogs, cats and all of these very few endangered species are mentioned here. Nobody really talks about the endangered species. So surprisingly we did a survey and found out that Alpecas are one of the least talked about animals on the social media, on Reddit. And this made us really inquisitive and further on further research we found out that there are no wild Alpecas left and about 98% of the population has already been wiped out. So we decided to raise the awareness on Alpecas by creating the Alpeca bot. What does this bot do? So this bot basically moves through all the latest comments on Reddit and searches for the string Alpeca. And when it finds the string it just comments on about some random facts about Alpecas. So initially when you start out doing this, there will be some limitations. As Reddit presently has a great limiting time off which is because of the low karma. So for the initial users you will have a low karma and gradually as you gain more comments and the awards the karma rating will grow even further. So initially there will be some issues but after that the number of comments that you can do will eventually and gradually increase. So we built an Alpeca bot on Reddit and a Kuala bot on Twitter. So these are some of the comments that we got. So for Twitter we got one of the government saying that we need to keep on promoting these gorgeous freshest animals as you know that Kuala are getting more extent because of the latest fires in Australia, Australian forest fires. And also Alpeca, for Alpeca bot we got a lot of messages saying that we love the bot and we are so happy to have it. Even one of the users even proposed to the Alpeca bot to hand. So are you guys interested to build your own bot now? So each social media has their own API which we can use to interact with the network. So Reddit's official API is prompt and we can use it, we can use Python to interact with, to write the code and interact with the Reddit. So initially there will be some authentication needed which will include the client ID and the client string. Sohan will explain on much more detail about it in a while. So yeah, similarly for Twitter we have Pipe. So Pipe is not an official API of Twitter but it's a wrapper to be official one and it's much more easier to use. So we will be using that as well. Now Sohan will give us a demo on how can we build our own bot. Thank you actually. So if you can access this link, it will have a few overlap notebooks that will have the code for all the bots that we will be building. So it's tinyurl.com slash pos dash asia dash bot. So everyone get ready. So once you open the link you will find a few overlap notebooks. The first one we will be looking at is this notebook which is Reddit body full. So first what we will do is we will show you a overview of the whole bot and once we get an idea of the whole overview then we can start building the bot. So the first thing we need to do is install cloud. So you can do that by using this link. Desclamatory mark here just shows that it will run on the shell. And then since we are using Python so we have to do pip install. So I hope everyone is familiar with Jupyter notebooks. You don't have to write the code now. I will show you the code later on. This is just like an overview. After that we have to import some of the packages we need. So we will import more and time and time. So what kind of works like this? It first checks all of the comments and every time it finds a comment with alpaca it will fetch a fact from a fact file. And then it will reply to that comment with the fact. So this is the function that gets the fact. So it chooses a random fact from a list of file which contains a unique fact in each line. After that it just fills in the fact in the text which also contains info of the bot and the code. There is also a link where you can read that. And so once all of that is done it will return the text. The next function we have to create is a get comments function which will get all the comments. So how this works is we first need to call a top level API from Reddit which returns all the supplements. After that we need to stream comments from all the supplements which we do in this line. So in this folder what's happening is every time this folder finds a list it will fetch a new comment from Reddit. So we also need to check whether the comment author is the bot itself because we know how to keep replying to the same bot. So that's why we check if the comment author is alpaca bot. And if it is then we just continue and continue on to the next iteration. If it's not then we check whether the string alpaca is present in the comment body. And if it is then it just calls another function that replies to the comment. So this is the reply function and basically what it does is it takes the comment and calls the reply function. And so it replies with the fact that we get from the get plan function. And finally over here we have to first initialize an object of Reddit. We do that by specifying our own client and client secret and also the username of the bot and the password. So I'll show you later on how you can actually access all of these. And yeah so we do all of this in a value because we want the bot to keep them authentic. So yeah now that you have an idea of the code overview let's actually create our own bot. So for that you'll have to open the reddit.net. And what this notebook basically contains is it contains some of the skeleton code. But it also contains some to-dos that you will have to fill in. So yeah so the first thing that we have to do is install browser. This is the text. I think you want me to zoom in. Oh yeah sure. So this is the link. So yeah to create a new code cell just click the plus code button here. If you need a code cell if not you can just directly run this way by either clicking the play button here or pressing shift and then. So clicking the play button here will start to install any issues. Yeah any issues in this step. Sure. Once you go to the you are you just open the reddit.net what it says I think. All right. So the Jupyter so the file is read only. So all you have to do is kind of copy it into your own drive and then start editing. So yeah just so running this first cell with install the data in the package. Right so the next step is to import. And if you need any other packages you can import it later on. So the next step will be to actually get facts. So how we will do that is. Is there a new span for this because it's behind the time. Can I keep it like this? Yeah. I guess you should be. When you talk to you otherwise you want to go into the video because they make a problem. You want to try something else. And you don't need to get the audio. Hello business. I think it should be fine. Yeah. Okay. So the first thing we need to do is actually get a pack from the time. So if you go back to the. So if you go back to the. The main folder you'll see a file called alpaca packs. If you want to build a bot for alpaca as you can use this same file. Or even if you want to build a bot for another animal of your own that. You can just create a file with your own one. So how you can. Upload a fact is. Just download the file. Save it. And then click upload from here. So the first thing we need to do is actually open the file so we can open the file by. And call it by the pilot. Again, like you don't have to use the same file. You can create a new pile of your own with your own pants. So the next thing you want to do is actually create a list of all the. Sentences of all the lines in the fight. So to do that, you know, just write list. And then. Grab the open part of the list. Awesome. Finally, the next thing we have to do is select a random item from that list. So to do that, we'll use the random package and random dot choice. Followed by the list will actually give us that. So we're using a package random here. So you have to import it and be able to. So now that we have a tax, we can. We can create a text that it will reply. So I'm using abstinence here and what abstinence actually does is. If you if you use curly braces with a variable inside, then it will replace that that with the actual variable battle. So in this case, what it will do is it will just return a fact, but that's not informative. So we can give some more information something like. Here is. Awesome. And yeah, so once you've done that, and again, you can make this as informative and as customized. But basically what this function will do is it will return a new fact every time you call it. Our next function that we have to finish is a function that will get comments from that. So the first thing we have to do is actually get an object from type subreddit. So to do that, we'll have to use the Reddit object, which we will create later on that subreddit. So Reddit subreddit will actually return an instance of subreddit. And inside the inside this, you can actually specify which subreddits you want to target. So for instance, if you want to target only a few specific subreddits, like say ask Reddit, then you can specify it like this. Or if you want to target all the subreddits, then just replace that text with all and it will target all the subreddits. The problem with targeting all the subreddits is that the volume of comments is so high in Reddit that it might miss a few comments. But I think that's fine because a lot of comments won't actually have that in the end. The next thing we have to do is stream comments. So for each subreddit, we have to stream the comments. And we also want to skip any existing comments that have already been commented. So yeah, just to end of repeat. So from here we'll get an instance of all the subreddits. With that we can then stream the comments. And what this does is it creates a generator. So every time you call it, it will return a new comment. And so what do we want to stream? We want to stream the comments. We also want to skip any existing comments. So we only want the latest comments. So that is what this line here does. So we'll create this later. That's the Reddit class and we'll create it later. So yeah, the next thing we want to do is actually check the comment author. And here you can give the name of an edit account that you'll be using. And actually before we actually move ahead, I think we should actually create an edit account. So if you have an edit account, then you can use that. Or if you don't and you want to create a new Reddit account, then this is how you do it. So yeah, go to reddit.com and then click sign up. And yeah, just specify an email ID and click next. So yeah, this is now where you can choose a username. So I just call it BossAsiaBot. Select the password. Check this. Sign up. Sign up. So yeah, great. So we created our own Reddit account. So yeah, we don't want to reply to our own comments. So over here just fill in the name of the Reddit account that you created. So yeah. I think we're using all the same ID. It's telling me that I'm trying it out too much. And I have to wait. Because we're all going out for the same ID. That means my ID is still there. Yeah, that's fine. It's also for a partner. So do you want me to wait till you guys wait? Yeah. And I think I can wait for like a minute or two. So. It's funny. Or what? Yeah. I will be. Awesome. So does everyone have an account created? Okay, great. So yeah, you don't want to keep replying to your own comments. So if the comment author is, you know, ourselves, then just continue. And if it is not, then. So yeah, you're using it. So if the author is not ourselves, then we want to actually check whether the animal string is present in the comment. So since I'm building a bot for alpacas, I'll just use alpaca in. So we want to check if it is present in the comment body. And also there'll be issues with capital cases and all of that. So let's just lower this comment. So what this will do is it'll check whether the word alpaca is present in the comment body. This is where I want to also mention something else, which is if you don't put spaces in here, then what that will do is it'll just search for the string alpaca in the whole comment body. And if there's any word that uses that has a substring alpaca in it, then it'll also catch that. And I've had a lot of funny experiences with this. So there's actually a trading platform, I think, that has the word alpaca in it. And there's this whole subreddit where they use this algorithm. And one of those subreddits is actually the cases where my bot commits a lot until they banned it. So yeah, that happens. There are also a few user names with alpaca in it. And so one of these guys used to get a lot of these comments until he finally, I guess, deleted his reddit account. A lot, I don't know. So yeah, but you know, it's also funny because you might be using this, but you might be using alpaca in one of your comments and you'll have a random bot reply to it, which a lot of people also like. And they also abuse that a lot because they just keep replying alpaca and they expect to get a new fact. But the problem there is, again, because there's so many comments that come in reddit, it doesn't pick up all of the comments. And so people actually use started getting angry about the fact that the bot was not replying to their comments. And there was nothing really I could do to fix that just because I can't really go faster than how the reddit API responds. So basically this is what I did and this is how you can turn a bug into a feature basically. So if you look at the reply text that you get when you call the bot, it actually has like the last name is something like this. So if you don't, you know, you get a fact, if you think only alpaca word is, you get it. And the funny thing is, you know, after putting this line there, people stopped complaining because they thought that, you know, there was like something, I don't know. Yeah, there was an algorithm behind that checks whether the bot replies to your comment or not. But actually then there isn't. It's just a reddit API. So that's how you turn a bug into a feature. Yeah, so basically if there is alpaca in the comment body, then we want to reply. So there's a function down here that's reply alpaca. So yeah, we'll just call that and we have to pass the comment. Awesome. Yeah, that's it. So yeah, we are done with this function. So any problems are down still here. The next part is where we have to reply to the comment. So again, like reddit API is actually very simple. So all you have to do is comment dot reply. And then we want to reply with the fact that we get from the compact function. And if you want to like print out whether you know it actually replied or not. So you can like print something like reddit to comment with reddit comment dot ID. Awesome. So now this function is also done. Finally, we have to just fill in our client ID and client secret here. So how you can get that is going to this site here. So if you come here, you can see a button called are you a developer, create an app. So yeah, just click this link here. And so this is the developer page where you can register an app. So yeah, click that button and give it a name. And this name can be anything. We are running a script. So you'll have to select script here and give it a, you know, meaningful description. So in the about URL, you can, you can just give any URL. So, you know, either give your LinkedIn URL or something. I'll just give my site. Yes. Fill this in with your own site or something. You can use your LinkedIn link here also. So once you're done with that, just click create app. So once you've done that, you'll get a page like this. So this random string here, this is your client ID. So copy that and put it here. So I'll just go back. This string here is going to be your client ID. The client secret is this string. You can place that here. User agent is basically a small string that you can specify to help let its engineering team or development team to help debug. So just, you know, just give a small description of your model. In the username, specify the username that you selected or that you created. So if you forgot your username, you can find it here. Sorry. Oh. In one of the new students, you dropped an M in college. The one before. We applied to college before. Yeah. So college was before. I'm using an M. Yeah. Oh, yeah. And yeah, finally in the password tab, just provide your password. And yeah, so once you're done with that, just run all the cells by clicking restart and run all in one thing. So yeah, so your bot is now running. I'll just see if I can actually trigger it. So the best way to trigger it is to go into if you're using the reddit app, just go into popular and click new posts. And just, you know, randomly just select a random post and comment something on it. So you might want to do it for a few different posts because, like I said, you know, oftentimes it misses some of the comments because of the huge volume of comments. Yeah, so you can see, right? It actually replied to a comment. And let me see if I can actually find that comment. Yeah. So that's what it looks like. How did you trigger that? Oh, yeah. So to trigger it just if you're using the website and again, you can use the company used to create the bot. But yeah, just go to all, sort by new and select a random, you know, comment. Select a random post, click comment and just comment. Or whatever. What bot did you create? Which animal did you create? You did it. My question is, why should I be triggered by your comment, right? Yeah. So that's one of the issues which is that since the volume of comments is huge. So yeah, it misses some of the comments. Yeah. That's why you have to kind of, also if you have created a new account then like Anjali mentioned, there's a rate limit where you can only comment once every 10 minutes. You get a message when you're posting it. Yeah. Okay. Yeah. Let me see if I can, you know, trigger your comment. They're just spamming a random post. The comment has to be with you or with anyone. No, it can be anywhere. Or anywhere in the hot running, right? Anywhere in the hot running. But eventually what will happen is they'll start banning you from your subreddits because a lot of subreddits don't allow comments. And I'll show that in the next slide. It bans you whenever you do a comment, right? So it bans you whenever, like whenever the mods find out that it's a bot. Like it's a comment bot. If it doesn't commit and if the watch is silently like, you know, reads post or something, then the mods will do. It's not really banning you. It's like whoever administers. Yeah. Whoever administers that subreddit will stop you from connecting. So yeah, did anyone get a reply to any of the comments? No. Oh wait. I can't comment as the same user. If I comment with the same user, I cannot do it, right? Yeah. I have to do it with another account. With another account, yeah. So what about, like, did you create the bot with another animal or different? Yeah. Okay. And what's the trigger word? The street report. Does that have any spaces? No. And no capital? Yes. Okay, so do you want me to move on or do you want to like test out the problem? No. I'm just gonna move on to the next one. Okay. There are so many options. I have to go right here. There are many options. Maybe there's another option for it. Yeah. So, that was the reddit bottom, the next bot that I wanted to show you was bot or Twitter and yeah, I'll just show you like the completed bot file and you can go back and try building a bot on your own. So yeah, so we'll input tp and before this step you want to actually install it, so you can do that by doing install and yeah, the next step is that you have to actually authorize your bot. So to authorize your bot you'll have to go into your Twitter developer account. So yeah, just go to Twitter developers and go to your dashboard, all right, yeah. So yeah, just under your user account you'll find this apps option over there you can create a new app. So give your app a name and all of this stuff and after that you will get your client secrets and your client IDs. So once you have that you will have to create a function that you get back and this function is again similar to the last function that we use but here you have to keep in mind that Twitter has a character limit of I think 280 characters. So whatever fact and text you have it has to always be less than 280, otherwise it just won't come. The next thing you have to do is create a class and this class has to be an instance of tp.streamlistener and it needs to have three functions. Actually it needs to have just the on status functions but it's good to have these two as well. But the on status function will do is, so status will give you an instance of the tweet that it receives and you just have to check a few stuff like if it's a retweet, if it is you don't want to kind of keep tweeting over a retweet and some other stuff. So yeah you also don't want to reply to like you don't want to reply to a reply to a tweet. So to check all of that so you just have to check whether all of these things are and finally, so api is something that we created over here so you have to use that api and update status will basically like beat up something and yeah you can specify the status with the screen name and the path and so on error and on timeout just checks whether like you know the api encompass some error or there's some timeout in the api and if it is we just want to return true because we wanted to keep it adding more people stuff and yeah finally to start this stream you have to like run these just this is kind of like a boiler bit pool and yeah so how do you actually track animals so to do that you have to use this dot filter function and here you can like track for the streams so just give a list of all the strings that you want to track so yeah so once you know you have that then and you run it then your bot will be in the track. One thing about Twitter though is that it actually it's really good at identifying bots unlike Reddit and so I've had you know like lots of instances where it kind of either doesn't show the beat because it's a bot or it just kind of straight out just blocks the bottom and one way to kind of fix that is so the bot checks for repeating strings so basically I have two strings here that I'm generating so one is an intro stream which is like they can just say something like hello I'm koala bot or something like that and that's also randomly selected from a list of a bunch of introductions and the fact is also randomly selected so that just ensures that it's not tweeting the same thing again and again and another thing that Twitter also does is if your account is tweeting a lot then it will also block it so it's good to kind of limit how often you're tweeting but again that depends on the animal you're tracking so koalas are like kind of tweeted about a lot and like in 10 minutes it gets like 40 tweets and replies to 40 tweets and then Twitter just blocks it so one thing you could do here is just generate a random number and every time that number is saying you know less than 30 then you tweet so that means it will be 30% of the time or you know like you can play around with that number and see what works so yeah so that's the whole Twitter bot and if you go back to here you'll actually find there's a Twitter bot the exercise I like to do and there you can try building a one for your own yeah so one last thing that I wanted to show was like so this is like a survey I did on net it is this visible or do you want me to do something you can show me so we start this thing Basically, this was like a daily survey that I did. The blue line shows the number of daily comments that I commented on, and the red line shows the number of daily uploads that I used to get. And again, this is just for the first 30 days right now. It's much larger. So yeah, so you can see like just after the first day, people started figuring out that it's a bot and they started banning it. So on the second day I got banned from like four subreddits, and the next day there was like three subreddits banned, and yeah. But yeah, there's also some really nice stuff. So like, I got a silver over here on, I think the first 10 days, and yeah, later on I got gold awards and a bunch of other awards also. Someone actually also donated, so they donated $4, which was also pretty cool that helped me keep the water running. And yeah, if you're wondering where you can keep the water running on Google Cloud Compute, so it's like GCP. It has like this VM called F1 micro, and that's free. It's just one code, but it's good enough to run like two, three months at the same time. So yeah, so you can just use that to keep your water running for free. And yeah, this is also taken on by subreddits. And it involved with, I think it's a game, a gaming subreddit, and not surely why it has a huge number of commits, but it does. There's also late-stage capitalism, and this is where there was that analysis tool called Alpaca something, and people kept referring to it, and so that's why this has a huge number of comments also. Yeah, and another thing you can also kind of infer from this is even though the number of comments here is quite, sorry, even though the number of comments that I used to give in Rimworld is kind of like the fourth most, but it has the most outputs, and I think that just shows that Rimworld is kind of like a daily open and accepting community. And that's kind of nice. So yeah, those are basically some of the things that I kind of learned from running bots, yeah. And so yeah, I'll give this back to you. Thank you so much for the detailed workshop. So now let's do our part of helping endangered species. So like Alpaca, there are many other endangered animals while animals across the world. So you can also set up a PayPal account or have a domain button. So as Soha mentioned, we got some donations through it. So even you can do that, and we can donate it to the World Wide Fund for Nature, which can help us to conserve more of the endangered species. What are you waiting for? Let's start our weekend by building bots. These are our details if you contact us anytime. So yeah, so yeah, so this is where you can contact us and this QR code, it has like a link where you can get feedback, yeah. Feedback is really appreciated. It helps us improve, you know, how we present and how we conduct workshops. And yeah, it's anonymous, so, you know, you need whatever you say, we won't know who's saying it. And finally, if you want link links to all the slides and stuff, you can get it at the end of the feedback. Awesome, thank you. Thank you for having me, thank you. Any questions? Have you used this for any other bots so far? Yeah, so right now there's like two bots I'm running. One is Alpacabot on Reddit and another one is KoalaBot on Twitter. I also have, I just like creating bots, so I have a bunch of other bots. I have bots on Facebook that just, so for instance, I had this friend who used to put a really cringy profile pics and it used to just go and come in nice pic near every time he used to put up, you know, change his profile pic. So I had a bot that didn't just that until he kind of, you know, just, he actually deleted his Facebook account because of that. But yeah, so I had a bot for that. I also had another bot, so I don't know if you know this guy called DJ Khalid, and he just says another one, he has this kind of catchphrase. So I had another bot on Twitter and whenever my, I used to work before this, whenever my boss used to tweet something, it just used to come into another one, like whenever he tweeted something. These are just some spammy, fun bots that you can build, but yeah, I mean, using this you can actually build more bots and like build bots that have fun. Sorry, that are also fun, but also help animals and do something, any other questions? Yeah. How, what is the process, like once you get this Google Cloud thing, you want to deploy that into the Google Cloud, you set up the instance, and then. Yeah, so you can't actually deploy it on Google Cloud because Google Cloud instances are only present for like 12 hours, so after that the bot will like stop running. So what you can do is go into Google Cloud, Google Cloud Platform and create a VM. And they have this preset VM called F1 Micro, which is free, so it's like a very bare minimum of instance with just one CPU and I think 10 GB of memory, but that's like more than enough to run bots. Basically the bottleneck in running bots is not how much compute you have, because we're not doing any computing tests in the past. But it's how fast your internet is, because the faster your internet is, the faster it can get on the movie comments. So, and then GCP has pretty fast internet, so yeah, just deploy it on GCP and keep it running. Yeah, for the deployment, do you copy everything into a Python file and then upload it? Yeah, so you can just directly copy everything to a Python file and just copy all the text in all the cells and create a Python file and just do a running thing. So you'll have to keep it persistent and to keep it persistent, you can use something like Tmux or you can use, I don't know, Docker or something. Yeah, you keep it around. Otherwise, like if you don't use that, then as soon as you exit out of the shell, whatever you're running, you'll stop there. And how much is the cost of the instance? Oh, it's free. Oh. Yeah, so that F1 Micro is free. Okay. So when you actually go to create it, it will say like, you know, it's worth, like you have to pay $4, but actually if you're like, there's like this fine print that says it's actually free for 744 hours, which is the total number of hours in a month. So yeah, it's kind of free. Is it? How many, how many... You can just create one of those. Oh, one of those for free? Yeah. I'm not sure. I haven't really had the need to create more instances because just that one instance is enough to run through three boxes. Oh, you ran all the boxes? Yeah, that one instance, yeah. Any other questions? Okay. Thank you. Thank you very much.