Coding Challenge #118.2: Mastodon Fractal Tree Bot Part 2

Coding Challenge #118.2: Mastodon Fractal Tree Bot Part 2

– Hello. So, previously, I made a bot that picked a random
number between zero and 90, generated this fractal
tree using that angle, using processing, made the image, and then posted that image to Mastodon using this coding train
bot at bots in space, bots in dot space, whatever,
botsinspace.instance. So, you can see a bunch of these here. And so, now what I want to do… And the bot, if we look at the code, is just executing all of this stuff to generate the image
and post it once a day. So, what I would like to do now in this video is change it. Well, I’m not going to use setInterval. In fact, I’m not ever
going to post an image. I am only going to post an image as a reply to somebody else. And I’m going to look in their reply, find the angle that they have specified, and send them back a tree with that angle. And you can just imagine the possibilities of things you can do. I mean, one of my favorites,
this is a Twitter bot, but one of my favorite
bots is Lowpoly Bot. And what Lowpoly Bot does is, is it still running, is the question. ‘Cause most of these bots you can’t… One of the reasons why I’m using Mastodon is that you can’t really run
these anymore on Twitter. They’ve changed the API
specifications too much. So, the idea is that,
you send in an image, and an image processes for
you and sends it back, okay. So, I want to connect
to the streaming API, and I want to connect to the user stream, meaning any time I get
any sort of notification, somebody favorites something,
somebody mentions me, re blogs me, if I post something,
I think I get an event, so the way that I do that
is by saying Right, I have to go look
it up here in this API. path and parameters. Returns a stream listener instance. See examples how how to use it. Okay, why not? I’ve done this already
in a previous video. So, I could look at my previous code. But let’s actually… Sorry, let’s just go to
examples, streaming.js. Oh, and we can see that here. So, this is what I want to do,
I want to connect to the stream. Create a stream instance. All right, connecting to streaming/user. And then, on a particular event, like a notification, is that what it was? Oh no, just a message. So the event here is called message. And then, on a message… And this by the way, I’ve just been told from the chat, thank
you to Alca in the chat, that the streaming API
doesn’t support promises. So in this case, I do
have to use a call back and I can say response, I
like to use the word response, like this. Now I do want to look up my previous code ’cause I need to figure out what, I need to determine what I’m
looking for in that response. So I have that open here and you can see that what I’m doing here is if the event is a notification and if it’s a mention,
that’s what I’m looking for. I’m looking for a mention. So I’m going to say if
response.event equals a notification. I’m just only going to do mentions. And
I think equals mention. Is that right? I should be just looking
at my previous code. Message .data.type. Message .event. So where is the mention
in this one that I did? Do do do do do. Ah, message .data, it’s right there. Message .data.type so I’m
using different variable names, which is fine, but if the
event is a notification and the type of the event is a mention, then, now, I am ready
to basically do this. The idea is if I’m mentioned,
go ahead and do this. But, and I don’t need a
separate function for this. So I’m just going to take this here. But, what I want to do is I need to add some stuff to this. So for example, I probably want to mention a user account and I
want to have a reply id and also that angle. So I need to, if I’m going to reply, I want to at mention them,
so I need their account to reference. Also, if I want it to be threaded, I need the id of that post so I can include that,
and then the angle I want. So now the angle shouldn’t come, oh I’ve got to send the
angle in to processing. Alright, that’s going to be
something we have to figure out. Okay, I’m going to get
to that in a second. So let’s try to get the
whole flow of this working. This is an interesting problem that I completely forgot
that I had to figure out. So first, let me get the id is, and I did this before,
so I’m just going to, we can see the id is this. This is pulling the id out of, and I said response instead
of message, which is fine. And then the account name is right here. So I want to also get the account, which is also response. So I can then pass on the account, pass on the id, now the
angle is a tricky one. So what am I expecting the person to say? I guess I’m expecting
somewhere in their post that there’s a number. And they could put multiple numbers, I’m just going to pick the first number. So what I’m going to do, whoops. This is by the way my
inspiration, the tree bot. But where am I? I’m looking at my code. I need to get the content
of what they’ve sent me. This is their message content. So what I want to do is
use a regular expression. So my regular expression is going to be I need to find a number. The number that I want is
between two and three digits. So I guess I’m going to allow… I mean, do I allow an
angle greater than 90? I mean, sure. I could actually, it could be any number. I could really just say any number. It doesn’t matter how big. So I want to match this. And so if I say regular… Oh, if I say content.match
regular expression. Is this– Now I’ve forgotten how
regular expressions work. So let’s just go to the
browser for a second and let’s noodle around here. So let’s say if I make
a regular expression and I make it equal to this, and then I have a string like hello 42 goodbye. If I saw s.match that regular expression, what do I get? Ah, perfect. I get what it matched, the
index, and some more information. All I care about is what it matched. And I don’t need it to be global. I don’t need it to get all the numbers. I could get all the numbers
and average them or something. But I’m just going to get the first one. So this is fine. So then I’m going to… And I do have to deal with the fact, what if it doesn’t match anything? So let’s see what it gives
me if it doesn’t match. So let’s say s equals this. And I’m going to say s
match regular expression. Null, so that’s fine. So now I’m going to say
results equals content match regular expression and the angle, so I’m going to say if results, or I could, if results, I could probably
use that ternary thing. Then the angle equals results index zero, otherwise angle equals, oh otherwise I’m going
to say there’s no angle. I could reply and say you need
to mention me with a number. So I’ll… So I’m actually going to just say, yeah, angle equals negative one. So I’m going to use
negative one as like a… And I can actually just do this and then do this, okay. Great great great great great. So now in theory, now I
can pass that angle here. So alright, now one thing I can do. Let’s just do this. So if, the first thing I want to do is just say if angle equals negative one, right, if I’ve gotten
a negative one angle, I want to actually just be done. Like I want to say… Oh but I have to mention the person. Okay, so I have to say if in a status post, can I find that here? Right, so the text of the status is this text of the status in reply to id, so this is important. So I’m going to say… Please specify an angle
in degrees using digits. ‘Cause it won’t work if
you say the word ninety. Then I need to say in reply, what is it? In reply to id, in reply
to id is that reply id. And then also I want to use the account. So I’m going to, I want
to start with the account. Account, and do I say at? I think I have to say at the account. Please specify an angle in digits. Then in reply to id. Okay, so this is good and I’ll call this, and then I’m going to say response, I’m going to say params and response and success, angle, negative one. Okay, so this now should… If I run this… And then I’m going to put an else here. This function has gotten quite long. But basically what I’m doing is first I’m checking if
there was a legitimate angle. And if there wasn’t,
I’m replying back to say please specify an angle in degrees and then let’s see how that goes. So right now if I run this bot, oh I forgot to make a new one. That’s fine. If I run this bot,
somebody should feel free to at mention me without a digit. Can somebody do that please? Alright, somebody at messaged me, thank you for helping me debug this. And I got an error in bot.js line 25. Bot.js line 25, ah,
response, response, response. Sorry! Response, response, alright
let’s try this again. Be prepared. Alright, at mention me
with no number please. You can put a number in
it, but it won’t work if you put a number in it. Alright, oo! Interesting. Can someone please? There we go, okay. So let’s take a look, so someone mentioned me with a number. That’s great that we had
both of those tested. And we can see this one said please specify an angle
in degrees using digits. That’s right. And then this one just said behold my beautiful tree. So I haven’t done the thing
where I actually get the angle. So let’s first, let’s just first if I really do get an angle, let’s do all the same steps but right now, I’m going to also add at account, so I’m going
to mention that account with angle, and then I’m going to use the angle that they asked for and then, and I need in reply id. What was it? Same thing. In reply to id. Where is that, where
is that, where is that? There we go. In reply to id. Okay. So now, if I run this one more time, you can now at mention me with an angle and you’ll get a tree back. Or you can at mention me with no angle and you will get a message
back saying you need an angle. But, I won’t be using the
angle you asked for just yet. But let’s just see if this works. Okay great, so we can see that we got one two three four five mentions, one of which had a negative one. So let’s see, let’s just check our bot now and we can see 60, 38,
56, now here’s the thing. I’m not actually using the angle, it’s still just a random angle. (laughing) So, what I need to do is… Oh and I forgot that I
was pulling it out of here so it’s actually not, hold on. Let’s… So we see Alca asked for 56
and I made a tree with 52. So now let’s take this out and actually try to use that angle. Now here’s the thing. I can’t just… How do I do this? So what I’m actually going to do, this is interesting, is I need to go back to that exec command. Where did I have that? Where is that? Oh, command. So that’s, whoa, it’s
all the way way up here. So what I actually need to do is I need to add another argument to this. And so what I’m going to do, and I think, I haven’t done this in a while. Let’s, alright, let’s
do it this way, sorry. Let me grab this command. We’re going to figure this out together. And we go back to here. And I’m going to run that command. So this runs and that spits out the angle. Now there should be a way in processing for me to get the arguments. How do you get the arguments in Java? Okay, I looked it up, I can’t believe I forgot this. But processing actually just has a build in variable called args which has those command line arguments. So I’m going to put printArray args and now I’m going to do this. And we should see whoa! Oh, it’s null ’cause there were none. That’s weird. Because where do those go? They’re not here. How do I pass arguments? If I just add something else like here? Oh yeah, there we go, okay. So if I just add, the
args come at the end. I thought some of these might be args, but of course there’s none. So we can see here if I just
execute it with an argument like with the angle 40, then, bleh 40 ’cause I have some… I had a bl in there. Bleh 40 comes out. Okay, perfect, this is
much easier than I thought. All I need to do is now say command. Where do I execute that command? Right here. Execute command plus space angle, right? I guess I could use a
template literal there. But just need to add that angle there and I probably want to
double check to make sure there are any args, but if
args is not equal to null, then… And then I’m going to make
this a global variable. A equals zero. And then I’m going to say
a equals args index zero. I guess that’s going
to be a string, right? So I need to convert that to an integer or a float maybe. And then I don’t need it here. And now here we go. So now, if… Let’s take out the exit just so we see. Let’s do this. Processing java. Run with the angle 10. That looks like the angle 10 right? Now, let’s run with an angle of 90. And that looks like the angle 90! (clap)
Perfect! I passed in the angle. And so now I should be able to… I mean right, is there
anything left to do? I think this works. ‘Cause I got the angle, I got the angle, I’m going to give it the angle, yeah, alright. What the hey ho? Let’s run this. Ha! Node bot.js, Mastodon Bot starting. And now, I’ll just wait for a little bit. (jazzy music) Try test anything you can imagine. Oh woops! Sorry stop, stop! It’s funny. Actually, this is fine
I forgot the exit thing. So amusingly I forgot the exit thing so it’s opening up processing but it’s never finishing. So actually that exit
thing is very important because I forgot that I had that in there. I’ve got to put that back in there. And let’s try this one more time. And here we go. (jazzy music) Alright, thank you everybody, this looks like some
good amount of testing. Let’s go now to bots in space and take a look. Here is, behold my tree
with an angle of 90, please specify an angle,
so we can see here, nope, right that worked. Here, a right triangle has
an angle of 90 degrees. I love right triangles. There we go, perfect. So, 89, and there we go. 128 degrees. 128, this is working! Yay! (trumpet fanfare) This is done! So I hope now you have enjoyed, you can imagine sort of… I mean there is another piece
to this that I could do, which is what if the
person sends me an image and I do something to the
image and send it back. I guess I’ll have to come back another day and do that one. But now you can see the full process that you can have another
user at mention you with some data, text data, use that text data to generate an image, and send it back. So I hope you enjoyed this tutorial, I hope you make some wonderful
bots at bots in space and I will see you in a future video. (ding) (playful music) (ding)

18 thoughts to “Coding Challenge #118.2: Mastodon Fractal Tree Bot Part 2”

  1. I don't watch your videos anymore because I don't get enough time to learn something …. But I still pressed the notification and pressed like without even watching the video bcz I know all your videos are best….

  2. There is a website called, that has a game and people can conduct competitions. We have to program algorithms to play the game,in python, please make a video on how to make an algorithm.

  3. Hi Dan! Could you do a coding challenge or video that implements a main menu? Or if you have one could someone point me to it? You have a bunch of material and I'm just making my way through it. Love the videos!!

  4. Hey! I two weeks ago I found your video about fractal tree and I like it! I made my own random tree generator by using C++ and SFML.

  5. Anyone else never installed or attempted any of these but just thoroughly enjoy watching it be done and explained here? Dan, you are amazing at this. (<- 'this dot' pun)

Leave a Reply

Your email address will not be published. Required fields are marked *