# Build a WhatsApp chatbot using chatGPT / GPT-3 API: A step by step guide

In this article, we will build a WhatsApp chatbot using chatGPT / GPT-3 API that will respond to user messages with human-like responses.

We will build this app in Python programming language and we will use Twilio APIs to send and receive WhatsApp messages.

If you would rather just use an already-built WhatsApp AI chatbot, try this [**WhatsApp AI chatbot that uses GPT 3.5**](https://aibuddy.chat/). Use Coupon code _**harishgarg-com**_ to get a discount.

Otherwise, read on.

Table of Contents

*   [Overview of the tools and technologies](https://harishgarg.com/#Overview_of_the_tools_and_technologies)
*   [set up and configure chatGPT / GPT-3 API and Twilio](https://harishgarg.com/#set_up_and_configure_chatGPT_GPT-3_API_and_Twilio)
*   [Creating the chatbot logic using FastAPI and integrating it with chatGPT / GPT-3 and Twilio](https://harishgarg.com/#Creating_the_chatbot_logic_using_FastAPI_and_integrating_it_with_chatGPT_GPT-3_and_Twilio)
*   [Conclusion](https://harishgarg.com/#Conclusion)

Overview of the tools and technologies
--------------------------------------

*   GPT-3 (Generative Pretrained Transformer 3) is a powerful language processing API developed by OpenAI. It can be used to generate natural language text and understand user inputs.
*   Twilio is a communication platform that allows developers to integrate messaging, voice, and video into their applications. In this case, it will be used to connect our chatbot to the WhatsApp messaging service.
*   FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It will be used to create the chatbot’s logic and handle requests and responses between the chatbot and the user.

set up and configure chatGPT / GPT-3 API and Twilio
---------------------------------------------------

1.  Sign up for an account on the OpenAI website and obtain an API key for accessing the GPT-3 API.
2.  Install the GPT-3 API Python library using pip: `pip install openai`
3.  Set up a Twilio account and obtain a WhatsApp messaging API key and a Twilio phone number.
4.  Install the Twilio Python library using pip: `pip install twilio`
5.  Configure the Twilio phone number to be able to send and receive messages from the WhatsApp messaging service.
6.  Test the GPT-3 API and Twilio integration by sending a sample message from the Twilio phone number to a designated WhatsApp number.

Creating the chatbot logic using FastAPI and integrating it with chatGPT / GPT-3 and Twilio
-------------------------------------------------------------------------------------------

Install the FastAPI Python library using pip: `pip install fastapi`

Define the chatbot logic and handling of user inputs using FastAPI and the GPT-3 API.

Save the below code in a python file called main.py

from fastapi import FastAPI  
from twilio.rest import Client  
import openai

\# Set your OpenAI API key  
openai.api\_key = “your\_api\_key”

app = FastAPI()

@app.get(“/”)  
def send\_message():  
\# Your Twilio account SID and auth token, which can be found in your Twilio account settings  
account\_sid = “ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”  
auth\_token = “your\_auth\_token”

\# Your Twilio phone number  
from\_number = “+15551234567”

\# The phone number you want to send the message to  
to\_number = “+15559876543”

\# Create a Twilio client  
client = Client(account\_sid, auth\_token)

\# The prompt or context for the text you want to generate with GPT-3  
prompt = “Hello there! How can I assist you today?”

\# Generate the response text using GPT-3  
response = openai.Completion.create(  
engine=”text-davinci-002″,  
prompt=prompt,  
max\_tokens=1024,  
n=1,  
temperature=0.5  
)

\# Get the generated response text  
response\_text = response\[“choices”\]\[0\]\[“text”\]

\# Send the message using Twilio  
message = client.messages.create(  
body=response\_text,  
from\_=from\_number,  
to=to\_number  
)

return {“message”: “Message sent successfully!”}

Make sure to add your openai API key, your twilio SID, token and from phone number at the appropriate place.

Once done, you can run this from the command line: uvicorn main:app –reload

Make sure to set your server URL in the Twilio WhatsApp sandbox.

Now you are ready to receive and respond to WhatsApp messages in your chatbot.

Conclusion
----------

In this step-by-step guide, you saw how to use Twilio and GPT-3 API to respond to WhatsApp messages automatically in a human-like way. Try it out and see how it works out.
