Last active
October 7, 2017 16:40
-
-
Save jagrament/e4ea69f0baf9cbc7b2ab7e97fb31200d to your computer and use it in GitHub Desktop.
CGS Lambda sample for Expert Agent Developer Dashboard (https://developers.sebastien.ai)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib | |
def respond(err, res=None): | |
return { | |
'statusCode': '400' if err else '200', | |
'body': err.message if err else json.dumps(res), | |
'headers': { | |
'Content-Type': 'application/json' | |
} | |
} | |
def lambda_handler(event, context): | |
print("Received event: " + json.dumps(event, indent=2)) | |
payload = json.loads(event['body']) | |
headers = { | |
'Content-Type': 'application/json', | |
'x-api-key': 'Set your Repl-AI APIKEY' | |
} | |
# Detail: https://repl-ai.jp/docs/references/dialogue | |
request_url = "https://api.repl-ai.jp/v1/dialogue" | |
params = { | |
"appUserId":"Set your Repl-AI appUserId", | |
"botId":"Set your BotId", | |
"voiceText": payload["args"]["utterance"], | |
"initTopicId":"Set your scenario ID" | |
} | |
json_data = json.dumps(params).encode("utf-8") | |
encoded_params = urllib.parse.urlencode(params).encode('utf-8') | |
request = urllib.request.Request(request_url, data=json_data, method="POST", headers=headers) | |
with urllib.request.urlopen(request) as response: | |
response_body = response.read().decode("utf-8") | |
print(response_body) | |
result = json.loads(response_body) | |
return respond(None, {"error_code": "success", "status": "true", "user_id": payload["user_id"], "bot_id": payload["bot_id"], "params": {"status": "true", "message": result["systemText"]["expression"]}}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment