Skip to content

Instantly share code, notes, and snippets.

@skolo-online
Created January 3, 2023 17:32
Show Gist options
  • Save skolo-online/f25350c0c621df2529ebaf3755a90b90 to your computer and use it in GitHub Desktop.
Save skolo-online/f25350c0c621df2529ebaf3755a90b90 to your computer and use it in GitHub Desktop.
WhatsApp Function Handle WhatsApp Chat
from .models import *
from django.contrib.auth.models import User
def handleWhatsAppChat(fromId, profileName, phoneId, text):
##Check if there is a chat session
try:
chat = ChatSession.objects.get(profile__phoneNumber=fromId)
except:
##Check that this use does already exist
if User.objects.filter(username=phoneId).exists():
user = User.objects.get(username=phoneId)
user_profile = user.profile
else:
##Create a User
user = User.objects.create_user(
username=phoneId,
email='tester1@skhokho.tech',
password='password',
first_name=profileName)
##Creating a profile
user_profile = Profile.objects.create(
user=user,
phoneNumber=fromId,
phoneId=phoneId)
#Create a chat session
chat = ChatSession.objects.create(profile=user_profile)
##Send a message
message = "Welcome to the AI Business Plan creator 😀 \n I am going to take you through the process of creating your business plan, right here on WhatsApp. To get started, Enter your Business Name: "
sendWhastAppMessage(fromId, message)
return ''
##When you return
####Continue with function
if chat.business_name:
if chat.business_type:
if chat.country:
if chat.product_service:
if chat.short_description:
if chat.years:
if chat.progress:
###Do something else
message = "👩‍💻 Our AI is working on it 👩‍💻 Give us a moment, we will message you when your business plan is ready."
sendWhastAppMessage(fromId, message)
##Run in background
loop.run_in_executor(None, createNewBusinessPlan, chat)
return ''
else:
chat.progress = text
chat.save()
message = "😀 Great we have everything we need to build your Business Plan. Enter Anything to Continue . . . . "
sendWhastAppMessage(fromId, message)
return ''
else:
try:
years = int(text.replace(' ',''))
chat.years = years
chat.save()
message = "How much traction have you made in your business?"
sendWhastAppMessage(fromId, message)
return ''
except:
message = "Please try again, enter only a number like 1 or 2"
sendWhastAppMessage(fromId, message)
return ''
else:
chat.short_description = text
chat.save()
message="How many years have you been in business for? Enter a number like 1 or 2."
sendWhastAppMessage(fromId, message)
return ''
else:
chat.product_service = text
chat.save()
message="Describe your business idea in one or two sentences."
sendWhastAppMessage(fromId, message)
return ''
else:
chat.country = text
chat.save()
##Send next message
message="What product or service will your business be providing?"
sendWhastAppMessage(fromId, message)
return ''
else:
#Test for the number
try:
type = int(text.replace(' ',''))
if type == 1:
chat.business_type = '(Pty) Ltd'
chat.save()
message="Which country are you from?"
sendWhastAppMessage(fromId, message)
return ''
elif type == 2:
chat.business_type = 'Not Profit'
chat.save()
message="Which country are you from?"
sendWhastAppMessage(fromId, message)
return ''
elif type == 3:
chat.business_type = 'Partnership'
chat.save()
message="Which country are you from?"
sendWhastAppMessage(fromId, message)
return ''
else:
message="Please try again. Enter the number correspondig to the Business Type: \n 1. (Pty) Ltd \n 2. Not Profit \n 3. Partnership \n \n Enter just the number."
sendWhastAppMessage(fromId, message)
return ''
except:
message="Please try again. Enter the number correspondig to the Business Type: \n 1. (Pty) Ltd \n 2. Not Profit \n 3. Partnership \n \n Enter just the number."
sendWhastAppMessage(fromId, message)
return ''
else:
chat.business_name = text
chat.save()
##Send next message
message="Please select the type of business. Enter the number correspondig to the Business Type: \n 1. (Pty) Ltd \n 2. Not Profit \n 3. Partnership \n \n Enter just the number."
sendWhastAppMessage(fromId, message)
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment