Build Your Own WhatsApp - ChatGPT (OpenAI) With Flask

Published on
790 words
4 mins read
--- views
Authors

ChatGPT is an AI Chatbot developed by OpenAI. It has been buzzing around the technology world. If you are someone who has been using ChatGPT for a while and wants to integrate it to WhatsApp, then this blog is only for you. Here we are using OpenAI API, WhatsApp API, and uses Flask for Creating a webhook. Looks interesting na? So Let's get Botified.

OpenAI Developer

You need OpenAI API key to use OpenAI API and SDK. We are using this to send our messages to OpenAI thus we'll get the appropriate response from OpenAI based on the request. You can sign up for a free OpenAI account here.

On Successful signup, you need to create a API Key. To do this visit the API Keys page. Then click on Create new secret key and give any name.

You need to paste this key into your .env file. So For now, Just note down the API KEY.


Facebook Developer

You can sign up for a free facebook developer account here.

Once developer account setup is completed, You're required to create a new app.

  1. On dashboard, Click on Create App button.
App Dashboard

  1. Initially, It'll ask you to select the purpose of the app. Each option has some predefined configurations inside the app. If you don't know what to select, Select Others.

App Purpose

  1. Then, you'll need to select the type of the App. Here select Business which is more related to our requirement.

App Type

  1. After that, Type App Name, Contact Email, Business Account. These data can be anything.

App Details

  1. In App Dashboard, There you can see various type of integration. Select Setup which is in WhatsApp card.

App Integration

  1. Click on Start Using The API.

WhatsApp Setup

  1. Here you can find Temporary access token (we can also create long lived access token), Test phone number, Your phone number, and a sample WhatsApp API request.

WhatsApp API

You need to paste this Access token and Test phone number into your .env file. So For now, Just note down the Access token and Test phone number.


Since this is a developement mode, You are allowed to have 5 numbers from the test number. It'll only work from this 5 numbers. If you want to use this bot in real time people, you are required to purchase a phone number and need to verify your business.


  1. Subscribe the fields based on the requirement. Only messages field is enough for events that has to be trigged on messages. But for now, You can select all the permissions.

API Subscription

  1. Need to configure the Webhook for integrating WhatApp with our Backend code.

WhatsApp Webhook

  • Callback URL - Our backend server URL. Whenever any events occurred on the selected fields from previous step, the data with the action is sent to this URL.
  • Verify token - Any string that should match with VERIFYTOKEN in our .env file.

Running And Testing Code Locally

  • Clone the project
  git clone https://github.com/gandstech/openai-whatsapp-flask
  • Go to the project directory
  cd openai-whatsapp-flask
  • Install dependencies
  pip install -r requirements.txt
  • Copy the env.local to .env and fill OPENAI_TOKEN (Obtained from OpenAI Account), VERIFYTOKEN (Any String that has to match with Verify Token in Webhook configuration), FACEBOOK_API (Obtained from Facebook Developer Account) and TEST_PHONE_NUMBER(Obtained from Facebook Developer Account).
  cp env.local .env
  • Start the server
  flask run
  • Use ngrok or some other services to put our app in the public.
ngrok http 5000 --region us

This generate a public accessible URL that should be given as a callback URL in webhook configurations. Ex. https://88d5-103-103-9-202.ngrok-free.app/python-flask/webhook/facebook

  • From your WhatsApp, send any message to the test number obtained in facebook developer to test the chatbot.

Flow Diagram



Flow Diagram

Working Flow Of The Bot



Conclusion

To summarize, you just made your own chatgpt, integrated with whatsapp and had conversation with it.

Feel free to integrate this chatbot backend with other messaging services like slack, telegram or even in your own application. If you want me to help you to integrate with any other application, please reach out to me or leave a comment below.

Have fun with your own whatsapp chatbot!



Related Blogs