12 apps in 12 weeks - App 4

3 min. read

Intro

When I did this project I was on the last day of my challenge for the week. I almost want to say not this week and take a break this day. But when I was watching tv for fun I just got an idea about just making a small chat bot. Nothing too crazy, so I search in the net which one of API I can use for this. I finally decide to go with the Twitter one cause the documentation is really good.

Why this bot?

The first thing I always want to see how this kind of bot is created. I know this is nothing really difficult but someone curious like me always wants to see what happen behind the scene. For like a week before this idea, I was following the #100DaysOfCode challenge on twitter. Where people talk about what they did today in their challenge and what they learn. I know a bot was already built for that with the Free Code Camps team so I just decided to change the way the bot message.

I choose to send motivation message with some super-heroes from “Marvel & DC” cause I loved everything about this. Plus we all know the message they passed in some movies or BD is a good source of motivation.

Some code

1
2
3
4
5
6
7
8
9
10
11
12
export const blackListUsers = [
'_100DaysOfCode',
'heroes_bot',
'javascriptd',
'wintershawls',
'CodeNewbies'
];
if (blackListUsers.indexOf(randomName) !== -1 || new RegExp('bot', 'ig').test(randomName)) {
console.log('THIS IS A BOT');
return;
}

This is the code I wrote for be sure I don’t let my bot to reply to other bots.

This is the only place of code who is maybe not standard all the rest of my code is using async await.

1
2
3
4
5
6
7
8
9
10
11
const tweetIt = async txt => {
console.log({ txt });
console.log('TWEET ON THE WAY');
try {
await T.post('statuses/update', { status: txt });
console.log('TWEET SENT');
} catch (err) {
handleError(err);
}
};

I really like the async await syntax. I just started to use it and that make the code so much cleaner. I know the promise is a big step from the callback hell. But this async await is the better for human readability.

Last Word

That was a quick little challenge and I recommend everyone to just try to make one. This is a quick little project where you can learn some good practice and for the beginner to understand how to read API documentation.

Library use