-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dialogflow_intent_client] Handling dialogflow intents
- Loading branch information
Showing
9 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
dialogflow_intent_client/launch/dialogflow_intent_client.launch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
110
dialogflow_intent_client/node_scripts/dialogflow_intent_node.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters