-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (119 loc) · 4.75 KB
/
app.py
File metadata and controls
140 lines (119 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Flask, render_template, jsonify, request
import json
from typing import Any, Dict, List
app = Flask(__name__)
@app.get("/")
def handle_get():
return render_template("messenger.html")
def format_response_for_dialogflow(messages: List[Dict[str, Any]], session_info: Dict[str, Any] = None) -> Dict[str, Any]:
response_data = {
"fulfillmentResponse": {
"messages": messages,
"mergeBehavior": "MERGE_BEHAVIOR_UNSPECIFIED"
}
}
if session_info:
response_data["sessionInfo"] = session_info
return response_data
def create_text_message(text: str) -> Dict[str, Any]:
return {
"text": {
"text": [text],
"allowPlaybackInterruption": False
}
}
def create_chip_message(options: List[str]) -> Dict[str, Any]:
return {
"payload": {
"richContent": [
[
{
"type": "chips",
"options": [{"text": opt} for opt in options]
}
]
]
}
}
# Intent Handlers
def handle_default_welcome_intent():
return format_response_for_dialogflow([
create_text_message("Good day! Let’s take a step toward better health. How can I assist you?"),
create_chip_message([
"Book appointment",
"Reschedule appointment",
"Cancel appointment",
"Check appointment Status",
"Connect to human agent"
])
])
def handle_book_appointment(body: Dict[str, Any]):
return format_response_for_dialogflow([
create_text_message("Sure, I can help with booking an appointment.")
])
def handle_cancel_appointment(body: Dict[str, Any]):
session_info = body.get("sessionInfo", {})
appointment_id = session_info.get("parameters", {}).get("appointment_id")
if appointment_id:
message = f"Your appointment with ID {appointment_id} has been successfully canceled."
else:
message = "To cancel your appointment, please provide the appointment ID."
return format_response_for_dialogflow([
create_text_message(message)
], session_info)
def handle_reschedule_appointment(body: Dict[str, Any]):
session_info = body.get("sessionInfo", {})
appointment_id = session_info.get("parameters", {}).get("appointment_id")
new_date = session_info.get("parameters", {}).get("new_date")
if appointment_id and new_date:
message = f"Your appointment {appointment_id} has been rescheduled to {new_date}."
else:
message = "Please provide the appointment ID and the new preferred time to proceed with rescheduling."
return format_response_for_dialogflow([
create_text_message(message)
], session_info)
def handle_check_status(body: Dict[str, Any]):
session_info = body.get("sessionInfo", {})
appointment_id = session_info.get("parameters", {}).get("appointment_id")
if appointment_id:
# Mock status
message = f"Your appointment {appointment_id} is confirmed for tomorrow at 10 AM."
else:
message = "To check the status, please provide your appointment ID."
return format_response_for_dialogflow([
create_text_message(message)
], session_info)
def handle_connect_agent(body: Dict[str, Any]):
return format_response_for_dialogflow([
create_text_message("All our agents are currently busy. An agent will join shortly.") # this can be modified as per the live agent integration method as stated in the business requirement
])
def handle_confirmation(body: Dict[str, Any]):
return format_response_for_dialogflow([
create_text_message("Thank you! Your booking is confirmed and your booking details will be sent to your phone number via text message.")
])
# Webhook dispatcher
@app.post("/")
def webhook_entry():
body = request.get_json()
tag = body.get("fulfillmentInfo", {}).get("tag")
print("Triggered tag:", tag)
if tag == "defaultWelcomeIntent":
return jsonify(handle_default_welcome_intent())
elif tag == "bookAppointment":
return jsonify(handle_book_appointment(body))
elif tag == "cancelAppointment":
return jsonify(handle_cancel_appointment(body))
elif tag == "rescheduleAppointment":
return jsonify(handle_reschedule_appointment(body))
elif tag == "checkStatus":
return jsonify(handle_check_status(body))
elif tag == "connectAgent":
return jsonify(handle_connect_agent(body))
elif tag == "confirm":
return jsonify(handle_confirmation(body))
else:
return jsonify(format_response_for_dialogflow([
create_text_message(f"No handler implemented for tag: {tag}")
]))
if __name__ == "__main__":
app.run(debug=True)