Placeholder Image

字幕列表 影片播放

  • (ding) - 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 M.stream.

  • Right, I have to go look it up here in this API.

  • M.stream 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 response.data.type 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