Skip to content

Commit

Permalink
Merge pull request #5 from GenesysCloudBlueprints/cards-and-carousels…
Browse files Browse the repository at this point in the history
…-support

Adds handling for cards/carousels support
  • Loading branch information
crcgenesys authored Nov 11, 2024
2 parents 434dd52 + e95a124 commit cd4815d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

This Genesys Cloud Developer Blueprint explains how to deploy a Microsoft Power Virtual Agent (VA) bot to answer your customer queries through web messaging and Messenger. The blueprint also provides the solution for using a third-party bot that Genesys Cloud does not support as a strategic vendor. The solution uses the Genesys Bot Connector that provides the API and acts as the link between Genesys Cloud and the bot. In this solution, the bot interpreter application created using AWS Lambda functions acts in between Genesys Cloud and Microsoft Power VA. The application receives the utterances from the Messenger through Genesys Bot Connector. It changes the format of the request before sending out an HTTP request to Microsoft Power VA for Natural Language Understanding (NLU). It also converts the response message received from Microsoft Power VA to match the postUtterance API that is provided by the Genesys Bot Connector.

![Web Messaging with Genesys Bot Connector and Microsoft Power VA](blueprint/images/bot-wm-aws.png "Web Messaging with Genesys Bot Connector and Microsoft Power VA")
![Web Messaging with Genesys Bot Connector and Microsoft Power VA](blueprint/images/overview.png "Web Messaging with Genesys Bot Connector and Microsoft Power VA")

