Introduction
WhatsApp is one of the most popular messaging apps in the world, with over 2 billion active users. It's an easy and convenient way to stay in touch with friends and family, but what if you could automate your WhatsApp messages and have an AI chatbot respond to your contacts in real-time without any third party library? In this post, we'll explore how to use ChatGPT, a large language model trained by OpenAI, to reply to messages on WhatsApp.
Our goal is to have ChatGPT respond to incoming messages on WhatsApp, without using Twilio or any other 3rd party service which costs money.
Prerequisites
To follow this tutorial, you will need the following:
- A WhatsApp account
- Go installed on your computer
- Basic knowledge of Go programming language
- Basic knowledge of Django web framework
- A basic understanding of REST APIs
Setup
We will start by setting up the Django REST API. Follow the steps below:
Create a new Django project using the django-admin startproject command.
Navigate to the directory djangoapp we created and establish a Django project.
(myenv) $ django-admin startproject mainapp
This will auto-generate some files for your project skeleton:
mainapp/
manage.py
mainapp/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Now, navigate to the directory you just created (make sure you are in the same directory as manage.py) and create your app directory.
(myenv) $ python manage.py startapp monitor
This will create the following:
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
On the mainapp/settings.py file, look for the following line and add the app we just created above.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',#new line
'monitor', #new line
]
Ensure you are in the monitor directory then create a new directory called templates and a new file called urls.py. Your directory structure of monitor application should look like this
monitor/
__init__.py
admin.py
apps.py
migrations/
templates/
__init__.py
models.py
tests.py
urls.py
views.py
Ensure your mainapp/urls.py file, add our monitor app URL to include the URLs we shall create next on the monitor app:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
#path('admin/', admin.site.urls),
path('', include('monitor.urls')),#monitor app url
]
Now, on the monitor/urls.py file, add our website there:
from django.urls import path
from .views import *
urlpatterns = [
path('chat', ChatGPTView.as_view()),
]
- In the app's views.py file, add the following code:
from rest_framework.response import Response
import openai
from rest_framework.views import APIView
class ChatGPTView(APIView):
def get(self, request):
input_text = request.GET.get('q')
openai.api_key = "GET YOUR OWN KEY ON OPENAI"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": input_text}]
)
answer = completion['choices'][0]['message']['content']
return Response(answer)
You can now test the /chat endpoint by sending a GET request to http://localhost:8000/chat?q=Hello. You should receive a JSON response.
Now that we have the REST API set up, we can move on to building the WhatsApp chatbot.
Running the Golang 'Whatsmeow' Client.
- Please make sure to install GOlang then git clone the branch below:
git clone -b django-version https://github.com/paulwababu/whatsapp-gpt.git
- Run the go file like so:
go run main.go
- The application will prompt you to log in to your WhatsApp account. Go to your whatsapp on mobile and add a new linked device the way you do whatsapp web, then scan the QR generated on your terminal. It will look like so:
- Once logged in, the application will listen for incoming messages. If a message is received, it will make a GET request to the Django REST API with the message as a query parameter, and send the response back to the sender on WhatsApp.
Screenshots of the bot using my own fine tuned model
Conclusion
In this blog post, we looked at how to build a WhatsApp chatbot using Go and Django. We set up a simple REST API using Django and used the Whatsmeow library for Go to build the chatbot. You can use this as a starting point to build more complex chatbots that can handle different types of messages and provide more personalized responses.
Top comments (4)
I am getting this erro code
Ensure you have rest framework installed in your apps
On your installed apps add the below. That should work.
rest_framework',#new
Thanks for this one
You are welcome!