Learn everything about Analytics

Home » Creating a Callback to Send Notifications on WhatsApp in Keras and TensorFlow

Creating a Callback to Send Notifications on WhatsApp in Keras and TensorFlow

This article was published as a part of the Data Science Blogathon.


Training a deep neural network takes a long time in any project. Personally, I initiate the training process and leave my
laptop alone peacefully for some time. But sometimes when I check my laptop after a few hours, I am welcomed with an error message which might have occurred during training and interrupted the entire progress.

In such cases, one would want to be informed as soon as the training process stops (due to an error or after completing the specified number of epochs) so that one can take the next steps accordingly with minimal wastage of time.

Text Categorization

In this tutorial, we will create a Keras callback that sends notifications about your deep learning model on your WhatsApp. Although, this tutorial covers creating notifications for the beginning and end of the training process, however, this approach can be extended to any other use-case.


This tutorial is broadly divided into 3 segments

  1. Sending messages to WhatsApp using Python
  2. Understanding Keras Callbacks and creating one
  3. Tying everything together


Section 1: Sending messages to WhatsApp using Python

There are a number of methods for connecting the WhatsApp messenger with python. However, the simplest one is the one using Twilio. Twilio is a communications platform that allows developers to send notifications on different platforms such as vanilla text SMS, WhatsApp, and even voice calls. It offers a very clean and concise API for connecting WhatsApp with python.

Step 1. Head over to this link and sign-up for Twilio.

Step 2. Activate the Twilio Sandbox for WhatsApp by following this link. You will be able to see an interface like the one below. You can activate the sandbox by saving this number (highlighted) as a contact on your phone and sending the message ‘join main-circle’ to the saved contact. Wait for the interface to show you a confirmation message.

Step 3. Head over to your Twilio console by clicking on the Twilio logo on the top left corner of your screen or clicking this link. Here, you can find your Account ID and Auth Token which will be needed while setting up your Twilio client in Python.

Step 4. Setting up the Twilio Client in Python and Sending your first message. Open a code-editor and paste the code available here. In the script, the account_sid and auth_token are the tokens obtained from the console as shown in Step 3. ‘Your_whatsapp_number’ is the number where you want to receive the text notifications. Finally, ‘From_number’ is the phone number you encountered while setting up the Twilio Sandbox for WhatsApp in Step 2. Note – Include the country code in the numbers. For eg – if your phone number is 112233445 and the country code is +91, you have to write +91112233445 in ‘Your_whatsapp_number’ field.

Upon executing this script, you will see a WhatsApp notification on your mobile from the number obtained in Step 2.

Section 2: Understanding Keras Callbacks and creating one

The official Keras documentation defines a callback as a “set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training”.

While training your deep neural networks, you might have faced situations where you want to record different metrics such as accuracy, loss to a file, or changing the learning rate with epochs (adaptive learning rates). In these situations, we use an object of a class called Callbacks which allows you to retrieve, set values of different model parameters during different stages of training, testing, or evaluation process. The official TensorFlow guide is an excellent resource for learning how to create your own Callback.

The TensorFlow team has done an excellent job in naming the methods in the Callback class which makes it really simple to understand what each function is intended to do/ capable of doing. The naming convention used names each function in the form of on_{train/ test/ batch/ epoch}_{begin/ end}.

For our task, we want to know when a model stops training. This information could be obtained from the function on_train_end(). Hence, we can create a class ‘WhatappCallBack’ which extends the Callback class of Keras. Inside this class, we will overload the on_train_end() function to in order to know the information required.

First, we encapsulate the code that we wrote for sending a message using Twilio in a function named ‘send_message’. This function takes in the text that we want to send as a parameter. Next, we define our Callback and overload the on_train_end() function. Don’t worry, the entire code is available here in the form of a Jupyter Notebook! I am attaching some images below for better understanding.

Section 3: Tying it all together

In order to put our function to test, we need to define a simple feed-forward network and create a dummy dataset. This will be demonstrated in the next steps.

Step 1. We use sklearn library’s make_classification method to generate 1000 random sample data points and a very simple ANN.

Step 2. We create an instance of the ‘‘WhatappCallBack’ ’ class and pass this instance in the callbacks argument while fitting our model.

Step 3. Enjoy the results

You can find the entire code script here:


Reach out to me in case you have any questions!

You can also read this article on our Mobile APP Get it on Google Play

One Comment