このハンズオンでは、外部のWebサービス(OpenWeather)から取得したデータに基づく、”動的なIVR”を構築してみます。具体的には、顧客が入力した郵便番号をキーにして現在の気象情報を取得し、それを IVR = Amazon Connect の問い合わせフローで再生する、という内容です。これは非常にシンプルなシナリオですが、こうした”動的な IVR”の活用例は以下の様なものが考えられます。
この様に、顧客の置かれている背景やこれまでの履歴など、コンテキストに基づいたコールルーティングは、それぞれの顧客に対してパーソナライズされた、最適な体験を提供するために重要な観点と言えます。
このハンズオンでは、以下のような作業を実施します。
このハンズオンでは、“1.準備” の章の作業が完了していることを前提にしています。まだ、Amazon Connect インスタンスの作成や電話番号の取得が済んでいない方は、先に “1.準備” の作業を実施してください。
Create an Account
をクリックする。
AWSLambdaBasicExecutionRole
を検索し、チェックを入れます。”次のステップ:タグ”を押します。Role_Lambda_CW
と入力して、”ロールの作成”ボタンを押します。getWeatherInfoFunction
と入力します。ランタイムは Python 3.7 を選択します。さらに、 デフォルトの実行ロールの変更 メニューを展開し、上で作成した Role_Lambda_CW ロールを選択します。最後に、右下の”関数の作成”ボタンを押します。
2021年10月時点で AWS Lambda がサポートする Python ランタイムの最新バージョンは3.9ですが、このハンズオンで使用するサンプルコードでは botocore.vendored.requests
を使用するので、Python 3.7 を選択します。
lambda_function.py のコードを以下のものに置き換え、”Deploy”ボタンを押します。
from botocore.vendored import requests
import json
import os
def lambda_handler(event, context):
print(event)
details = event["Details"]
parameters = details["Parameters"]
zipcode = parameters["zipcode"]
#At this point, you are doing some basic validation to ensure that the zip code is in right format
if str.isdigit(zipcode) and len(zipcode)==7:
pass
else:
return{
'Success':'False'
}
#At this point, you are making a GET request to the REST endpoint
url = 'http://api.openweathermap.org/data/2.5/weather'
APPID = os.environ["APPID"]
parameters = {
"zip":zipcode[:3] + "-" + zipcode[3:] + ",jp",
"APPID":APPID,
"units":"metric"
}
print(parameters)
try:
resp = requests.get(url,params=parameters)
if resp.status_code != 200:
# This means something went wrong.
print("there is an error")
return{
'Success':'False1'
}
else:
response=json.loads(resp.text)
weather=response['weather'][0]['description']
temperature=response['main']['temp']
place=response['name']
wind=response['wind']['speed']
msg = json.loads(resp.text)
print(msg)
return{
'Success':'True',
'weather':weather,
'temperature':temperature,
'place':place,
'wind':wind
}
except:
return{
'Success':'False2'
}
環境変数に APPID
と 先ほどメモした OpenWeather API キーを入力します。
“Test”ボタンの右の ▼ を押して、”Configure test event”を選択します。イベント名に amazonConnectTestEvent
と入力し、イベントのペイロードを以下のものに置き換えて、右下の”作成”ボタンを押します。
{
"Details": {
"ContactData": {
"Attributes": {},
"Channel": "VOICE",
"ContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
"CustomerEndpoint": {
"Address": "+1234567890",
"Type": "TELEPHONE_NUMBER"
},
"InitialContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXXX",
"InitiationMethod": "INBOUND | OUTBOUND | TRANSFER | CALLBACK",
"InstanceARN": "arn:aws:connect:aws-region:1234567890:instance/c8c0e68d-2200-4265-82c0-XXXXXXXXXX",
"PreviousContactId": "4a573372-1f28-4e26-b97b-XXXXXXXXXX",
"Queue": "QueueName",
"SystemEndpoint": {
"Address": "+1234567890",
"Type": "TELEPHONE_NUMBER"
}
},
"Parameters": {
"zipcode": "1000001"
}
},
"Name": "ContactFlowEvent"
}
“Test” ボタンを押してテストを実行し、以下のような Response が返されることを確認してください。
Response
{
"Success": "True",
"weather": "scattered clouds",
"temperature": 22.25,
"place": "Chiyoda",
"wind": 0.45
}
まず、以下のサンプルフローをダウンロードします。