WhatsApp App to Generate a 4 Page Business Plan using GPT-3 API, Python Django and Meta Api

Skolo Online Learning
Python in Plain English
7 min readJan 10, 2023

--

Happy new year and a prosperous 2023. Towards the end of last year, I completed a tutorial series that combines the power of Meta WhatsApp API, GPT-3 and Python Django. You can find the full youtube playlist here:

I created a WhatsApp interface — Django powered app that can generate a full business plan PDF document for a client, from 5–7 data points or responses that the client provides, using AI to beef up the business plan contents.

Note that, I used a business plan document as an example, this architecture can be used to generate any type of document. They important points to mention are:

  • The ability for the user to completely interface with the application on WhatsApp — which allows your app to be more accesible.
  • The AI engine that takes a few prompts and generates human readable and complex content.
  • Generating a PDF document on Django, saving it and sending the link to the user via WhatsApp.

Tech Stack — WhatsApp GPT-3 Business Plan Generator application

To get started, you will need a virtual server, you can get from Digital Ocean using my referral link here: https://m.do.co/c/7d9a2c75356d

In addition you will need a facebook account in order to create a developer account that will enable you to add a new app with WhatsApp access here: https://developers.facebook.com/

You will also need an OpenAI API access key that you can get here: https://openai.com/api/

In the YouTube video tutorial series, I cover everything in great detail — here I will post the highlights of the project.

WhatsApp API — Send a receive messages via Django

As a start you will need a working Django application, that has been deployed to a https server (this is required so you can receive the web-hooks from Meta API and save the PDF document you create in a location the user can access).

Once you create an app in facebook developers page — you will get a test phone number and temporary access token that you can use.

The WhatsApp developer page will look like this:

WhatsApp Developer API Cloud API

Step 1: Make sure the correct app is selected, in case you have multiple apps.

Step 2: Add WhatsApp permissions and go to the “Getting Started” tab

Step 3: You will receive a temporary access token, valid for 24hrs. To get a permanent one, you need to enter your own phone number.

Step 4: This is your temporary test number, if you scroll down the page you will see example code to get started sending a message.

Sending a WhatsApp Message with Python

Sending a message is straight forward, all you need is the phone number and a requests POST to the API endpoint.

WhatsApp only allows you to send messages to numbers that have sent you a message first. So send a message from your phone to the test number you were given before you can get started.

In a real life scenario — users will have your application/business number, and send you the first message to get started with the process.

Send a WhatsApp message

This number on the WhatsApp url — 110244361711861:

'https://graph.facebook.com/v15.0/110244361711861/messages'

is a number unique to your app and phone number, replace it with your number from the developer console.

At the time of reading this article — use the latest API version, we are now at v15.0 but that will be different when you read this. You can get both these details from the screenshot page above, if you scroll down to the example code.

Receive a WhatsApp message with Python Django

This is a little bit more complicated, in short you will need to

(1) Create the Django app,

(2) Create a url and view function for the web-hook,

(3) enable the web-hook from the Facebook side,

(4) make sure the facebook app is live — there is a toggle button at the top of the page,

(5) add a privacy page to your facebook developer app and

(6) deploy your application to secure https.

Once you have done that — — your view function for the web-hook will look like so:

There are a couple of things you need to get right for this to work:

  • The verify token in the code, must match the verify token you provide Facebook. You will need this to work first, to test the web-hook.
  • You must always send back a HTTP response code to Facebook on both GET and POST requests.
  • Your Facebook app needs to be in “live” mode.

Now that we can send and receive messages, we need to know what questions to ask to user. So we developed a model to manage the chat and collect responses from the user.

Chat Intent Model Class — Python Django

Note: Before creating the chat-session model, you will need a profile model that is used to identify the user. We will not have login and registrations in this app, but it is still a good idea to identify the users in your application — so you can map the data created to a user.

A profile model is simple — I decided to use the phone number as the main unique variable, WhatsApp will ensure that there are never two users with the same number, and if I get a message from a number, I know it can only be that specific user.

