(bell dings)
- Okay, I'm back.
I'm going to do some,
I made a Mastodon bot.
All it did so far was tweet, it didn't tweet, it tooted.
Toot, tweet, blog, post, who knows what all this stuff is?
All I know is that it said, (whistle blows) choo choo.
So I would like to show you some more things to
make the bot a little bit better before we move on
and hopefully start making some actual interesting
examples of different kinds of bots you might want
to try to make.
So the first thing that I want to do is actually,
I don't want to have all of this, all these secret
keys and everything right here in my code
because I'm going to upload this as an example
for other people to use.
I want to be able to hide that stuff away, but still use it.
There's actually a wonderful node package.
Thank you to Alka for the suggestion,
called a dotenv.
So I'm going to say npm install dotenv.
I'm going to install this node package.
What this allows me to do is create environment
variables for a particular project.
That way I can upload the code without the values of those
environment variables, but anybody who's using that code
could set their own values for those environment variables.
So what I'm going to need to do is create a new file,
I'm going to call it .env.
So it's kind of like, you can see it right here,
it's like a hidden file .env.
It even has this crazy setttings thing.
Then in this, ooh, look there's all these extensions
I could use probably to like format it in all sorts
of fancy ways.
But I'm not going to be fancy.
What I'm going to do and I'm looking over here
because thankfully Alka gave me some suggestions,
I'm going to say things like AUTH_TOKEN=
CLIENT_SECRET= and I'm going to say CLIENT_ID=.
I think those are my three things.
So that they're in the code, client_key is this.
I'm going to put this back in here, client_key,
okay I guess CLIENT_KEY is what I meant.
Does it need to be in quotes?
Maybe somebody can tell me.
Maybe it shouldn't be in quotes actually.
I'm going to get the client secret.
Then I'm going to go back to the env file
and put that in here.
Right now I'm using single quotes, which may or may not
be correct.
Then I'm going to go back to my code.
I'm going to get the access token
and I'm going to call it ACCESS_TOKEN,
just to be consistent with my naming.
Now if I do this, I can now go to my bot
and I can also say const env = require
and then I want to require a dotenv.
Okay, just for consistency's sake maybe I'll make
this capital letters, I don't know.
Then I'm going to say, then I need to call ENV.config
which will load it.
The chat thankfully helped me out that I do not want
quotes here.
It'd either adjust the values
and then I should be able to now, down here somewhere,
I should be able to say ahh, I should be able to say
process.env.CLIENT_KEY.
This will now.
Is that right, process.env?
Let me take a look.
I have the documentation over here.
Yeah, I think that's right.
So I should be able to grab those environment variables.
Whoops, just like this.
Then the CLIENT_SECRET.
Then I should be able to grab this one
and say ACCESS_TOKEN.
Who knows if I've made some mistakes?
But let's try running this now.
Let's "Choo choo!
Um, Choo choo, choo choo!
Let's say choo choo twice and let's run this.
All right (claps hands) I think this might have worked.
We can see.
This is the wrong page again.
Choo choo choo choo!
We could see that work.
So all of this stuff is now hidden inside
my environment file and what I'm also going to do now
is I'm going to make
I'm going to make another file called .env-sample.
Then I'm going to copy this into there.
Into here and then I'm going to take out all this stuff.
I'm going to make, because eventually I'll put this
on git.
I'm going to make a file called .gitignore
and then I'm going to say .env.
So basically what I've done is I'm saying, hey,
this is my file .env.
That's my file that I'm only ever going to have
locally on my computer.
But when I publish this, I'm going to publish a sample one,
which has information basically,
about what you need to put in there and then
I'm going to make sure that the actual .env file is
not included if I ever check this into a git repository
uploaded on git hub, so to speak.
Let's make sure this works.
Still working, we can go here and I've said,
(laughs) somebody definitely hacked my bot,
so I'm going to read which is great,
because I had my keys up on the screen (laughs).
(bell dings) Well done whoever you are.
I am going to take a break and regenerate my access keys
so that nobody else can hack my bot,
keep them hidden in my environment file
and then I am going to, before something bad happens,
I'm going to just close out of this window and I'm
going to come back and show you more about writing
Mastodon bots.
I'm going to show you this fun thing called spoiler_text.
And how to have the bot post to Mastodon periodically
every so often.
(claps hands) Okay so see you in the next video.
(techno music)