Skip to content

Commit

Permalink
[dialogflow_intent_client] Handling dialogflow intents
Browse files Browse the repository at this point in the history
  • Loading branch information
mqcmd196 committed Nov 27, 2022
1 parent a8464dc commit 0ac8440
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 0 deletions.
46 changes: 46 additions & 0 deletions dialogflow_intent_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
6 changes: 6 additions & 0 deletions dialogflow_intent_client/action/ListIntent.action
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
std_msgs/Empty request
---
dialogflow_intent_client/IntentInfo[] intents
bool done
---
string status
5 changes: 5 additions & 0 deletions dialogflow_intent_client/action/RegisterIntent.action
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dialogflow_intent_client/IntentInfo intent
---
bool done
---
string status
11 changes: 11 additions & 0 deletions dialogflow_intent_client/launch/dialogflow_intent_client.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<launch>
<arg name="credential" />
<node name="dialogflow_intent_client"
pkg="dialogflow_intent_client" type="dialogflow_intent_node.py"
output="screen">
<rosparam subst_value="true">
google_cloud_credentials_json: $(arg credential)
</rosparam>
</node>
</launch>
3 changes: 3 additions & 0 deletions dialogflow_intent_client/msg/IntentInfo.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
string intent
string[] concat_training_phrases
string[] message_texts
110 changes: 110 additions & 0 deletions dialogflow_intent_client/node_scripts/dialogflow_intent_node.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions dialogflow_intent_client/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<package format="3">
<name>dialogflow_intent_client</name>
<version>2.1.24</version>
<description>The dialogflow_intent_client package</description>

<maintainer email="[email protected]">Yoshiki Obinata</maintainer>

<license>BSD</license>

<author email="[email protected]">Yoshiki Obinata</author>

<buildtool_depend>catkin</buildtool_depend>
<build_depend>catkin_virtualenv</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>roslaunch</build_depend>
<exec_depend>message_runtime</exec_depend>

<export>
<pip_requirements>requirements.txt</pip_requirements>
</export>
</package>
2 changes: 2 additions & 0 deletions dialogflow_intent_client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-auth==2.9.0
google-cloud-dialogflow==2.14.1
7 changes: 7 additions & 0 deletions dialogflow_task_executive/launch/dialogflow_ros.launch
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<arg name="project_id" default="$(optenv DIALOGFLOW_PROJECT_ID false)"/>
<arg name="enable_hotword" default="true" />
<arg name="always_publish_result" default="false" doc="Always publish dialog_response topic even the node gets actionlib request." />
<arg name="launch_intent_client" default="false" />

<node name="dialogflow_client"
pkg="dialogflow_task_executive" type="dialogflow_client.py"
Expand All @@ -28,4 +29,10 @@
file="$(find dialogflow_task_executive)/config/dialogflow_hotword.yaml"/>
</node>

<group if="$(arg launch_intent_client)">
<include file="$(find dialogflow_intent_client)/launch/dialogflow_intent_client.launch">
<arg name="credential" value="$(arg credential)" />
</include>
</group>

</launch>

0 comments on commit 0ac8440

Please sign in to comment.