Placeholder Image

Subtitles section Play video

  • (bell dinging)

  • - Hello, all right.

  • (scoffs)

  • I'm still working with Mastadon

  • and what I'm going to do in this video now

  • is I'm going to take the sample bot that I made

  • and instead of just on a timer,

  • every 24 hours, every 60 minutes,

  • I happen to post something.

  • I toot.

  • What I'm going to do is I'm going to use the streaming API.

  • The streaming API is a way for me to,

  • in real time, listen for events.

  • And the particular kind of events

  • that I'm going to listen for

  • are what's known as user events.

  • So a user event, and we'll see all the different kinds

  • any time that I might get a notification

  • or somebody that I follow might post something

  • or anytime that I might...

  • Anyway, there's lot of things that come in, user events.

  • And these are the good ones to use

  • because if you're using your user event as a bot,

  • you're sort of making sure that your bot only engages

  • with people who are opting in.

  • And this is pretty important.

  • You don't want your bot just randomly spamming people

  • and favoriting random things

  • or replying to random people who haven't really asked

  • to engage with your bot.

  • So you're going to want to make sure

  • that your bot follows the code of conduct

  • and the terms of service of bots in space.

  • I'll show you where you could find that.

  • But typically, a good way to think about it

  • is if somebody is at mentioning the bot,

  • then you're welcome to reply to them.

  • If somebody follows you, then you're also welcome

  • to engage with that person as the bot programmer.

  • Okay, so let's go over and look at the streaming API.

  • So before I start using the streaming API,

  • let me just point out to read the information page

  • with the code of conduct and also the terms of service

  • if you're choosing to post your bot on botsin.space.

  • You're going to want to make sure you follow the rules

  • of the space.

  • Okay, now, what I'm going to do

  • is I am going to start using the streaming API.

  • So the way that I do that,

  • and we can find it here.

  • Remember this is the node package that I'm using,

  • mastadon-api.

  • And this is basically what I want to do.

  • I want to create a listener

  • and whenever there is a message,

  • I want to take a look and do stuff.

  • Act upon that message.

  • So let's actually do exactly this.

  • I'm going to keep the error one in here as well.

  • I'm just going to copy, paste this

  • into my code.

  • I'm going to comment out this auto-posting thing

  • that I had before about the meaning of life.

  • I'm just going to put this here.

  • And actually, what I want to do now

  • is I want to use my little trick.

  • Instead of just console logging the message,

  • if you remember a little trick I did

  • in the previous video,

  • is I used write file.

  • So I'm going to write files out

  • so I can look at what kind of messages I'm getting.

  • Oops, (screaming) where have I gone?

  • And so let me un-comment this out.

  • I do want to put, like, a timestamp also here.

  • So it makes sense for me to say, like,

  • data and then, actually,

  • the message probably has a timestamp built into it

  • but I can also, javascript, timestamp.

  • I think it's just like new date, get time.

  • Yeah, so I can say new date, get time.

  • I think this is right.

  • Again, you can put with template literals,

  • I can put a whole string...

  • A whole line of code to evaluate,

  • in essence, inside that area.

  • So let's see if this works.

  • I don't know what's going to happen.

  • Let me see.

  • Am I in the right place?

  • No, sorry, I made a new folder

  • so I'm going to release these examples separately.

  • I'm going to run this bot.

  • And now, okay.

  • I don't know if it's working

  • because I don't know if I've gotten any notifications yet.

  • Maybe somebody watching this live

  • is going to favorite something or at mention my bot.

  • That would be nice, right?

  • And then something would come in through here.

  • Oh, I did get a message from somebody

  • but I made some sort of mistake.

  • Data is not defined.

  • Okay.

  • Because, oh yes.

  • The variable name is MSG for message

  • which is why I was thinking that, I guess.

  • So it should be MSG here.

  • Let's try this again.

  • And actually, I'm just going to take this out

  • and I'm just going to write console, dot log, user event.

  • All right everybody.

  • Are you watching?

  • Are you giving me some user events?

  • Here we go.

  • Waiting for my user events.

  • (sped up techno music)

  • Okay.

  • I think that was enough user events.

  • Thank you very much.

  • Let's go back and we can see here

  • that I have all of these data, dot json files

  • for all of these events.

  • So I can kind of click through them

  • and see what kind of events.

  • I'm hoping that the kind people of the internet

  • are not spamming me with horrible things.

  • The first kind of event we go here is a follow event.

  • So if the event is a notification of type follow,

  • we can act on that.

  • So let's do that.

  • Let's say...

  • Let's go back to our code.

  • And I'm going to say...

  • Right here.

  • I'm going to not write these out anymore.

  • If message dot, what was it again?

  • Message, dot event equals notification

  • and I think there's going to be different kinds

  • of notifications so I'm going to say

  • if message, dot data,

  • dot type, follow,

  • then what I want to do is I want to get the username.

  • Let me get the username

  • and that would be where?

  • It would be, ah, right there.

  • And actually, I want the account.

  • The username is useful

  • but you always, on Mastadon, need both the username

  • and the instance, the address or the instance.

  • The host name.

  • Let me grab account equals message, dot data, dot account.

  • And then the other thing that I pretty sure

  • that I need is the ID, maybe.

  • Seven, six, seven, zero.

  • That's the account ID.

  • This is the...

  • I don't know if this the ID of the event, I guess?

  • So I want that account ID.

  • So I'm going to say constant ID equals message,

  • dot data, dot ID.

  • Oh!

  • Dot account.

  • I forgot about account.

  • Message, dot data, dot account, dot ID.

  • Dot account, and then dot account, dot ID

  • and then I want to send a message.

  • So how do I do that?

  • Just with this nice M, dot post.

  • So there's the thing.

  • Maybe I want to make this, quote-unquote,

  • toot function a bit more generic

  • and I'm just going to give it...

  • I'm going to pass in a status.

  • So I'm going to get rid of the random number stuff,

  • which was from before.

  • And then I'm going to just put...

  • It's a little confusing

  • but I'm going to take whatever I pass in

  • and then here is...

  • And then I'm going to post that.

  • Now I have a function that I can basically say, toot.

  • Then I can say...

  • I'm going to use at, data dot...

  • Message, dot data, dot account.

  • This is me referencing the person that followed me.

  • Thank you for the follow.

  • I'll just say, "Choo, choo.

  • "Welcome aboard."

  • That's a train theme, welcome aboard.

  • So we can see it'll say welcome aboard.

  • Now here's the thing.

  • I really should also...

  • If I go back to the API.

  • This one.

  • I should probably say in reply...

  • And that's a reply to the...

  • Actually I don't need...

  • It's not in reply to a specific status

  • so this is actually fine.

  • I think as long as they at mention that, I'm done.

  • So let me go.

  • I've lost my code.

  • I think I'm good.

  • I think that I have everything I want.

  • Let me just put the listen on air up here.

  • So I'm listening for a message.

  • If the message is a notification of a follow

  • then I will toot back to the person,

  • their account name and say welcome aboard.

  • Let's see how this goes.

  • I look forward to all of you now.

  • You can unfollow and follow if you already followed.

  • Let me run it first.

  • And here we go.

  • Oh, okay, I'm at status, status.

  • I have a mistake here.

  • I'm going to just change this variable name to TXT.

  • Or content.

  • Let me make it content.

  • I don't like having the same name everywhere.

  • It's confusing.

  • But there should be no semicolon after there.

  • Okay, here we go.

  • (instrumental elevator music)

  • Welcome aboard!

  • Welcome aboard.

  • Welcome aboard.

  • Okay, so you can see a bunch came in

  • and now I can go back here

  • and I can go to here

  • and we can see, look at this.

  • These are all the people who have now...

  • And if I click there we can see...

  • It's showing me and going to these people's accounts.

  • Yay!

  • So we now have a bot that responds to follows.

  • Alka from the chat pointed out something here.

  • What did I do?

  • Oh, the whole point in making this variable

  • was so that I don't have to write this all out here.

  • I don't know why I did that.

  • So I can just do this.

  • This will make it much more readable.

  • And actually don't need the ID so I can take that out.

  • So this is all I need.

  • We are done.

  • All right.

  • So this is follow.

  • Now... (knocking)

  • I'm going to show you something in the next video.

  • I think I'm going to take a break

  • and in the next video,

  • I'm going to look for messages,

  • the at mention the bot

  • and then I'm going to have that bot act on those,

  • either reply to them or favorite

  • or do something like that, okay?

  • So that's what I'm going to do next.

  • (bell dinging)

  • (upbeat music)

(bell dinging)

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it