Subtitles section Play video Print subtitles (bell rings) - Alright I'm back again, this is another video, I'm just continuing making this Mastodon Bot. (claps hands) And I got some good tips, first of all I did kind of like a goofy thing here. This .ENV package, it's I guess good practice or convention to put it as the first thing. And I can just call .ENV .config, I don't need to save it. The variable I can do this just in one line of code. So let me clean that up, that's a little bit nicer now. The other thing that I want to do, is... (snaps fingers) I want to make this bot post every so often. And so a quick way that I could do that is with the setInterval function. You know what I should also do? Look at this, let me show you something. So one thing that I often like to do, so if I run this right now, actually I just ran it, you can see this is the response that I get after I post to Mastodon. So I get each 'toot' as an id, and as a timestamp, and as metadata about whether it's a reply to something, that has it's content, you can see it's formatted with HTML. So what I actually would like to do is not just console log all this JSON data to the console, I would like to just pick and choose a few things to console log out as kind of debugging information. And also, something that I often like to do, just to kind of help me with this kind of stuff is use the built in node file system package. So I'm going to require file system, this is not a package I usually install, it just comes with node, just whatever version of node you're using. And I'm actually going to say right here, I'm going to say file system .writeFileSync, which is synchronous file writing. And then I'm going to write this data, actually the path is first. And then the thing that I want to write, which is probably JSON.stringify. I want to stringify the data, and I want it to have two space tabs. And then I need to write this file out, so I could just say data.JSON, and then if I run this again we should see now all of a sudden I have a new JSON file that's appeared in my directory here. And I can look at it, and now if I want to figure out what's the data that I got in the back, I have this as a reference. So I can now... And I can timestamp the name of the file and all sorts of things like that, but I can now go and I can just comment this out. And now I can be more thoughtful about this and I can say console.log success... Id. And then I can put, sorry I need to go back here, and I could say it's data.id created at. So I would go back here and I would say data.id ... Plus ... Data.created. You know what? Actually this is a good one, I really should just do a separate video on this entirely because it's such a wonderful feature of JavaScript now, but you know how I'm always doing this console logging, this text, and then some variable stuff and I'm joining with a plus and concatenation. You can use something called 'template literals', which is a way of embedding expressions inside of a string. And the way to do that is instead of quotes, with back ticks. So if I put a back tick with the start and the beginning, you no longer need this plus. I'm just going to get rid of all this nonsense. I just going to do id... And timestamp... Colon, and so now this is a string and it has basically, I want id, this is what I want to literally see, and then what I want to see is the value of this. And the way that I do that is with ${ } so now anything inside of here is an expression. So I could write 4 + 7 and it would put 11 in the string. And then I could say timestampy, I could make this like this. And now we could run this again one more time, node bot.js, and we could see this is what I get now. Id and timestamp. So this is what I'm going to see, and maybe it would be useful for me to also put the content there, who knows what I want to log in console.log. But I want to be more thoughtful about that, not just spit out massive amounts of JSON data. Okay. (claps hands) Let's get to the good part. Now. What I want to do is, my first example of a bot, is I want to make a random number bot. So I'm going to say, I'm going to use template literals again. I'm going to say, "The meaning of life is..." and then I'm going to use an expression and I'm going to say Math.floor. You know, let me put this in a separate variable. Const num = Math.floor math.random times 100, see if we get 42. And then I'm going to just put that here. And now this. My bot should now post: "The meaning of life is..." this random number. So Math.random gives me a random number, a floating point number between 0 and 1, I multiply that by 100 so I get a number between 0 and 100, and technically the highest number is 99.9999999. And then Math.floor takes off the decimal point, so I now have a random number between 0 and 99. I could add 1 if I want between 1 and 100, whatever, that's not the point. The point is now... And you know what, I really would like to see from the console what it posted, so I am also just going to do this. Console log data., what was it? Now I can go back to my data.JSON. And it's going to be... Where is the content? Content, just data.content. So I'm going to say data.content. We're getting somewhere. So let's run this one more time. Success, the meaning of life is 95. Okay, did we get it? We can go here, we can double check. Somehow it went away from there. the meaning of like is 95, great. Okay, now two more things I want to do, make this bot exciting. I am now going to use setInterval. So the idea here is this is all of my code to post to Mastodon. And again, if this is new to you, if you've just been watching my p5-js videos, this weird syntax, this arrow syntax, is part of EF6 JavaScript, might be unfamiliar to you. I will put a link to a video where I describe what arrow syntax is in this video's description. Okay, so now what I want to do is all this stuff here is basically just a function called 'toot'. (laughs) Alright. Because what I want to do is this function will pick a random number, create the status, and post it. What I want to do is I want to say now setInterval, and I want to do this toot every 5000 milliseconds, which would be every five seconds. Now this is a bit extreme, this is not really appropriate bot etiquette, to have a bot that posts every five seconds. So probably a much more thoughtful way of doing this if you're going to have a bot that posts in an automated way, maybe it's a word of the day, or a haiku of the day, maybe it's just once a day, once an hour. I'm going to run this every five seconds, which is reasonable just for testing. Something interesting about setInterval, is what if I make this every 50 seconds. If I run this right now, I got to wait 50 seconds for it to do it the first time, and that's not really such a great thing. So I am going to actually also just call it once first, and then do it every five seconds. Okay, here we go. We've got meaning of life is- whoa it really picked 99? That is awesome. (bell rings) And it's 48, ooh we're getting close to 42, we can see it's doing this every five seconds. I'm going to quit out of it. And I'm going to go back to here, we can see- there it is, these are my posts that I did. You can see ten seconds ago, ten seconds ago, now. I mean this is really five seconds ago. Alright, so here's the thing. I want to show you, remember how I made this... These parameters, all I'm doing is saying this is the status that I want to post automatically from my node program. Well one of the things that I can actually do is I can go back to the documentation, there are all these other things. So media id's is something I really want to show you in a future video. It's a way I can include an image or other media with the post. But one thing that I could do that's kind of fun is this spoiler text thing. So what spoiler text does is allows me to have sort of two aspects to the post. The meaning of life is... And then the status can just be num. So I'm going to break this up into two parts, and I need a comma here. And I'm just going to show you what this looks like. So I'm using these parameters, different properties of the JavaScript object, that's going to go here into my post call. And I'm going to now run this one more time, and I'm going to go back to my bot, and I'm going to look and you can see, look at this. The meaning of life, but I now have this nice 'show more' button. So spoiler alert, if you click on this, you'll see that its 52, or that it's 23, and again I'm doing this too often, so I'm going to absolutely quit this. And then if I wanted to do- Now if I want to have my bot post once a day every 24 hours, I could just go right back to my code, and I could say set interval should be 24 hours. There's 60 minutes an hour, 60 seconds an hour, and 1000 milliseconds in a second. So now, here we go, we now have a bot, a Mastodon bot. It will post a numeric meaning of life once a day. So in theory, if I just left this running here and never closed my laptop, or never did anything, this would just run forever and once a day post. The truth of the matter is you're going to have to think about well, once you've created your bot where is that bot going to live? I mean, you could have it live on your laptop or computer and it's always plugged in and always connected to the internet, but more than likely you're going to want to host it on some sort of server, or maybe get a raspberry pi, plug it into the wall and always have it always sitting there connected to the internet. I will cover that in future videos, in fact I have covered that for how to deploy a twitter bot, and ultimately it's exactly the same thing, but just now the code is changed and it's working with Mastodon. Okay, so I am going to show you a bunch of other kinds of things you in bots, most notably listen. So the streaming API is a way that I could connect and I could say if anybody mentions me I could reply to them. So I could have a bot that participates in a conversation. One thing you should really think about, and I'll talk about this again at the beginning of the next video, I mentioned bot etiquette. But if you really want to be thoughtful about making a bot that's not suddenly going to spam people, so it's not going to just pick random Mastodon users and start @ mentioning them. Or start picking random posts and start replying to them. You really want your bot to engage when people opt in to engage. So maybe you only want to post messages to people who have chosen to follow the bot, or chosen to mention the bot already in a particular post. So I just kept saying post because I have gotten comfortable. Just when I got comfortable saying tweet, I'm now no longer comfortable with saying toot. But I'll get there eventually. Okay, see you in the next video. (pop music)
A2 post mastodon id data json random 4.4: Mastodon Bot - Posting with setInterval() 4 0 林宜悉 posted on 2020/03/27 More Share Save Report Video vocabulary