New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade Dialogflow to work with V2 API #25975
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,9 @@ | |||||
|
||||||
CONFIG_SCHEMA = vol.Schema({DOMAIN: {}}, extra=vol.ALLOW_EXTRA) | ||||||
|
||||||
V1 = 1 | ||||||
V2 = 2 | ||||||
|
||||||
|
||||||
class DialogFlowError(HomeAssistantError): | ||||||
"""Raised when a DialogFlow error happens.""" | ||||||
|
@@ -84,23 +87,45 @@ async def async_unload_entry(hass, entry): | |||||
|
||||||
def dialogflow_error_response(message, error): | ||||||
"""Return a response saying the error message.""" | ||||||
dialogflow_response = DialogflowResponse(message["result"]["parameters"]) | ||||||
_api_version = get_api_version(message) | ||||||
if _api_version is V1: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intended pointer equivalency here since it's comparing integers, but I'm happy to change it to ==. |
||||||
parameters = message["result"]["parameters"] | ||||||
elif _api_version is V2: | ||||||
parameters = message["queryResult"]["parameters"] | ||||||
dialogflow_response = DialogflowResponse(parameters, get_api_version(message)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good eye! Should be fixed now. |
||||||
dialogflow_response.add_speech(error) | ||||||
return dialogflow_response.as_dict() | ||||||
|
||||||
|
||||||
def get_api_version(message): | ||||||
"""Get API version of Dialogflow message.""" | ||||||
if message.get("id") is not None: | ||||||
return V1 | ||||||
if message.get("responseId") is not None: | ||||||
return V2 | ||||||
|
||||||
|
||||||
async def async_handle_message(hass, message): | ||||||
"""Handle a DialogFlow message.""" | ||||||
req = message.get("result") | ||||||
action_incomplete = req["actionIncomplete"] | ||||||
_api_version = get_api_version(message) | ||||||
if _api_version is V1: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem. |
||||||
_LOGGER.warning( | ||||||
"Dialogflow V1 API will be removed on October 23, 2019. Please change your DialogFlow settings to use the V2 api" | ||||||
) | ||||||
req = message.get("result") | ||||||
action_incomplete = req.get("actionIncomplete", True) | ||||||
if action_incomplete is True: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return | ||||||
|
||||||
if action_incomplete: | ||||||
return None | ||||||
elif _api_version is V2: | ||||||
req = message.get("queryResult") | ||||||
if req.get("allRequiredParamsPresent", False) is False: | ||||||
return | ||||||
|
||||||
action = req.get("action", "") | ||||||
parameters = req.get("parameters").copy() | ||||||
parameters["dialogflow_query"] = message | ||||||
dialogflow_response = DialogflowResponse(parameters) | ||||||
dialogflow_response = DialogflowResponse(parameters, _api_version) | ||||||
|
||||||
if action == "": | ||||||
raise DialogFlowError( | ||||||
|
@@ -123,10 +148,11 @@ async def async_handle_message(hass, message): | |||||
class DialogflowResponse: | ||||||
"""Help generating the response for Dialogflow.""" | ||||||
|
||||||
def __init__(self, parameters): | ||||||
def __init__(self, parameters, api_version): | ||||||
"""Initialize the Dialogflow response.""" | ||||||
self.speech = None | ||||||
self.parameters = {} | ||||||
self.api_version = api_version | ||||||
# Parameter names replace '.' and '-' for '_' | ||||||
for key, value in parameters.items(): | ||||||
underscored_key = key.replace(".", "_").replace("-", "_") | ||||||
|
@@ -143,4 +169,8 @@ def add_speech(self, text): | |||||
|
||||||
def as_dict(self): | ||||||
"""Return response in a Dialogflow valid dictionary.""" | ||||||
return {"speech": self.speech, "displayText": self.speech, "source": SOURCE} | ||||||
if self.api_version is V1: | ||||||
return {"speech": self.speech, "displayText": self.speech, "source": SOURCE} | ||||||
|
||||||
if self.api_version is V2: | ||||||
return {"fulfillmentText": self.speech, "source": SOURCE} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't prefix parameters with
_
if you're using them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - that was bad styling on my part. It's fixed now.