📍

どこからの電話?Amazon Connect着信番号をGoogle Maps APIで場所特定

に公開

はじめに: Google Maps PlatformのPlaces API

電話番号をGoogle検索すると、その電話番号に当てはまるGoogle Mapの施設や場所が表示されることがありますよね。それと同じことをAPIでできないか?と調べたら、Google Maps PlatformのPlaces APIの Text Search で実現できると分かったので使ってみました。

https://developers.google.com/maps/documentation/places/web-service/text-search?hl=ja

「+1 + 514-670-8700」 このクエリには電話番号が含まれています。検索品質を向上させるため、検索文字列に国コードを含めることをおすすめします。これにより、その電話番号に関連付けられている場所の検索結果が複数返されます。

Places API利用には、Google Cloudへの登録と、APIキーの発行が必要です。トライアル期間後も毎月200ドル分の利用は無料枠なので、安心して遊ぶことができます。

https://developers.google.com/maps/gmp-get-started?hl=ja#create-project
https://mapsplatform.google.com/pricing/?hl=ja

今回はAmazon Connectという、AWSのコンタクトセンター構築サービスで、電話を受信したときにどこの施設/場所からの電話番号かを特定して、メール通知する仕組みを作ってみます。

システム構成

Amazon Connect

公式ドキュメントを参考に、ワークフローに Invoke AWS Lambda function ブロックを作成して、後述のLambda関数を呼び出します。

Lambdaのコード内で event['Details']['ContactData']['CustomerEndpoint']['Address'] によりイベント解析で顧客電話番号を取得できるので、Input Parameterは設定しなくて大丈夫です。

https://docs.aws.amazon.com/ja_jp/connect/latest/adminguide/connect-lambda-functions.html

Lambda

Google Maps APIのリクエストには、こちらのPython SDK google-maps-services-python を使いました。

https://github.com/googlemaps/google-maps-services-python

SDKによるtext searchの方法は、SDKのこちらのテストコードを参考にしました。client.places を使えばtext searchができると分かります。

https://github.com/googlemaps/google-maps-services-python/blob/master/tests/test_places.py#L79-L99

Google Mapsに登録されていない電話番号ももちろんあるので、その場合はシンプルにググれるようにGoogle検索のリンクも付けてみます。

Python 3で書いたLambdaのコード例は以下です。TODOコメントの部分は個人の値に書き換えてください。

import json
import boto3
import googlemaps

def lambda_handler(event, context):
    customerPhoneNumber = event['Details']['ContactData']['CustomerEndpoint']['Address']
    sns_client= boto3.client('sns')
    sns_topic_arn = 'arn:aws:sns:ap-northeast-1:xxxxx:xxxxx' # TODO: SNS topic ARNを指定
    google_maps_api_key = 'api_key' # TODO: Google Maps API keyを指定

    google_maps = googlemaps.Client(key=google_maps_api_key)
    map_results = google_maps.places(customerPhoneNumber).get('results', [])
    map_address = 'Unknown'
    map_name = 'Unknown'
    if map_results:
        first_result = map_results[0]
        map_address = first_result.get('formatted_address', 'Unknown')
        map_name = first_result.get('name', 'Unknown')

    # Google検索リンク用: 日本の電話番号+81なら0に置き換える
    phone_number = customerPhoneNumber.replace('+81', '0', 1) if customerPhoneNumber.startswith('+81') else customerPhoneNumber

    sns_response = sns_client.publish(
        TopicArn = sns_topic_arn,
        Message = (
            f"Amazon Connectに着信がありました。\n"
            f"顧客電話番号: {customerPhoneNumber}\n"
            f"Google Maps住所: {map_address}\n"
            f"Google Maps名前: {map_name}\n"
            f"Google検索: https://www.google.com/search?q={phone_number}"
        ),
        Subject = "Amazon Connect call notification"
    )
    return {
        'statusCode': sns_response['ResponseMetadata']['HTTPStatusCode'],
        'body': 'Hello!'
    }

SNS

公式ドキュメントを参考にSNS Topicを作成します。

私の場合はAWS Management Consoleから、オプションの値 (暗号化など) は全て設定せずスキップして、自分のメールアドレスをEndpointに設定したシンプルなSNS Topicを作成しました。

https://docs.aws.amazon.com/ja_jp/sns/latest/dg/sns-create-topic.html

これでAmazon Connectのワークフローに着信があると、このようなメールが届くようになります。

Discussion