How to integrate ChatGPT with WhatsApp for customer support

Use the power of ChatGPT to provide streamlined support to customers on WhatsApp. Here's how to integrate ChatGPT into WhatsApp.

Use the power of ChatGPT  to provide streamlined support to customers on WhatsApp. Here's how to integrate ChatGPT into WhatsApp .

How to integrate ChatGPT with WhatsApp for customer support Picture 1How to integrate ChatGPT with WhatsApp for customer support Picture 1

Many businesses today use messaging apps like WhatsApp to communicate with customers. This led to a spike in automating customer interactions with chatbots.

Take for example the ChatGPT chatbot, a massive OpenAI language model that you can integrate with WhatsApp to automate customer interactions by providing quick and accurate responses to guest requests.

How to access ChatGPT API

Before integrating with WhatsApp, you need to have a basic understanding of ChatGPT and ChatGPT API. ChatGPT is a large language model that takes text-based queries and returns human-like responses. OpenAI provides a simple API interface for programmers to access and use ChatGPT's GPT 3.5 & 4 modules.

To access the ChatGPT API key, you need to navigate to the OpenAI API platform. After logging in, click on the profile icon. Then, click the View API keys option .

 

How to integrate ChatGPT with WhatsApp for customer support Picture 2How to integrate ChatGPT with WhatsApp for customer support Picture 2

Next, click Create new secret key to generate a new API key.

How to integrate ChatGPT with WhatsApp for customer support Picture 3How to integrate ChatGPT with WhatsApp for customer support Picture 3

Enter the required details, for example the name of the key.

How to integrate ChatGPT with WhatsApp for customer support Picture 4How to integrate ChatGPT with WhatsApp for customer support Picture 4

A secret key will be prompted.

How to integrate ChatGPT with WhatsApp for customer support Picture 5How to integrate ChatGPT with WhatsApp for customer support Picture 5

 

Copy and store the key in a safe place for later use.

Set up a WhatsApp Business account

To integrate ChatGPT with WhatsApp, you must set up a WhatsApp Business account. Note, WhatsApp Business is different from normal WhatsApp. This account will allow you to access the WhatsApp Business API, which you will integrate with the ChatGPT API.

You can use a third-party service like the Kommunicate chatbot, WhatsApp, or a Python script to integrate ChatGPT with WhatsApp. This tutorial focuses on integrating ChatGPT with a Python script. It will then give you an overview of the integration using a WhatsApp chatbot.

Use Python script to integrate WhatsApp with ChatGPT API

Before you start programming, create a virtual environment for your application using Pipenv as follows:

pipenv install django djangorestframework openai

This command will install the django , djangorestframework , and openai packages . Then create a new Django project.

django-admin startproject whatsapp

Navigate to the whatsapp folder and create a Django app named gpt :

py manage.py startapp gpt

In whatsapp/settings.py file , add new app, gpt on installed apps list as follows:

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'gpt', ]

In the whatsapp/urls.py file , add the gpt app URL . This action will lead to the URLs you will create on the gpt app :

from django.contrib import admin from django.urls import path, include urlpatterns = [ . path('api/', include('gpt.urls')), # gpt app URL ]

On the gpt/views.py file , add the following block of code to create the ChatGPT API viewer:

from rest_framework.response import Response import openai from rest_framework.views import APIView class OpenAIGPTView(APIView): def get(self, request): input = request.GET.get('q') openai.api_key = "ENTER_OPENAI_API_KEY" completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": input}] ) answer = completion['choices'][0]['message']['content'] return Response(answer)

The view sets up an API endpoint, waiting for a GET query with query parameter q including user input. It then uses OpenAI's gpt-3.5-turbo model to generate a response based on the provided input and return the result as the API output.

Next, create the file urls.py and register the API view window by adding the following line of code:

from django.urls import path from .views import * urlpatterns = [ path('chat', OpenAIGPTView.as_view()), ]

 

Run the migrate and runserver commands as follows:

python manage.py migrate python manage.py runserver

Test the /api/chat endpoint by sending a GET query to http://localhost:8000/api/chat?q=Hello .

Result:

How to integrate ChatGPT with WhatsApp for customer support Picture 6How to integrate ChatGPT with WhatsApp for customer support Picture 6

Next, install Go if you don't already have it installed on your computer.

Clone the Whatsmeow client with the command below:

git clone https://github.com/Huskynarr/whatsapp-gpt.git

Navigate to the repository , whatsapp-gpt , and on this file, main.go update this line:

url := "http://localhost:5001/chat?q=" + urlEncoded

Wall:

url := "http://127.0.0.1:8000/api/chat?q=" + urlEncode

Save changes and run this file:

go run main.go

This app will prompt you with a QR code to login to your WhatsApp account.

Result:

How to integrate ChatGPT with WhatsApp for customer support Picture 7How to integrate ChatGPT with WhatsApp for customer support Picture 7

Navigate to the WhatsApp mobile app and navigate to Settings > QR code > Scan code to add the linked device and scan the QR code that appears on your terminal.

Once logged in, this app will start listening for incoming notifications. If someone sends a notification, the received message generates a GET query to the API with the message as a query parameter and sends a response back to the sender on WhatsApp.

Result:

How to integrate ChatGPT with WhatsApp for customer support Picture 8How to integrate ChatGPT with WhatsApp for customer support Picture 8

Use WhatsApp chatbot to integrate application with ChatGPT API

Another way to integrate ChatGPT into WhatsApp notifications is to use a WhatsApp chatbot. After signing up for the WhatsApp Business API, use a chat builder like OpenDialog to create a chat thread. Then you test the chatbot and add it to your mobile.

Integrate OpenAI API with WhatsApp Chatbot

To integrate WhatsApp chatbot with OpenAI templates, use API key from OpenAI. Although this method is easier when you have a chatbot, you should only choose it when you are comfortable with the possible risks.

 

Configure automatic responses

For better customer service, configure automatic responses to provide fast and accurate customer message responses. These responses will personalize customers by name and provide relevant responses to customer queries.

Testing WhatsApp chatbot

Like all AI API system, testing is key before you implement WhatsApp chatbot. This ensures it works as expected. API testing tools like Postman and Curl can help you test chatbot responses.

In this case, the Django REST framework provides a browseable API to help test the API query and response.

Measure success and fine-tune

After implementing a WhatsApp chatbot, it is important that you measure its success and usage over time. This helps you fine-tune it accordingly. Analytics tools like OpenDialog and Rasa can help track metrics like response time, retention rates, and customer satisfaction.

Above is how to integrate ChatGPT into WhatsApp . Hope the article is useful to you.

5 ★ | 1 Vote