Build your own personal Whatsapp bot

A guide to building your own personal Whatsapp bot and customizing it however you'd like.

Build your own personal Whatsapp bot

A guide to building your own personal Whatsapp bot and customizing it however you'd like.

I love bots, both building and using them. They make complex tasks simpler, mostly by reducing the number of steps you need to do before completing them. Earlier this year, I built a bot that gets information from the Fantasy Premier League website. It would let you look up players, their teams, your individual score and also notify you of price changes. I later had to move the bot to Telegram because I don't have a registered company so I couldn't launch it on Whatsapp but that's a story for another day/post. You can check it out here if you'd like.

But today, I had another idea, to build a personal Whatsapp bot. Think the Alfred to my Batman. It could do nothing but at the same time, it could do anything and everything I wanted. So, let's get started.

Prerequisites

You need to go through the initial steps that involve signing up for and setting up the bot using Twilio. I started writing about it and then remembered that I myself followed this post by Samarth Jajoo. In his tutorial, he uses repl.it. I faced some issues with repl.it so I personally opted for Glitch. I also set up a very basic app on Glitch that responds with Hello there to every single message. You can fork it here if you'd like to use Glitch as well. Just make sure you set up the environment variables in the .env file properly. You can find your account ID and auth token for Twilio here.


Features

To begin with, I thought we could start with something simple and get the bot to show me my to-do list.

Let's start by integrating the Todoist API so we can fetch our tasks. The first thing we need to do is get our Todoist API token and add it to our project so let's create an environment variable called TODOIST_TOKEN. You can find your token here. We also want to install axios and moment to our project as we will be using those libraries to help us with our implementation. Oh, and it always helps to have the documentation handy, so here's a link.

First, we want to make a request to the Todoist API to fetch all our active tasks.

const response = axios.get('https://api.todoist.com/rest/v1/tasks',
	{ headers: {'Authorization': `Bearer ${TODOIST_TOKEN}`} }
);

const tasks = response.data;
API request to get active tasks

Next, we want to filter and get only our pending tasks.

const pendingTasks = tasks.filter(task => {
    if (!task.hasOwnProperty('due')) return false;
    const dueDate = moment(task.due.date);
    return dueDate.isSameOrBefore(moment());
});
Filtering to get pending tasks

We can now format the tasks any way we want to before we send them back as a message to ourselves. I, for now, am going to fetch the task content and its due date.

const formattedTasks = tasks.map((task, index) => {
    return `${index + 1}. ${task.content} (${task.due.string})`
}).join('\n');
Formatting tasks before sending them as a message

Once we put it all together, this is what it looks like.

Tasks

We can use this as a starting point and implement things like adding new tasks, completing existing tasks and even deleting unwanted tasks. I'll probably write another post going over how to implement those features into our bot. In the meantime, you can try rebuilding this bot for yourself and tell me how it went!


Feel free to throw any questions/ideas about the whole project to me on Twitter. You can also find my version of the app on Glitch and simply fork it if you don't want to bother with the implementation yourself. You can find it here.

Tags:

Faraz Patankar

Probably obsessing over fantasy football, figuring out what to eat for my next meal or working on my next great (soon to be abandoned) side project but hello to you too! 🤗

More posts from this author

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.