From d03ed03b93700e9938b96b7b5680820797acdae3 Mon Sep 17 00:00:00 2001 From: Yoshiki Obinata Date: Mon, 21 Nov 2022 12:37:39 +0900 Subject: [PATCH] [dialogflow_intent_client] Handling dialogflow intents --- dialogflow_intent_client/CMakeLists.txt | 46 ++++++++ .../action/ListIntent.action | 6 + .../action/RegisterIntent.action | 5 + .../launch/dialogflow_intent_client.launch | 11 ++ dialogflow_intent_client/msg/IntentInfo.msg | 3 + .../node_scripts/dialogflow_intent_node.py | 110 ++++++++++++++++++ dialogflow_intent_client/package.xml | 23 ++++ dialogflow_intent_client/requirements.txt | 2 + .../launch/dialogflow_ros.launch | 7 ++ 9 files changed, 213 insertions(+) create mode 100644 dialogflow_intent_client/CMakeLists.txt create mode 100644 dialogflow_intent_client/action/ListIntent.action create mode 100644 dialogflow_intent_client/action/RegisterIntent.action create mode 100644 dialogflow_intent_client/launch/dialogflow_intent_client.launch create mode 100644 dialogflow_intent_client/msg/IntentInfo.msg create mode 100644 dialogflow_intent_client/node_scripts/dialogflow_intent_node.py create mode 100644 dialogflow_intent_client/package.xml create mode 100644 dialogflow_intent_client/requirements.txt diff --git a/dialogflow_intent_client/CMakeLists.txt b/dialogflow_intent_client/CMakeLists.txt new file mode 100644 index 000000000..7480c57f4 --- /dev/null +++ b/dialogflow_intent_client/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.0.2) +project(dialogflow_intent_client) + +find_package(catkin REQUIRED COMPONENTS + catkin_virtualenv REQUIRED + message_generation + std_msgs + actionlib_msgs + ) + +add_message_files( + FILES + IntentInfo.msg + ) + +add_action_files( + FILES + RegisterIntent.action + ListIntent.action + ) + +generate_messages( + DEPENDENCIES + std_msgs + actionlib_msgs + ) + +catkin_package( + CATKIN_DEPENDS message_runtime + ) + +catkin_generate_virtualenv( + PYTHON_INTERPRETER python3 + ) + +file(GLOB NODE_SCRIPTS_FILES node_scripts/*.py) + +catkin_install_python( + PROGRAMS ${NODE_SCRIPTS_FILES} + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) + +install(DIRECTORY launch + DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} + USE_SOURCE_PERMISSIONS +) diff --git a/dialogflow_intent_client/action/ListIntent.action b/dialogflow_intent_client/action/ListIntent.action new file mode 100644 index 000000000..024a3eb7e --- /dev/null +++ b/dialogflow_intent_client/action/ListIntent.action @@ -0,0 +1,6 @@ +std_msgs/Empty request +--- +dialogflow_intent_client/IntentInfo[] intents +bool done +--- +string status diff --git a/dialogflow_intent_client/action/RegisterIntent.action b/dialogflow_intent_client/action/RegisterIntent.action new file mode 100644 index 000000000..30bc6f3a4 --- /dev/null +++ b/dialogflow_intent_client/action/RegisterIntent.action @@ -0,0 +1,5 @@ +dialogflow_intent_client/IntentInfo intent +--- +bool done +--- +string status diff --git a/dialogflow_intent_client/launch/dialogflow_intent_client.launch b/dialogflow_intent_client/launch/dialogflow_intent_client.launch new file mode 100644 index 000000000..d5db77a9f --- /dev/null +++ b/dialogflow_intent_client/launch/dialogflow_intent_client.launch @@ -0,0 +1,11 @@ + + + + + + google_cloud_credentials_json: $(arg credential) + + + diff --git a/dialogflow_intent_client/msg/IntentInfo.msg b/dialogflow_intent_client/msg/IntentInfo.msg new file mode 100644 index 000000000..332e81164 --- /dev/null +++ b/dialogflow_intent_client/msg/IntentInfo.msg @@ -0,0 +1,3 @@ +string intent +string[] concat_training_phrases +string[] message_texts diff --git a/dialogflow_intent_client/node_scripts/dialogflow_intent_node.py b/dialogflow_intent_client/node_scripts/dialogflow_intent_node.py new file mode 100644 index 000000000..1af4e9cb4 --- /dev/null +++ b/dialogflow_intent_client/node_scripts/dialogflow_intent_node.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python + +import actionlib +from dialogflow_intent_client.msg import RegisterIntentAction, RegisterIntentGoal, RegisterIntentResult, RegisterIntentFeedback +from dialogflow_intent_client.msg import ListIntentAction, ListIntentGoal, ListIntentResult, ListIntentFeedback +from dialogflow_intent_client.msg import IntentInfo +import google.cloud.dialogflow as df +from google.oauth2.service_account import Credentials +import rospy + +class DialogflowIntentClient(object): + def __init__(self): + credentials_json = rospy.get_param( + '~google_cloud_credentials_json', None) + credentials = Credentials.from_service_account_file(credentials_json) + self.intents_client = df.IntentsClient(credentials=credentials) + self.project_id = credentials.project_id + self._intent_create_as = actionlib.SimpleActionServer("~register_intent_action", + RegisterIntentAction, + execute_cb=self.register_cb, + auto_start=False) + self._intent_list_as = actionlib.SimpleActionServer("~list_intent_action", + ListIntentAction, + execute_cb=self.list_cb, + auto_start=False) + self._intent_create_as.start() + self._intent_list_as.start() + + def register_df_intent(self, intent_name, training_phrases, message_texts): + parent = df.AgentsClient.agent_path(self.project_id) + phrases = [] + for phrase in training_phrases: + part = df.Intent.TrainingPhrase.Part(text=phrase) + training_phrase = df.Intent.TrainingPhrase(parts=[part]) + phrases.append(training_phrase) + text = df.Intent.Message.Text(text=message_texts) + message = df.Intent.Message(text=text) + intent = df.Intent( + display_name=intent_name, training_phrases=phrases, messages=[message]) + response = self.intents_client.create_intent( + request={"parent": parent, "intent": intent} + ) + return response + + def list_df_intent(self): + parent = df.AgentsClient.agent_path(self.project_id) + req = df.ListIntentsRequest(parent=parent, + intent_view=df.IntentView.INTENT_VIEW_FULL) + intents = self.intents_client.list_intents(request=req) + msgs = [] + for intent in intents: + msg = IntentInfo() + msg.intent = intent.display_name + for training_phrase in intent.training_phrases: # training phrases + phrase = "" + for part in training_phrase.parts: + phrase += str(part.text) + msg.concat_training_phrases.append(str(phrase)) + for message in intent.messages: + msg.message_texts.append(str(message.text)) + msgs.append(msg) + return msgs + + def register_cb(self, goal): + feedback = RegisterIntentFeedback() + result = RegisterIntentResult() + success = False + try: + res = self.register_df_intent(goal.intent.intent, + goal.intent.concat_training_phrases, + goal.intent.message_texts) + feedback.status = str(res) + success = True + rospy.loginfo("Succeeded in registering the intent: {}".format(goal.intent.intent)) + except Exception as e: + rospy.logerr(str(e)) + feedback.status = str(e) + success = False + finally: + self._intent_create_as.publish_feedback(feedback) + result.done = success + self._intent_create_as.set_succeeded(result) + + def list_cb(self, goal): + feedback = ListIntentFeedback() + result = ListIntentResult() + success = False + try: + msgs = self.list_df_intent() + intents_info = "" + for msg in msgs: + result.intents.append(msg) + intents_info += "{}, ".format(msg.intent) + success = True + rospy.loginfo("Listed intents: {}".format(intents_info)) + # from IPython.terminal import embed; ipshell=embed.InteractiveShellEmbed(config=embed.load_default_config())(local_ns=locals()) + except Exception as e: + rospy.logerr(str(e)) + feedback.status = str(e) + success = False + finally: + self._intent_list_as.publish_feedback(feedback) + result.done = success + self._intent_list_as.set_succeeded(result) + + +if __name__ == "__main__": + rospy.init_node("dialogflow_intent_manager") + node = DialogflowIntentClient() + rospy.spin() diff --git a/dialogflow_intent_client/package.xml b/dialogflow_intent_client/package.xml new file mode 100644 index 000000000..888319578 --- /dev/null +++ b/dialogflow_intent_client/package.xml @@ -0,0 +1,23 @@ + + + dialogflow_intent_client + 2.1.24 + The dialogflow_intent_client package + + Yoshiki Obinata + + BSD + + Yoshiki Obinata + + catkin + catkin_virtualenv + message_generation + std_msgs + roslaunch + message_runtime + + + requirements.txt + + diff --git a/dialogflow_intent_client/requirements.txt b/dialogflow_intent_client/requirements.txt new file mode 100644 index 000000000..257568462 --- /dev/null +++ b/dialogflow_intent_client/requirements.txt @@ -0,0 +1,2 @@ +google-auth==2.9.0 +google-cloud-dialogflow==2.14.1 diff --git a/dialogflow_task_executive/launch/dialogflow_ros.launch b/dialogflow_task_executive/launch/dialogflow_ros.launch index fa0045df8..0e3f866c2 100644 --- a/dialogflow_task_executive/launch/dialogflow_ros.launch +++ b/dialogflow_task_executive/launch/dialogflow_ros.launch @@ -10,6 +10,7 @@ + + + + + + +