Integrate Todoist into a Whatsapp bot

Integrating the Todoist API to add, complete and delete tasks using our Whatsapp bot.

Integrate Todoist into a Whatsapp bot

Integrating the Todoist API to add, complete and delete tasks using our Whatsapp bot.

In an earlier post, We went over how to set up Todoist in our node JS project to integrate it with the Whatsapp bot. We set up the API and also fetched and displayed all our pending tasks. You can read the post here.

In this post, we will go over adding new tasks, completing existing tasks and also deleting unwanted tasks.

Adding a new task

If we follow the documentation to add a new task, we can see that we need to provide the content for the task and use a unique ID every time we add a new task. So let's set up a basic command where we can enter the task content and it automatically gets added to our to-do list. I think the command add TASK_NAME would be a good start.

const addTask = async (task) => {
  // We create the structure expected by the Todoist API.
  const taskObject = { content: task, due_string: 'today' };
  try {
    const response = await axios.post('https://api.todoist.com/rest/v1/tasks', JSON.stringify(taskObject), {
      headers: {
        'Authorization': `Bearer ${TODOIST_TOKEN}`,
        'X-Request-Id': uuid(), // I used the uuid package from npm.
        'Content-Type': 'application/json',
      }
    });
    const { content } = response.data;
    return content;
  } catch (error) {
    console.log('error', error.response.status, error.response.data);
    // We return a very basic error if something goes wrong.
    return 'There was an issue adding the task.';
  }
}
Add task using Todoist API.

Once we get this setup, we should be able to add our tasks to Todoist using Whatsapp. The tasks by default get added into the Todoist inbox and I am adding a deadline of the same day as I intend to use this for immediate tasks. You can explore the API further and even go as far as adding the tasks to a specific project.

Completing a task

If you look at the documentation for completing/closing a task, you will see that we need the task ID. To get this, I updated my original task list API to return the task ID along with the content and the due date. Now I can just send back the ID in my command to tell Todoist to complete that task - complete TASK_ID.

const completeTask = async (taskId) => {
  try {
    const response = await axios.post(`https://api.todoist.com/rest/v1/tasks/${taskId}/close`, {}, {
      headers: { 'Authorization': `Bearer ${TODOIST_TOKEN}` }
    });
    return 'Task completed! 🚀'
  } catch (error) {
    console.log('error', error.response.status, error.response.data);
    return 'There was an issue completing the task.';
  }
}
Complete task using Todoist API.

Deleting a task

The deletion API is very similar to the completion API except for the fact that we have to use the DELETE request instead of the POST request. We will also be using a similar command where we just say delete TASK_ID.

const deleteTask = async (taskId) => {
  try {
    const response = await axios.delete(`https://api.todoist.com/rest/v1/tasks/${taskId}`, {
      headers: { 'Authorization': `Bearer ${TODOIST_TOKEN}` }
    });
    return 'Task deleted! 💣'
  } catch (error) {
    console.log('error', error.response.status, error.response.data);
    return 'There was an issue deleting the task.';
  }
}
Delete task using Todoist API.

We can now see our list of tasks, add new tasks, complete pending tasks and delete unwanted/canceled tasks. Here is a screenshot with all the features in action!

All Features

With these features, I think I am done with the Todoist part of my Botler experiment. You can find the updated code here!

What other features do you think I should add to the bot? Feel free to hit me up with your ideas on Twitter!

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.