Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic survey question values #98

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions caddy_chatbot/src/caddy_core/services/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_survey(user: str) -> Tuple[List[str], List[str]]:
user (str): The email of the user

Returns:
Tuple[List[str], List[str]]: The questions for the survey and the values for the survey
List[dict[str, List[str]]]: A list of questions and values for the survey
"""
user_workspace_variables = get_user_workspace_variables(user)

Expand All @@ -37,9 +37,8 @@ def get_survey(user: str) -> Tuple[List[str], List[str]]:
]

post_call_survey_questions = post_call_module["questions"]
post_call_survey_values = post_call_module["values"]

return post_call_survey_questions, post_call_survey_values
return post_call_survey_questions


def get_user_workspace_variables(user_email: str):
Expand Down
77 changes: 41 additions & 36 deletions caddy_chatbot/src/integrations/google_chat/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,30 +249,28 @@ def handle_survey_response(self, event):

survey_response = [{question: response}]

evaluation_entry = evaluation_table.get_item(Key={"threadId": str(threadId)})

if "Item" in evaluation_entry and "surveyResponse" in evaluation_entry["Item"]:
evaluation_table.update_item(
Key={"threadId": str(threadId)},
UpdateExpression="set surveyResponse = list_append(surveyResponse, :surveyResponse)",
ExpressionAttributeValues={":surveyResponse": survey_response},
ReturnValues="UPDATED_NEW",
)
else:
evaluation_table.update_item(
Key={"threadId": str(threadId)},
UpdateExpression="set surveyResponse = :surveyResponse",
ExpressionAttributeValues={":surveyResponse": survey_response},
ReturnValues="UPDATED_NEW",
)
evaluation_table.update_item(
Key={"threadId": str(threadId)},
UpdateExpression="set surveyResponse = list_append(if_not_exists(surveyResponse, :empty_list), :surveyResponse)",
ExpressionAttributeValues={
":surveyResponse": survey_response,
":empty_list": [],
},
ReturnValues="UPDATED_NEW",
)

remaining_sections = len(card[0]["card"]["sections"])

card[0]["card"]["sections"] = [
section
for section in card[0]["card"]["sections"]
if section["widgets"][0]["textParagraph"]["text"] != question
]
for i in range(remaining_sections):
if (
card[0]["card"]["sections"][i]["widgets"][1]["textParagraph"]["text"]
== f"<b>{question}</b>"
):
del card[0]["card"]["sections"][i]
remaining_sections -= 1
break

if len(card[0]["card"]["sections"]) == 0:
if remaining_sections == 0:
card[0]["card"]["sections"].append(self.messages.SURVEY_COMPLETE_WIDGET)

self.update_survey_card_in_adviser_space(
Expand Down Expand Up @@ -443,11 +441,9 @@ def run_new_survey(self, user: str, thread_id: str, user_space: str) -> None:
Returns:
None
"""
post_call_survey_questions, post_call_survey_values = get_survey(user)
post_call_survey_questions = get_survey(user)

survey_card = self.get_post_call_survey_card(
post_call_survey_questions, post_call_survey_values
)
survey_card = self.get_post_call_survey_card(post_call_survey_questions)

self.send_dynamic_to_adviser_space(
response_type="cardsV2",
Expand All @@ -457,14 +453,14 @@ def run_new_survey(self, user: str, thread_id: str, user_space: str) -> None:
)

def get_post_call_survey_card(
self, post_call_survey_questions: List[str], post_call_survey_values: List[str]
self,
post_call_survey_questions: List[dict[str, List[str]]],
) -> dict:
"""
Create a post call survey card with the given questions and values

Args:
post_call_survey_questions (List[str]): The questions for the survey
post_call_survey_values (List[str]): The values for the survey
post_call_survey_questions (List[dict[str, List[str]]]): The questions and values for the survey

Returns:
dict: The survey card
Expand All @@ -480,14 +476,24 @@ def get_post_call_survey_card(
],
}

for question in post_call_survey_questions:
i = 0
for question_dict in post_call_survey_questions:
i += 1
question = question_dict["question"]
values = question_dict["values"]
section = {"widgets": []}

question_section = {"textParagraph": {"text": question}}
label_section = {
"decoratedText": {
"topLabel": f"Question {i}",
}
}

question_section = {"textParagraph": {"text": f"<b>{question}</b>"}}

button_section = {"buttonList": {"buttons": []}}

for value in post_call_survey_values:
for value in values:
button_section["buttonList"]["buttons"].append(
{
"text": value,
Expand All @@ -503,6 +509,7 @@ def get_post_call_survey_card(
}
)

section["widgets"].append(label_section)
section["widgets"].append(question_section)
section["widgets"].append(button_section)

Expand Down Expand Up @@ -1051,11 +1058,9 @@ def get_survey(self, user: str) -> dict:
Returns:
dict: The survey card
"""
post_call_survey_questions, post_call_survey_values = get_survey(user)
post_call_survey_questions = get_survey(user)

survey_card = self.get_post_call_survey_card(
post_call_survey_questions, post_call_survey_values
)
survey_card = self.get_post_call_survey_card(post_call_survey_questions)

return survey_card

Expand Down