async def _async_converse( opp: core.OpenPeerPower, text: str, conversation_id: str, context: core.Context ) -> intent.IntentResponse: """Process text and get intent.""" agent = await _get_agent(opp) try: intent_result = await agent.async_process(text, context, conversation_id) except intent.IntentHandleError as err: intent_result = intent.IntentResponse() intent_result.async_set_speech(str(err)) if intent_result is None: intent_result = intent.IntentResponse() intent_result.async_set_speech("Sorry, I didn't understand that") return intent_result
async def async_process( self, text: str, context: Context, conversation_id: Optional[str] = None) -> intent.IntentResponse: """Process a sentence.""" response = await self.api.async_converse_text(text, conversation_id) first_choice = True buffer = "" for message in response["messages"]: if message["type"] == "text": buffer += "\n" + message["text"] elif message["type"] == "picture": buffer += "\n Picture: " + message["url"] elif message["type"] == "rdl": buffer += ("\n Link: " + message["rdl"]["displayTitle"] + " " + message["rdl"]["webCallback"]) elif message["type"] == "choice": if first_choice: first_choice = False else: buffer += "," buffer += f" {message['title']}" intent_result = intent.IntentResponse() intent_result.async_set_speech(buffer.strip()) return intent_result
async def async_process( self, text: str, context: Context, conversation_id: str | None = None) -> intent.IntentResponse: """Process a sentence.""" response = await self.api.async_converse_text(text, conversation_id) first_choice = True buffer = "" for message in response["messages"]: if message["type"] == "text": buffer += f"\n{message['text']}" elif message["type"] == "picture": buffer += f"\n Picture: {message['url']}" elif message["type"] == "rdl": buffer += (f"\n Link: {message['rdl']['displayTitle']} " f"{message['rdl']['webCallback']}") elif message["type"] == "choice": if first_choice: first_choice = False else: buffer += "," buffer += f" {message['title']}" intent_result = intent.IntentResponse() intent_result.async_set_speech(buffer.strip()) return intent_result
async def _async_handle_conversation_message(self, conv_id, user_id, event): """Handle a message sent to a conversation.""" user = self._user_list.get_user(user_id) if user.is_self: return message = event.text _LOGGER.debug("Handling message '%s' from %s", message, user.full_name) intents = self._conversation_intents.get(conv_id) if intents is not None: is_error = False try: intent_result = await self._async_process( intents, message, conv_id) except (intent.UnknownIntent, intent.IntentHandleError) as err: is_error = True intent_result = intent.IntentResponse() intent_result.async_set_speech(str(err)) if intent_result is None: is_error = True intent_result = intent.IntentResponse() intent_result.async_set_speech( "Sorry, I didn't understand that") message = (intent_result.as_dict().get("speech", {}).get("plain", {}).get("speech")) if (message is not None) and not ( is_error and conv_id in self._error_suppressed_conv_ids): await self._async_send_message( [{ "text": message, "parse_str": True }], [{ CONF_CONVERSATION_ID: conv_id }], None, )
async def post(self, request, data): """Handle intent with name/data.""" opp = request.app["opp"] try: intent_name = data["name"] slots = { key: { "value": value } for key, value in data.get("data", {}).items() } intent_result = await intent.async_handle(opp, DOMAIN, intent_name, slots, "", self.context(request)) except intent.IntentHandleError as err: intent_result = intent.IntentResponse() intent_result.async_set_speech(str(err)) if intent_result is None: intent_result = intent.IntentResponse() intent_result.async_set_speech("Sorry, I couldn't handle that") return self.json(intent_result)
async def async_process(self, text, context, conversation_id): """Process some text.""" calls.append((text, context, conversation_id)) response = intent.IntentResponse() response.async_set_speech("Test response") return response