字幕表 動画を再生する 英語字幕をプリント (bell dings) - Hello, welcome to the second Mastodon video, in the first video I talked a bit about what Mastodon is or at least my impressions of it, the idea of decentralization. And in this video, the reason why I'm doing this, is that I want to find a platform to write crazy weird experimental avant-garde art bots. And so, I'm going to use this platform. (creepy piano music) Bots in space! The sound that that, this was going to, ya know this, yeah it just seemed better when I thought of playing this music then the way it turned out but, so the first thing I'm going to want to do is sign up for an account here, bots in space. And I am going to say sign up with coding train bot and I am going to use the email address, Daniel at the coding train dot com. I'm going to use the password, ooh suggested password, I don't think I should use this, because then you will all see it. I'm going to use the password, I love blueberries or heart heart rainbow. Sure you'll all be able to guess my password really easily. What is, what does this nonsense, go away! A message with confirmation link has been sent to your email address. Verify email address, ah, my email address has been... (bell dings) Okay, so I clicked the verify my email, I have now put in my username and password. I'm going to click log in, and now welcome to Mastodon. I'm going to save this, just why not. This is my full handle at coding train bot at bots in space. I'm not going to skip through this little tutorial here, but first thing I'm going to do, very important, is I'm going to go to edit profile, and I'm just going to go down here. I would want to put other stuff here, maybe I'll do that eventually, but I want to go here and I want to click this is a bot account. Because one of the nice things about Mastodon, it has a feature for you to indicate that a particular account is a bot, which is, you know, a really nice thing to do in terms of transparency. Okay, the other thing that I want to do is I want to go down under development, so I'm a hit save changes, we're down here in the bottom there's a little option under development, I'm going to want to click that, and I want to create a new application. So I'm going to create a new application, it is coding train example bot. The website will just be thecodingtrain.com. I want to, what do I want to be able to do, so. You have a lot of options in terms of what permissions you can give your bot. Like maybe the bot can only read certain things, or only write certain things. But I'm actually going to keep the red global write checked, and the red global read checked, so that everything, and follow, so this bot can basically do everything. It can read all the posts, it can write as many posts as it wants, and it can follow. So I'm going to hit submit and then I'm going to click here. And now, now the thing is, this is stuff that I really do not want to share with anybody. So what I'm going to do is I'm going to quickly just turn the screen off, no that's me. I'm going to turn the screen off, there we go, I'm going to regenerate that access token. I'm going to go back to, I'm going to copy this stuff somewhere secret, actually I'm not going to worry about that now, I'm going to go back to Mastodon, then I'm going to put the screen back, there I am. So that you can't hack my bot at this moment. Alright, so, what's next, I want, there's a lot of different ways you could write a Mastodon bot. I'm sure you could come up with some way that I can't even think of right now. I'm going to use node.js, which is a JavaScript framework, for executing JavaScript code. And I am going to do all this from the command line. So I, you're going to want to have node installed and you're going also to, you're going to want to have some shell access to your computer to be able to do this. And I'll link to different workflow videos about how to get that stuff set up if you don't have that already. So, what I'm going to do is I'm in a directory, I'm on coding train desktop Mastodon bot. The first thing I'm going to do, is I'm just going to type npm init. What this does is it starts up my node project. This is going to create a package.json file, which has all the configuration information for my node project, I could just manually make that file or copy it from another project, but it's nice that this tool will step me through it. So package name Mastodon bot sounds good. This is version, I dunno, 0.0.1 The description is example code for Mastodon bot on coding train. Entry point, I'm going to have a file called bot.js. I'm not going to worry about testing right now, I don't have a Git repository. Bot, Mastodon, education, will be some keywords. The author is me, I guess license will be MIT, and this looks all looks good to me. So now, if I open this up in Visual Studio Code, we can see it has created a, it has created a package.json file with all of that information in it that I just typed. Okay, the next thing that I want to do is create my JavaScript file that is going to run the bot. So I'm just going to click new file and I'm going to say bot.js, and just to test things out I'm going to say console.log Mastodon bot starting dot dot dot. So, this is where all of my code for the bot is going to go. And the very first thing that I'm going to do is just test this code by executing it. And the way that I do that, is going back to terminal, and by the way, yes, there is actually a terminal directly in Visual Code Studio, and I could actually just run it right here, by saying like node bot.js, and maybe I should do that. This is so tiny though, I don't know why, I'm just not used to using this terminal yet. So I'm going to stick with my using a separate iTerm. And I'm going to run node bot.js. And we can see all it's doing is console logging that Mastodon bot starting. Okay, how am I going to connect to Mastodon? So, in order to do this, I'm going to use a node package that will communicate with the Mastodon API directly. So let's find that, sure let's look at notifications. Let's search node Mastodon API and let's see. So what comes up, Mastodon dash API npm. This one looks pretty good, let's go over to GitHub and see. Alright, so this is actually the one, this is the one that I'm going to choose to use. It was last updated eight days ago, I've used this before in making a practice example, so this one is going to work fine for me. And so now, the next thing that I need to do, is I need to run npm install. Because if I'm going to use this package to communicate with my bot, I need to make sure I have it installed. So this npm install dash dash save Mastodon API, and yes, I could use Yarn which is another package manager for node, but I've still, I'm like an npm person. Whenever there's the thing that's people are using instead of Yarn, that's when I'll use Yarn. Npm install, save, the dash dash save by the way, is a argument so that it automatically gets added to the package.json file and there we go. So if I go over here we can see if I look in package.json, there is now also a dependency, Mastodon dash API. So that is now a project dependency. Doot doot doot doot doot doot, breaking news, Alka in the chat is telling me that you no longer have to use dash dash save, so maybe it adds it automatically whether or not you use dash dash save or not. That's a nice little extra tip for today. Okay, what I want to do now is require it. And again, I'm not using, I'm going to, sort of still, I'm using require. So I want to say const, Mastodon, let's just call it const m maybe, no let's, I don't know, Mastodon equals require Mastodon dash API. So I am now requiring it, which means, that's basically, I'm importing it. And then what I want to do is I want to connect. And there's a lot of ways that you can connect live in your code because maybe you're making an app and you want people to be able to authenticate with different accounts. I don't need to do any of that. I just want to, do this. New, constant m equals new Mastodon. And I'm going to do this, and I'm going to need to put in my access token, I'm going to need. I'm not too worried about this, and I need to put in my particular instance, which is bots in space. So this is the, since I am now going to run my bot through bots in space, the API URL for making API queries is at bots in space, bots in dot space slash API slash v one. Now maybe someday there'll be a v two of the API, a v three, but this should always work. And now I just need to get that access token, which I'm throwing caution to the wind, by the time you watch this video, the access token will have changed. But just to show you, I'm going to go back to here, I'm going to go to settings, that's fine, I'm going to go to, where am I, development, coding train example bot, I'm going to grab the access token, I'm going to paste it here, and then I think, I'm pretty sure I need also the client secret, which is this thing, and... Client key, which is this thing. And then now, I should be all set and connected. So the next thing that I can do if I'm looking through this API, oh yeah, this is actually what I wanted to do, yeah. Is I can so now, ah, I'm being told that I wrote secrent. So that should be secret, thank you very much. Alright, so here's the thing, we got to talk about something. There are three core ways to engage with the Mastodon API. Sad to erase my beautifully decentralized diagram. There are three ways to engage with the Mastodon API. And this is very similar to how Twitter used to work, Twitter doesn't work this way anymore, but luckily for us Mastodon does. There is the get, a get request is basically saying, hey, I would like to get a whole bunch of stuff, like get me this list of user accounts, get me this list of posts, toots, statuses, tweets, whatever you want to call them. Search for every toot that has the word mango in it, that's what get is for. There is also post, I really should have written these in all capital letters, post is for posting something to Mastodon. Like for example, I want to post a new status, or I want to follow, or favorite, or reblog something, all of those are posts. And then, there is what is really, the exciting thing, the streaming API. The streaming API is a way for you to essentially have almost like a socket connection. A like, an attached connection to Mastodon where you're just listening for events. So every time somebody follows somebody you'll get an event, or every time somebody posts a status you'll get an event. So this is what's really powerful in the way that you can kind of engage with the service in a realtime synchronous way. And unfortunately, this a thing that Twitter recently removed from their API. And there's other ways to do the same exact thing, but it's a bit roundabout, plus, good luck getting your bot approved from Twitter or something that you can use there. Okay, so in this video to test the first idea out, I just want to do a post, so what I want to do is a post. Okay, so coming back over here, we can see this is what I have to do. Post with a sum path, now what is the path? So now we need to look up Mastodon API documentation. And we should be able to on this page here look under posts, and we can see, I can, this is a post for following something, for blocking, unblocking, for muting. But I want to post a status. Hold on, there we go, sorry about that. This is where it is, I want to post a new status. So remember, what is my API... URL, it's this one, so ultimately the endpoint that I want to go to is v one statuses. But I don't do it here, the path, I could put right here, statuses. Then I need some parameters and I'm going to put a callback, which I will use my fancy ES6 arrow syntax that I have covered in other videos, I'll come back to that in a second. So params, let's make that a separate variable, to JavaScript object, so these param, this is super important, what are the parameters? Well, we can go back to the API documentation, and we can look, here they are. I can say this is the status, the text that I want to post. In reply to, well, is it in reply to somebody, a different, an ID of a different status. Media IDs for images, all sorts of stuff here. Language, is it sensitive content, there's all sorts of things. But basically I'm just going to say, status choo choo. And there we go, why, why did you format yourself like this? What kind of crazy indentation is that? This isn't right, I'm going to have to deal with that later. Okay, so now, and then in the callback, what do I get? It's probably like error, first callbacks I'm guessing. Error or data, I'm just do this. So let's just do, say like, if error, console dot error error. And then, otherwise, console dot log data, okay. So now, here we go, I have my code, right. I started the bot, I connected, there's all my secret information, credit card number and social security number's embedded in there somewhere. Here's my status that I want to post and there we go. And hopefully nobody's already hacked my account and posted stuff. So let's go look over here... Okay, wait, no I'm in the wrong account, where did I, got to go to bots in space. And I got to go to my profile, which isn't anything yet, somewhere it'll show up here. Where's my profile, is this me? That's local, who knows, okay, let's run this code. Node bot.js, okay, we got an error, Mastodon is not defined, why is Mastodon not defined? Because I used a capital down here and I didn't use a capital up here. So this Mastodon, importing the whole library is now a function that I can call to connect. So let's try this again. Whoo, that seemed to have worked except it didn't console log anything. Thank you to Wilbur in the chat, who just told me that I actually put the wrong path here to the API, so if I go back to the API documenation, you can see the path, the endpoint is statuses plural. So let's try this one more time. Statuses, and now, let me run this again. Ah look, and we can see we got all this metadata back, because it was successful, if I go back here, if I go to bots in space, look at that, there we are, choo choo! (train whistle) Okay, we did it, we successfully made our first Mastodon bot that is posting just from... Just from the node, directly to Mastodon. Okay, so there's a few things that I want to show you next. I would like to show you, number one, is how to effectively hide your, you can open source your bot, but still hide your client secret, client code, all that sort of stuff, so I want to show you that. And also, we'll just look at some of the other parameters for... Some of the other parameters for posting. (energetic music)
A2 初級 4.2: Mastodon Bot - Mastodon Botの設定 (4.2: Mastodon Bot - Setting up a Mastodon Bot) 2 0 林宜悉 に公開 2021 年 01 月 14 日 シェア シェア 保存 報告 動画の中の単語