From 1d5541bccb6591f7522de6d7333cb66f03537ee9 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Fri, 31 Dec 2021 15:33:15 +0900 Subject: [PATCH] load dialogflow project_id from credentials --- .../node_scripts/dialogflow_client.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/dialogflow_task_executive/node_scripts/dialogflow_client.py b/dialogflow_task_executive/node_scripts/dialogflow_client.py index 27af53644..8f51b5f6e 100644 --- a/dialogflow_task_executive/node_scripts/dialogflow_client.py +++ b/dialogflow_task_executive/node_scripts/dialogflow_client.py @@ -3,13 +3,13 @@ import actionlib import dialogflow as df +from google.oauth2.service_account import Credentials from google.protobuf.json_format import MessageToJson import pprint import Queue import rospy import threading import uuid -import os from audio_common_msgs.msg import AudioData from sound_play.msg import SoundRequest @@ -62,8 +62,6 @@ def __ne__(self, state): class DialogflowClient(object): def __init__(self): - # project id for google cloud service - self.project_id = rospy.get_param("~project_id") # language for dialogflow self.language = rospy.get_param("~language", "ja-JP") @@ -72,10 +70,6 @@ def __init__(self): # sample rate of audio data self.audio_sample_rate = rospy.get_param("~audio_sample_rate", 16000) - # if GOOLGE_APPLICATION_CREDENTIALS is not set, use google_cloud_credentials_json param - if not "GOOGLE_APPLICATION_CREDENTIALS" in os.environ: - os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rospy.get_param("~google_cloud_credentials_json") - # use TTS feature self.use_tts = rospy.get_param("~use_tts", True) self.volume = rospy.get_param('~volume', 1.0) @@ -89,9 +83,26 @@ def __init__(self): self.state = State() self.session_id = None - self.session_client = df.SessionsClient() self.queue = Queue.Queue() - self.last_spoken = rospy.Time(0) + + credentials_json = rospy.get_param( + '~google_cloud_credentials_json', None) + if credentials_json is None: + rospy.loginfo("Loading credential json from env") + # project id for google cloud service + self.project_id = rospy.get_param("~project_id", None) + self.session_client = df.SessionsClient() + else: + rospy.loginfo("Loading credential json from rosparam") + credentials = Credentials.from_service_account_file( + credentials_json + ) + self.project_id = credentials.project_id + self.session_client = df.SessionsClient( + credentials=credentials + ) + if self.project_id is None: + rospy.logerr('project ID is not set') if self.use_tts: soundplay_action_name = rospy.get_param(