So we leverage off the security and authentication that comes with WhatsApp to ensure we have verified users, and we do not duplicate accounts. We do not even have to ask a user to provide their number — the message they send, comes with their phone number, and WhatsApp ID — both unique variables.

The chat model:

The aim of this model — is to store responses obtained from the user, in our chat conversation.

At the end of the chat, we use these responses to generate the business plan and delete the chat session instance.

Handle the Chat Conversation

Let us look at the handleWhatsAppChat function that we first saw in the web-hook view function.

This function has to be versatile enough to handle the following:

  • Always know where in the conversation we are, what responses have already been provided and what is still outstanding.
  • Collect the appropriate response at each point.
  • Handle multiple choice selection (similar to drop down in a form-select).
  • Give feedback to the client at every step, like a free-flowing conversation.
  • Responses to be stored in the database, as they are collected.

Note that this is not a chatbot. It is a carefully balanced fixed function that is aimed at collecting and storing the required responses from the user.

The chat function looks like this:

A few things to note:

Send WhatsApp Message — this is the same function we have at the beginning of this article.

User and Profile models — Not shown in this article, but you can figure it our from the code here. First we check if a user exists with that phone number, or else we create a new user. The user and profile are related on a OneToOne relationship.

Chat Session — Only one can exist per user/profile at a given time, because we delete them at the end of the conversation. If a session exists, we continue with it or else we create a new one.

Build the business plan document

This function is the heart of our application. After collecting all the responses from the user, we build the business plan in two steps:

  1. Call the OpenAI API for each section of the business plan and save that information to the database.
  2. Fetch the saved information and build a PDF document — add the information to the document, save the document and return the location url to send to the user. Note we will be saving the document in the “media” folder I decided to call “uploads”. Therefore, we need to make sure we serve this folder on our https server, so the user can access it.

The functions will look like so:

Build Business Plan with GPT-3 AI

Note the following:

You will need a HTML for the PDF document, it’s a simple template that displays the data sent through the context. The CSS must be minimal and included in the template to simplify things.

This import at the top:

from .aifile import *

Means — we have a separate file to call the GPT-3 API. An example of the GPT-3 prompt is:

import os
import openai
from django.conf import settings
openai.api_key = settings.OPENAI_API_KEY

def companyDescriptionProgress(business_name, business_type, country, product_service, short_description, years, progress):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Generate Company Description section for a Business Plan for the following business, using the guidelines provided:\nBusiness Name: {}\nBusiness Type: {}\nCountry: {}\nProduct or Service: {}\nShort Business Description: {}\nYears in operation: {}\nBusiness progress to date: {}\n\nGuidelines: Start the company description by listing the business name and company structure, if one is provided. Write a detailed business description for the short description provided, in a professional business tone. Describe the industry the business will be operating in and re-write the business progress to date. Finally, provide a numbered list of three suitable business objectives for this business and for each objective, describe how the objective fits the business and how it will benefit the stakeholders in the long run. \n\nCompany Description\n{}".format(business_name, business_type, country, product_service, short_description, years, progress, business_name),
temperature=0.7,
max_tokens=2000,
top_p=1,
best_of=2,
frequency_penalty=0,
presence_penalty=0)

if 'choices' in response:
if len(response['choices'])>0:
answer = response['choices'][0]['text'].replace('\n', '<br>')
return business_name + answer
else:
return ''
else:
return ''

You are now ready to run and test your function, of course I left out a lot of detail — there is only so much that you can write in a medium article. It took hours to complete this project with 5 videos on Youtube that you can watch in more detail.

Conclusion

What are your thoughts on building a total web-app free application, using Django to power a WhatApp application?

WhatsApp Django Application

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Build awareness and adoption for your tech startup with Circuit.

--

--

Founder of Tati Digital, passionate about digital transformation and ushering businesses in to the digital age and 4th industrial revolution.