[ad_1]
OpenAI has recently released a range of remarkable improvements that have surprised users. Among these additions, the OpenAI function calling feature emerged as the most prominent addition. With this powerful feature, developers gain the ability to select and call to solve specific problems. A key aspect of OpenAI’s function calling functionality is its seamless integration with external APIs, allowing developers to generate responses using external services.
In our previous blogs, we explored “How to use OpenAI feature call to build an appointment booking chatbot”. Through this research, we created a chatbot that integrates with the Google Calendar API. This integration allows the chatbot to efficiently handle meeting bookings and utilize the full functionality of Google Calendar.
In this blog, we will discuss how to create an appointment booking chatbot that integrates with the GoHighLevel platform for meeting management. GoHighLevel is a customer relationship management (CRM) platform that offers a variety of features for businesses to manage their customer interactions, marketing campaigns, sales processes, and more. One of the features provided by GoHighLevel is the calendar function. The GoHighLevel calendar allows users to schedule and manage appointments and delete appointments.
Let’s start with a blog where we’ll explore how to create an appointment booking chatbot that seamlessly integrates with the GoHighLevel (GHL) platform.
Step 1:
We’ll start by setting up the GoHighLevel platform. First you need to go to https://app.gohighlevel.com/ website and then request our free 14-day trial. You must specify your company name, name, e-mail address. Mail ID, phone number and credit card details to register for the first time.
After you create an account, you will see a dashboard like below:
Step 2:
We need to use GoHighLevel API to manage appointment creation, update and deletion. To use the API, we need an API key.
There are 2 types of API keys:
- Agency API Key – Used to manage agency-level objects such as subaccounts and users.
- Location API Key – used to manage all objects that are part of sub-accounts (Contacts, Appointments, Opportunities, etc.)
We need a location API key to manage meetings. To create a location API key, you must first add a location by creating a subaccount.
To create a sub-account, you must first click on “Sub-Accounts” from the left panel and then click on “Create Sub-Account” as shown below:
Step 3:
It will open a screen like below where you have to select “Blank Snapshot” under the heading “Regular Account”.
Then it will open a screen with map as below. You need to select your location and then proceed to the selected location by clicking the arrow.
After that, it will open a tab titled “Add account”. You need to add your details and then click on the ‘Save’ button. It will create a subaccount with the given location.
Step 4:
Next, you need to switch to the newly created sub-account from the left panel “Click here to switch” as shown in the image below:
A selection box will open, listing all subaccounts. You can select one from the list and the system will switch to that account. And then you can see the screen as below:
Step 5:
After creating the sub-account, you can retrieve the location API key that will be used to manage the meeting. To get a location API key, first go to the ‘Settings’ option on the left panel and then select ‘Business Profile’ from the available options. A screen will appear where you need to scroll down a bit and find your location API key as shown in the image below:
Step 6:
As we move forward, we need to add employees to our team. To add an employee, you must complete the following steps:
- First, you need to go to Settings and then select the “My Stuff” option from all the available options.
- On the screen that appears, find and click the “Add Employee” button.
- Provide the required personal data, including first name, last name, email, password and phone number.
- Next, set the “User Roles” for the “User” employee.
- Finally, click the “Save” button to save the employee information.
Step 7:
Now, let’s move on to creating a group to help manage the team. To create a group, you need to perform the following steps:
- Start by selecting the “Calendars” option from the left panel.
- On the right side of the screen, find and click the “Create Group” button.
- It will open a form to create a group. In the form you need to provide the following details: group name, group description and calendar URL.
- Once you have filled in the required information, submit the form.
In the Calendar URL, you can simply supply any string like “demo-calendar”.
Step 8:
Now let’s go ahead and create a calendar that will support the appointment management functions. Follow the steps below to create a calendar:
- From the same screen, select the “Create Calendar” option.
- This action will open a list of options. Select “Simple Calendar” from the list.
- A form will appear in which you have to fill all the necessary details.
- After filling the required details, click on the “Finish” button.
For this demo, we kept the “Encounter Slot Setting” as follows:
Step 9:
After you create a calendar, the next step is to move it to the group you created earlier. To achieve this, follow the steps below:
- Select “Calendars” from the settings.
- Find the previously created calendar on the screen.
- Click on the three dots symbol located on the right side of the calendar.
- This action will open a selection box as shown below.
- Select the option “Go to group” from the selection box.
- A pop-up window will appear allowing you to select the desired group for the calendar and press the “Select” button.
Step 10:
We have completed the basic setup of “GoHighLevel”. We are now fully ready to use the GoHighLevel API to create, update and delete an appointment. To use the GoHighLevel API, we need three types of IDs: Group ID, Calendar ID, and User ID. We will obtain these IDs in the following steps.
To get a group ID, you need to complete the following steps:
- Select “Calendars” from the settings.
- On the screen, you will see the tab “For groups”, in which you will find the previously created group.
- Click on the three dots symbol located to the right of the group information.
- This action will open a selection box as shown below.
- Click “Copy Embed Code” from the selection box.
Your copied embed code will look like this:
You can find the ID field from the above URL. The string before the “_” symbol represents your group ID.
Step 11:
Taking this step further, we get our Calendar ID.
To get a Calendar ID, you must complete the following steps:
- Select “Calendars” from the settings.
- On the screen, you will see a tab for “Calendars”, where you will find the previously created calendar.
- Click on the three dots symbol located to the right of the calendar information.
- This action will open a selection box as shown below.
- Click “Copy Embed Code” from the selection box.
Your copied embed code will look like below as before:
Step 12:
At this point, let’s proceed to get the user ID.
To get a user ID, you need to complete the following steps:
After completing these steps, we have all three IDs, now we will move on to encrypting the appointment booking chatbot using Python.
Step 13:
Now it’s time to start by developing a Python script for an appointment booking chatbot. To achieve this, we will use OpenAI’s feature calling feature, which integrates with the GHL calendar for efficient meeting management.
First, we import the necessary libraries:
import requests
import json
from datetime import date, datetime, timedelta
import time
import pytz
Step 14:
Next, we’ll define a utility function that will call ChatGPT and generate responses. To add this feature to our script, add the following lines of code:
GPT_MODEL = "gpt-3.5-turbo-0613"
openai_api_key = "<your_openai_key>"
def chat_completion_request(messages, functions=None, function_call=None, model=GPT_MODEL):
headers =
"Content-Type": "application/json",
"Authorization": "Bearer " + openai_api_key,
json_data = "model": model, "messages": messages
if functions is not None:
json_data.update("functions": functions)
if function_call is not None:
json_data.update("function_call": function_call)
try:
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=json_data,
)
return response
except Exception as e:
print("Unable to generate ChatCompletion response")
print(f"Exception: e")
return e
Step 15:
Moving forward, in this step, we’ll define a function that calls the GHL appointment booking endpoint. Before that, however, we need to create another function that converts the user-supplied date and time to the ISO 8601 date format. The ISO 8601 format is an internationally recognized standard for representing dates and times.
We can define both functions as follows:
limit1 = datetime.strptime("10:00:00", "%H:%M:%S").time()
limit2 = datetime.strptime("17:00:00", "%H:%M:%S").time()
limit3 = datetime.strptime("12:00:00", "%H:%M:%S").time()
headers =
'Authorization': 'Bearer <Your_Location_API_key>'
def convert_to_iso8601(datetime_string, target_timezone):
datetime_format = "%Y-%m-%d %H:%M:%S"
dt = datetime.strptime(datetime_string, datetime_format)
source_timezone = pytz.timezone('Asia/Kolkata')
dt = source_timezone.localize(dt)
target_timezone = pytz.timezone(target_timezone)
dt = dt.astimezone(target_timezone)
iso8601_datetime = dt.strftime("%Y-%m-%dT%H:%M:%S%z")
iso8601_datetime = iso8601_datetime[:-2] + ":" + iso8601_datetime[-2:]
return iso8601_datetime
def appointment_booking(arguments):
try:
provided_date = datetime.strptime(json.loads(arguments)['date'], "%Y-%m-%d")
provided_time = datetime.strptime(json.loads(arguments)['time'].replace("PM","").replace("AM","").strip(), "%H:%M:%S").time()
try:
email_address = json.loads(arguments)['email_address']
except:
return "Please provide email ID for identification."
try:
phone_number = json.loads(arguments)['phone_number']
except:
return "Please provide a phone number for identification."
if provided_date and provided_time and email_address and phone_number:
start_date_time = str(provided_date.date()) + " " + str(provided_time)
iso8601_datetime = convert_to_iso8601(start_date_time, 'Asia/Kolkata')
if day_list[provided_date.weekday()] == "Saturday":
if provided_time >= limit1 and provided_time <= limit3:
url = "https://rest.gohighlevel.com/v1/appointments/"
payload =
"calendarId": "kK8LwFPuNByksXB3h18s",
"selectedTimezone": "Asia/Calcutta",
"selectedSlot": iso8601_datetime,
"email": email_address,
"phone": phone_number
response = requests.request("POST", url, headers=headers, data=payload)
response = json.loads(response.text)
try:
if response['id']:
return "Appointment booked successfully."
except:
return response['selectedSlot']['message']
else:
return "Please try to book an appointment into working hours, which is 10 AM to 2 PM at saturday."
else:
if provided_time >= limit1 and provided_time <= limit2:
url = "https://rest.gohighlevel.com/v1/appointments/"
payload =
"calendarId": "kK8LwFPuNByksXB3h18s",
"selectedTimezone": "Asia/Calcutta",
"selectedSlot": iso8601_datetime,
"email": email_address,
"phone": phone_number
response = requests.request("POST", url, headers=headers, data=payload)
response = json.loads(response.text)
try:
if response['id']:
return "Appointment booked successfully."
except:
return response['selectedSlot']['message']
else:
return "Please try to book an appointment into working hours, which is 10 AM to 7 PM."
else:
return "Please provide all the necessary information: Appointment date, time, email ID, Phone number."
except:
return "We are facing an error while processing your request. Please try again."
Step 16:
Now, let’s define a function that updates appointments using the GHL endpoint. To update the meeting, we need to find the corresponding “ID” of the meeting. Therefore, we first describe a function that obtains an “ID” based on user-supplied parameters. This function will then return the ‘ID’ to the update function.
We can define both functions as follows:
def get_all_booked_appointment(arguments):
try:
provided_date = datetime.strptime(json.loads(arguments)['date'], "%Y-%m-%d")
ending_date_time = datetime.strptime(json.loads(arguments)['date'], "%Y-%m-%d") + timedelta(days=1)
try:
email_address = json.loads(arguments)['email_address']
except:
return "Please provide email ID for identification."
if provided_date and email_address:
starting_timestamp = time.mktime(provided_date.timetuple()) * 1000
ending_timestamp = time.mktime(ending_date_time.timetuple()) * 1000
url = f"https://rest.gohighlevel.com/v1/appointments/?startDate=starting_timestamp&endDate=ending_timestamp&userId=oJbRc7r2HBYunuvJ3XC7&calendarId=kK8LwFPuNByksXB3h18s&teamId=ONZc2nxIeE6a2liSwqmNX&includeAll=true"
payload=
response = requests.request("GET", url, headers=headers, data=payload)
response = json.loads(response.text)
events = []
for element in response['appointments']:
if element['contact']['email'] == email_address:
events.append(element)
if len(events) == 1:
id = events[0]['id']
return id
elif len(events) > 1:
print("You have multiple appointments with the same email address:")
count = 1
for ele in events:
print(str(count)+"]")
print(ele['address'])
print(ele['startTime'])
print(ele['endTime'])
print(ele['contact']['email'])
print()
count = count + 1
event_number = int(input("Please enter which appointment:"))
if event_number >= 1 and event_number <= len(events):
id = events[event_number - 1]['id']
return id
else:
return "Please select valid event number"
else:
return "No registered event found with this email ID."
else:
return "Please provide all the necessary information: Appointment date and email ID."
except:
return "We are facing an error while processing your request. Please try again."
def appointment_updation(arguments):
try:
provided_date = datetime.strptime(json.loads(arguments)['to_date'], "%Y-%m-%d")
provided_time = datetime.strptime(json.loads(arguments)['time'].replace("PM","").replace("AM","").strip(), "%H:%M:%S").time()
if provided_date and provided_time and json.loads(arguments)['date'] and json.loads(arguments)['email_address']:
start_date_time = str(provided_date.date()) + " " + str(provided_time)
iso8601_datetime = convert_to_iso8601(start_date_time, 'Asia/Kolkata')
if day_list[provided_date.date().weekday()] == "Saturday":
if provided_time >= limit1 and provided_time <= limit3:
id = get_all_booked_appointment(arguments)
if id == "Please select valid event number" or id == "No registered event found with this email ID." or id == "We are facing an error while processing your request. Please try again.":
return id
else:
url = f"https://rest.gohighlevel.com/v1/appointments/id"
payload =
"selectedTimezone": "Asia/Calcutta",
"selectedSlot": iso8601_datetime
response = requests.request("PUT", url, headers=headers, data=payload)
response = json.loads(response.text)
try:
if response['id']:
return "Appointment updated successfully."
except:
return response['selectedSlot']['message']
else:
return "Please try to book an appointment into working hours, which is 10 AM to 2 PM at saturday."
else:
if provided_time >= limit1 and provided_time <= limit2:
id = get_all_booked_appointment(arguments)
if id == "Please select valid event number" or id == "No registered event found with this email ID." or id == "We are facing an error while processing your request. Please try again.":
return id
else:
url = f"https://rest.gohighlevel.com/v1/appointments/id"
payload =
"selectedTimezone": "Asia/Calcutta",
"selectedSlot": iso8601_datetime
response = requests.request("PUT", url, headers=headers, data=payload)
response = json.loads(response.text)
try:
if response['id']:
return "Appointment updated successfully."
except:
return response['selectedSlot']['message']
else:
return "Please try to book an appointment into working hours, which is 10 AM to 7 PM."
else:
return "Please provide all the necessary information: Current appointment date, New appointment date, time and email ID."
except:
return "We are facing an error while processing your request. Please try again."
Step 17:
Next, we define the function to delete meetings. This function will first call the ‘get_all_booked_appointments’ function we created earlier to get the corresponding appointment ID. The resulting ID is then passed to the GHL deletion endpoint.
You need to add the following lines of code to define the delete function:
def appointment_deletion(arguments):
try:
id = get_all_booked_appointment(arguments)
if id == "Please select valid event number" or id == "No registered event found with this email ID." or id == "We are facing an error while processing your request. Please try again.":
return id
else:
url = f"https://rest.gohighlevel.com/v1/appointments/id"
payload=
response = requests.request("DELETE", url, headers=headers, data=payload)
if response.text == "OK":
return "Appointment deleted successfully."
except:
return "We are facing an error while processing your request. Please try again."
Step 18:
Now we need to define the function specifications for creating, updating and deleting an appointment. These function specifications are passed to ChatGPT, allowing it to determine which function to call based on the user argument.
functions = [
"name": "appointment_booking",
"description": "When user want to book appointment, then this function should be called.",
"parameters":
"type": "object",
"properties":
"date":
"type": "string",
"format": "date",
"example":"2023-07-23",
"description": "Date, when the user wants to book an appointment. The date must be in the format of YYYY-MM-DD.",
,
"time":
"type": "string",
"example": "20:12:45",
"description": "time, on which user wants to book an appointment on a specified date. Time must be in %H:%M:%S format.",
,
"email_address":
"type": "string",
"description": "email_address of the user gives for identification.",
,
"phone_number":
"type" : "string",
"description": "Phone number given by user for identification."
,
"required": ["date","time","email_address","phone_number"],
,
,
"name": "appointment_updation",
"description": "When user want to reschedule appointment, then this function should be called.",
"parameters":
"type": "object",
"properties":
"to_date":
"type": "string",
"format": "date",
"example":"2023-07-23",
"description": "It is the date on which the user wants to reschedule the appointment. The date must be in the format of YYYY-MM-DD.",
,
"date":
"type": "string",
"format": "date",
"example":"2023-07-23",
"description": "It is the date from which the user wants to reschedule his/her appointment. The date must be in the format of YYYY-MM-DD.",
,
"time":
"type": "string",
"example":"4:00:00",
"description": "It is the time on which the user wants to reschedule an appointment. Time must be in %H:%M:%S format.",
,
"email_address":
"type": "string",
"description": "email_address of the user gives for identification.",
,
"required": ["date","to_date","time","email_address"],
,
,
"name": "appointment_deletion",
"description": "When user want to delete appointment, then this function should be called.",
"parameters":
"type": "object",
"properties":
"date":
"type": "string",
"format": "date",
"example":"2023-07-23",
"description": "Date, on which user has an appointment and wants to delete it. The date must be in the format of YYYY-MM-DD.",
,
"email_address":
"type": "string",
"description": "email_address of the user gives for identification.",
,
"required": ["date","email_address"],
,
]
Step 19:
Now we are ready to test the appointment booking chatbot. You just need to add the following lines of code to run the chatbot.
day_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
messages = ["role": "system", "content": f"""You are an expert in booking appointments, who can fulfill appointment scheduling, rescheduling, and deletion very efficiently. You need to remember the below guidelines while processing user requests.
Guidelines:
- You will ask for the appointment date and time, phone number, and email address, when the user wants to book an appointment.
- When the user wants to reschedule an appointment, then you will ask for the current appointment date, new date and time for the appointment, and email address. If the user didn't remember the current appointment details then inform the user that rescheduling will not be possible without these details.
- You will ask email address every time, as it is a must for user identification.
- Don't make assumptions about what values to plug into functions, if the user does not provide any of the required parameters then you must need to ask for clarification.
- If a user request is ambiguous, you also need to ask for clarification.
- If a user didn't specify "ante meridiem (AM)" or "post meridiem (PM)" while providing the time, then you must have to ask for clarification. If the user didn't provide day, month, and year while giving the time then you must have to ask for clarification.
You must need to satisfy the above guidelines while processing the request. You need to remember that today's date is date.today()."""]
user_input = input("Please enter your question here: (if you want to exit then write 'exit' or 'bye'.) ")
while user_input.strip().lower() != "exit" and user_input.strip().lower() != "bye":
messages.append("role": "user", "content": user_input)
# calling chat_completion_request to call ChatGPT completion endpoint
chat_response = chat_completion_request(
messages, functions=functions
)
# fetch response of ChatGPT and call the function
assistant_message = chat_response.json()["choices"][0]["message"]
if assistant_message['content']:
print("Response is: ", assistant_message['content'])
messages.append("role": "assistant", "content": assistant_message['content'])
else:
fn_name = assistant_message["function_call"]["name"]
arguments = assistant_message["function_call"]["arguments"]
function = locals()[fn_name]
result = function(arguments)
print("Response is: ", result)
user_input = input("Please enter your question here: ")
testing
Book an appointment
You can view the booked appointments by clicking on the “Calendars” option in the main menu as shown below:
Appointment renewal:
Delete appointment:
In this blog, we’ve explored the process of creating an appointment booking chatbot using OpenAI’s function call function. We learned how to integrate the GoHighLevel calendar API, which will allow our chatbot to seamlessly connect to the calendar system. This integration allows users to book appointments by talking to a chat bot.
[ad_2]
Source link