5 import dialogflow
as df
6 from google.protobuf.json_format
import MessageToJson
13 from audio_common_msgs.msg
import AudioData
14 from sound_play.msg
import SoundRequest
15 from sound_play.msg
import SoundRequestAction
16 from sound_play.msg
import SoundRequestGoal
17 from speech_recognition_msgs.msg
import SpeechRecognitionCandidates
18 from std_msgs.msg
import String
20 from dialogflow_task_executive.msg
import DialogResponse
26 LISTENING =
"LISTENING" 55 return self.
_state == state
58 return not self.
__eq__(state)
67 self.
language = rospy.get_param(
"~language",
"ja-JP")
70 self.
use_audio = rospy.get_param(
"~use_audio",
False)
75 self.
use_tts = rospy.get_param(
"~use_tts",
True)
78 self.
timeout = rospy.get_param(
"~timeout", 10.0)
82 "~hotword", [
"ねえねえ",
"こんにちは",
"やあ"])
92 "robotsound_jp", SoundRequestAction)
93 if not self.sound_action.wait_for_server(rospy.Duration(5.0)):
102 "dialog_response", DialogResponse, queue_size=1)
106 audio_encoding=df.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16,
113 "speech_audio", AudioData, self.
input_cb)
116 "speech_to_text", SpeechRecognitionCandidates,
120 self.df_thread.daemon =
True 121 self.df_thread.start()
124 if self.
state != State.IDLE:
125 if (rospy.Time.now() - self.state.last_changed
126 > rospy.Duration(self.
timeout)):
127 self.state.set(State.IDLE)
132 rospy.loginfo(
"Hotword received")
133 self.state.set(State.LISTENING)
137 self.state.set(State.LISTENING)
140 self.
hotword_cb(String(data=msg.transcript[0]))
142 if self.
state == State.LISTENING:
144 rospy.loginfo(
"Received input")
146 rospy.logdebug(
"Received input but ignored")
149 query = df.types.QueryInput(
150 text=df.types.TextInput(
151 text=data, language_code=self.
language))
152 return self.session_client.detect_intent(
153 session=session, query_input=query).query_result
156 query = df.types.QueryInput(audio_config=self.
audio_config)
157 return self.session_client.detect_intent(
158 session=session, query_input=query,
159 input_audio=data).query_result
162 rospy.loginfo(pprint.pformat(result))
165 msg = DialogResponse()
166 msg.header.stamp = rospy.Time.now()
167 if result.action
is not 'input.unknown':
168 rospy.logwarn(
"Unknown action")
169 msg.query = result.query_text.encode(
"utf-8")
170 msg.action = result.action
171 msg.response = result.fulfillment_text.encode(
"utf-8")
172 msg.fulfilled = result.all_required_params_present
173 msg.parameters = MessageToJson(result.parameters)
174 msg.speech_score = result.speech_recognition_confidence
175 msg.intent_score = result.intent_detection_confidence
176 self.pub_res.publish(msg)
182 command=SoundRequest.PLAY_ONCE,
183 sound=SoundRequest.SAY,
185 arg=result.fulfillment_text.encode(
'utf-8'),
187 self.sound_action.send_goal_and_wait(
188 SoundRequestGoal(sound_request=msg),
189 rospy.Duration(10.0))
193 if rospy.is_shutdown():
196 msg = self.queue.get(timeout=0.1)
197 rospy.loginfo(
"Processing")
201 "Created new session: {}".format(self.
session_id))
202 session = self.session_client.session_path(
205 if isinstance(msg, AudioData):
207 elif isinstance(msg, SpeechRecognitionCandidates):
209 msg.transcript[0], session)
211 raise RuntimeError(
"Invalid data")
215 if result.action ==
"Bye":
216 self.state.set(State.IDLE)
220 except Exception
as e:
224 if __name__ ==
'__main__':
225 rospy.init_node(
"dialogflow_client")
def speak_result(self, result)
def __init__(self, init_state=None)
def speech_timer_cb(self, event=None)
def publish_result(self, result)
def detect_intent_text(self, data, session)
def hotword_cb(self, msg)
def detect_intent_audio(self, data, session)
def print_result(self, result)