Binary file added blueprint/images/overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 29 additions & 27 deletions blueprint/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ please see our Genesys Cloud blueprint [FAQ](https://developer.genesys.cloud/blu

This Genesys Cloud Developer Blueprint explains how to deploy a Microsoft Power Virtual Agent (VA) bot to answer your customer queries through web messaging and Messenger. The blueprint also provides the solution for using a third-party bot that Genesys Cloud does not support as a strategic vendor. The solution uses the Genesys Bot Connector API that acts as the link between Genesys Cloud and the bot.

![Web Messaging with Genesys Bot Connector and Microsoft Power VA](images/bot-wm-aws.png "Web Messaging with Genesys Bot Connector and Microsoft Power VA")
![Web Messaging with Genesys Bot Connector and Microsoft Power VA](images/overview.png "Web Messaging with Genesys Bot Connector and Microsoft Power VA")

## Scenario

Genesys Cloud supports and provides the interface for selected chatbot providers such as AWS and Google. Many new chatbot vendors are fast-growing in the market. A customer wants to bring in their preferred third-party bots in the Architect message flows and populate the bot list.
Genesys Cloud supports and provides the interface for selected chatbot providers such as AWS and Google. Many new chatbot vendors are fast-growing in the market. A customer wants to bring in their preferred third-party bots in the Architect message flows and populate the bot list. This provides a sample converter that demonstrates how to translate from the Genesys Bot Connector to a Microsoft Power Virtual Agent chatbot and back.

Note that this sample converter will only work with the classic Power Virtual Agent bots and not the newer Microsoft Copilot bots as the endpoints and calling conventions have changed. However, this sample could be used as a baseline sample for interfacing with Copilot bots.

## Solution

Expand Down Expand Up @@ -79,31 +81,31 @@ The application receives the utterances from the Messenger through Genesys Bot C

## Implementation steps

- [Scenario](#scenario)
- [Solution](#solution)
- [Contents](#contents)
- [Solution components](#solution-components)
- [Prerequisites](#prerequisites)
- [Specialized knowledge](#specialized-knowledge)
- [Genesys Cloud account](#genesys-cloud-account)
- [AWS account](#aws-account)
- [Microsoft Azure account](#microsoft-azure-account)
- [Implementation steps](#implementation-steps)
- [Clone the GitHub repository](#clone-the-github-repository)
- [Create a Microsoft Power VA bot](#create-a-microsoft-power-va-bot)
- [Configure AWS services](#configure-aws-services)
- [Set up Amazon DynamoDB](#set-up-amazon-dynamodb)
- [Create a function using AWS Lambda](#create-a-function-using-aws-lambda)
- [Add an Amazon API Gateway to the Lambda function](#add-an-amazon-api-gateway-to-the-lambda-function)
- [Configure Genesys Cloud](#configure-genesys-cloud)
- [Create Genesys Bot Connector integration](#create-genesys-bot-connector-integration)
- [Create a custom role in Genesys Cloud](#create-a-custom-role-in-genesys-cloud)
- [Load the Power VA bot to the Genesys Cloud bot list](#load-the-power-va-bot-to-the-genesys-cloud-bot-list)
- [Create an Architect flow](#create-an-architect-flow)
- [Set up Web Messaging and test the bot](#set-up-web-messaging-and-test-the-bot)
- [Additional resources](#additional-resources)

### Clone the GitHub repository
- [Scenario](#scenario "Goes to the Scenario section")
- [Solution](#solution "Goes to the Solution section")
- [Contents](#contents "Goes to the contents section")
- [Solution components](#solution-components "Goes to the Solution Components section")
- [Prerequisites](#prerequisites "Goes to the Solution appropriate section")
- [Specialized knowledge](#specialized-knowledge "Goes to the Specialized knowledge section")
- [Genesys Cloud account](#genesys-cloud-account "Goes to the Genesys Cloud account section")
- [AWS account](#aws-account "Goes to the AWS account section")
- [Microsoft Azure account](#microsoft-azure-account "Goes to the Microsoft Azure account section")
- [Implementation steps](#implementation-steps "Goes to the Implementation steps section")
- [Download the repository containing the project files](#download-the-repository-containing-the-project-files "Shows how to download the repository")
- [Create a Microsoft Power VA bot](#create-a-microsoft-power-va-bot "Goes to the Create a Microsoft Power VA bot section")
- [Configure AWS services](#configure-aws-services "Goes to the Configure AWS Services section")
- [Set up Amazon DynamoDB](#set-up-amazon-dynamodb "Goes to the Setup Amazon DynamoDB section")
- [Create a function using AWS Lambda](#create-a-function-using-aws-lambda "Goes to the Create a function using AWS Lambda section")
- [Add an Amazon API Gateway to the Lambda function](#add-an-amazon-api-gateway-to-the-lambda-function "Add an Amazon API Gateway to the LAmbda function section")
- [Configure Genesys Cloud](#configure-genesys-cloud "Goes to the Configure Genesys Cloud section")
- [Create Genesys Bot Connector integration](#create-genesys-bot-connector-integration "Goes to the Create Genesys Bot Connector Integration section")
- [Create a custom role in Genesys Cloud](#create-a-custom-role-in-genesys-cloud "Goes to the Create a custom role in Genesys Cloud section")
- [Load the Power VA bot to the Genesys Cloud bot list](#load-the-power-va-bot-to-the-genesys-cloud-bot-list "Goes to the Load the Power VA bot to the Genesys Cloud bot list section")
- [Create an Architect flow](#create-an-architect-flow "Goes to the Create an Architect Flow section")
- [Set up Web Messaging and test the bot](#set-up-web-messaging-and-test-the-bot "Goes to the Set up Web Messaging and test the bot section")
- [Additional resources](#additional-resources "Goes to the Additional Resources Section")

### Download the repository containing the project files

1. Clone the GitHub repository [GCBotConnectorPowerVa repository](https://github.com/msassoon/GCBotConnectorPowerVa "Opens the GCBotConnectorPowerVa GitHub repository") to your machine. The `GCBotConnectorPowerVA/src` folder includes the solution-specific Python files:
* `Automate_BYOB2MS.py`
Expand Down
74 changes: 66 additions & 8 deletions src/Automate_BYOB2MS.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import html
import http.client
import json
import time
import logging
import bot_sessions

# the token also acts as the bot id
MS_BOT_AUTHORIZATION_SECRET = "Bearer "
MS_BOT_AUTHORIZATION_SECRET = "Bearer [ms bot authorization token here]"

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# For MS Bots to work properly it's important that the bot conversation ends with a TransferToAgent action which will
# emit the pertinent session variables and intent (topic) name returned - otherwise this data is not returned back
# through the API

# Note this sample code is for the classic Microsoft Power Virtual Agents and not the newer Microsoft CoPilot Agents
# The code for CoPilots should be similar but the endpoints and requests/responses are different.


def lambda_handler(event, context):
logger.info('Event:')
logger.info(json.dumps(event))
Expand Down Expand Up @@ -165,7 +174,7 @@ def convert_ms_response_to_byob(ms_response, bot_session):
print('message')
# This is a return message to be sent out to the user
rv['botState'] = 'MoreData'
rv['replymessages'].append(convert_text_response_to_message_format(response))
rv['replymessages'] = convert_text_response_to_message_format(response)
# Likewise with intent, interim slot values aren't returned, we get this back when the conversation is over
#rv['slotValues'] = None
elif response['type'] == 'event' and response['name'] == 'handoff.initiate':
Expand Down Expand Up @@ -211,7 +220,7 @@ def get_slot_values_from_transfer_to_action_event(response):
# MS will return both user defined and internal context values in this set - internal variables are
# helpfully prefixed with 'va_' so we'll presume that any variable that's NOT prefixed with 'va_' is meant
# to be returned as a detected slot!
if key.startswith('va_'):
if not key.startswith('va_'):
rv[key] = response['value'][key]

return rv
Expand All @@ -224,13 +233,62 @@ def get_first_response_type(generic_response):
return None

def convert_text_response_to_message_format(response):
new_msg = dict()
rv = list()
if 'textFormat' in response and response['textFormat'] == 'markdown':
if 'text' in response:
new_msg['type'] = 'text'
new_msg['text'] = response['text']
if 'text' in response and response['text'] is not None:
# To handle cards/carousels support, have the response message in the MS Bot return the Genesys BotConnector
# JSON for the structured response. This can either be a single structured response (surrounded with '{}'
# or a list of responses (surrounded with '[]')
# -----
# For Example:
"""
[
{
"type": "Text",
"text": "Hello, how can I help you today?"
},
{
"type": "Structured",
"content": [
{
"contentType": "QuickReply",
"quickReply": {
"text": "I want to order cookies"
}
},
{
"contentType": "QuickReply",
"quickReply": {
"text": "I want to book a trip to Paris"
}
}
]
}
]
"""
# MS will escape the response so we need to unescape it...
unescaped_response = html.unescape(response['text'])
# And we need to do additional clean up on the brackets
unescaped_response = unescaped_response.replace('\[', '[')
unescaped_response = unescaped_response.replace('\]', ']')

# Check the first character of the response to see if it's Json...
if unescaped_response[0] == '{':
# Got a brace, parse this as one object.
new_msg = json.loads(unescaped_response)
rv.append(new_msg)
elif unescaped_response[0] == '[':
# Got a bracket, parse this as a list
rv = json.loads(unescaped_response)
else:
# No bracket or brace, treat this as a text response.
new_msg = dict()
new_msg['type'] = 'text'
new_msg['text'] = response['text']
rv.append(new_msg)

return rv

return new_msg

def convert_entities_to_slots(response_entities):
rv = dict()
Expand Down

0 comments on commit cd4815d

Please sign in to comment.