An API endpoint has been created following the guide in part 1.
Note: The SAP backend can be hosted on GCP, on premise, or on any other cloud.
Parameter | value |
---|---|
API_ENDPOINT | your api proxy endpoint |
APIKEY | your api key for the application |
# Function dependencies, for example:
# package>=version
requests==2.18.4
import json
import os
import requests
def webhook(request):
url = os.getenv('API_ENDPOINT')
apikey = os.getenv('APIKEY')
request_json = request.get_json()
if request_json.get('fulfillmentInfo', None):
tag = request_json['fulfillmentInfo']['tag']
print('Tag: {}'.format(tag))
else:
return ('Unrecognized request', 404)
if tag == 'salesorder.list':
params = dict(
apikey=apikey
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
reply = {
"fulfillment_response": {
"messages": [
{
"payload": {
"richContent": [
[]
]
}
}
]
}
}
for salesorder in data['results']:
salesorderitem = {
"type": "list",
"title": salesorder['SalesOrderID'],
"subtitle": salesorder['Note'],
"event": {
"name": "salesorder.list",
"languageCode": "",
"parameters": {
"SalesOrderID": salesorder['SalesOrderID'],
"SalesOrderKey" : salesorder['SalesOrderKey']
}
}
}
reply['fulfillment_response']['messages'][0]['payload']['richContent'][0].append(salesorderitem)
reply['fulfillment_response']['messages'][0]['payload']['richContent'][0].append({"type":"divider"})
elif tag == 'salesorder.detail':
order = str(int(request_json['sessionInfo']['parameters']['ordernumber'])).zfill(10)
params = dict(
apikey=apikey,
salesorderid=order
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
if len(data['results']):
salesorder = data['results'][0]
text = 'Sales order {} total value is {} {} and it has been {} and {}'.format(salesorder['SalesOrderID'],salesorder['TotalSum'],salesorder['Currency'],salesorder['DeliveryStatusDescription'],salesorder['BillingStatusDescription'])
else:
text = 'No sales order {} is available in the system'.format(order)
reply = {
"fulfillment_response": {
"messages": [
{
"text": {
"text": [text]
}
}
]
}
}
return json.dumps(reply)
{
"fulfillmentInfo": {
"tag" : "salesorder.list"
}
}
gcloud services enable dialogflow.googleapis.com
gs://public-demo-assets/dialogflow/SAPSalesOrder.json
git clone https://github.com/ivanfemia/ccai-agent-client.git
cd ccai-agent-client
cp .dockerignore.SAMPLE .dockerignore
cp Dockerfile.SAMPLE Dockerfile
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Configuration file."""
PAGE_TITLE = 'SAP Dialogflow CX'
PAGE_H1_TITLE = 'SAP Sales Order Agent'
Note: Make sure you have an empty line at the end of the file
Parameter | value |
---|---|
DIALOGFLOW_PROJECT_ID | your GCP project id |
DIALOGFLOW_AGENT_ID | your agent id |
Note: DIALOGFLOW_AGENT_ID is the unique identifier we copied in Dialogflow CX. In my case projects/sap-dialogflow/locations/us-central1/agents/644c170f-6543-4a7b-ab78-82736f9d05e9 the agent id is 644c170f-6543-4a7b-ab78-82736f9d05e9
export DIALOGFLOW_PROJECT_ID=**your GCP project id***
gcloud builds submit --tag gcr.io/$DIALOGFLOW_PROJECT_ID/agent
gcloud run deploy --image gcr.io/$DIALOGFLOW_PROJECT_ID/agent --platform managed
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |