lex_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #include <lex_common_msgs/KeyValue.h>
16 #include <lex_node/lex_node.h>
17 
18 #include <algorithm>
19 #include <iostream>
20 #include <utility>
21 
22 namespace Aws {
23 namespace Lex {
24 
25 LexRequest& operator<<(LexRequest& out_request, const lex_common_msgs::AudioTextConversationRequest& ros_request) {
26  out_request.accept_type = ros_request.accept_type;
27  out_request.audio_request = ros_request.audio_request.data;
28  out_request.content_type = ros_request.content_type;
29  out_request.text_request = ros_request.text_request;
30  return out_request;
31 }
32 
33 lex_common_msgs::AudioTextConversationResponse& operator<<(lex_common_msgs::AudioTextConversationResponse& ros_response,
34  const LexResponse& lex_response) {
35  ros_response.audio_response.data = lex_response.audio_response;
36  ros_response.dialog_state = lex_response.dialog_state;
37  ros_response.intent_name = lex_response.intent_name;
38  ros_response.message_format_type = lex_response.message_format_type;
39  ros_response.text_response = lex_response.text_response;
40  ros_response.slots = std::vector<lex_common_msgs::KeyValue>();
41  std::transform(lex_response.slots.begin(), lex_response.slots.end(),
42  std::back_inserter(ros_response.slots), [](const std::pair<std::string, std::string>& slot) {
43  lex_common_msgs::KeyValue key_value;
44  key_value.key = slot.first;
45  key_value.value = slot.second;
46  return key_value;
47  });
48  return ros_response;
49 }
50 
51 LexNode::LexNode() : node_handle_("~") {}
52 
53 ErrorCode LexNode::Init(std::shared_ptr<PostContentInterface> post_content)
54 {
55  if (!post_content) {
56  return ErrorCode::INVALID_ARGUMENT;
57  }
58  post_content_ = post_content;
59  lex_server_ =
60  node_handle_.advertiseService<>("lex_conversation", &LexNode::LexServerCallback, this);
61  return ErrorCode::SUCCESS;
62 }
63 
64 bool LexNode::LexServerCallback(lex_common_msgs::AudioTextConversationRequest & request,
65  lex_common_msgs::AudioTextConversationResponse & response)
66 {
67  LexRequest lex_request;
68  lex_request << request;
69  LexResponse lex_response;
70  bool is_success = !post_content_->PostContent(lex_request, lex_response);
71  if (is_success) {
72  response << lex_response;
73  }
74  return is_success;
75 }
76 
77 } // namespace Lex
78 } // namespace Aws
std::shared_ptr< PostContentInterface > post_content_
Definition: lex_node.h:48
std::string text_request
std::string content_type
std::vector< uint8_t > audio_request
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ErrorCode Init(std::shared_ptr< PostContentInterface > lex_interactor)
Definition: lex_node.cpp:53
bool LexServerCallback(lex_common_msgs::AudioTextConversationRequest &request, lex_common_msgs::AudioTextConversationResponse &response)
Definition: lex_node.cpp:64
std::ostream & operator<<(std::ostream &os, const Aws::LexRuntimeService::Model::PostContentRequest &request)
std::string message_format_type
std::string accept_type
std::unordered_map< std::string, std::string > slots
ros::ServiceServer lex_server_
Definition: lex_node.h:43
std::string text_response
std::vector< uint8_t > audio_response
std::string intent_name
ros::NodeHandle node_handle_
Definition: lex_node.h:53
std::string dialog_state


lex_node
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:13:38