...
Before creating the demo, we need it’s essential to understand that the design of the Webserver WebserverComponent
goes hand in hand with the requirements of the demo, as the Webserver WebserverComponent
is the component that handles any event events from the a web client. So, in For this specific tutorial, the Webserver needs to handle the WebserverComponent
should handle a button click event and send live transcripts transcribed produced by Dialogflow service to the a connected client over a WebSocket connection.
So, this This tutorial can split be divided into two parts:
The design of html HTML and
Webserver
Creating the html
Creating the flask server in WebserverHow to create a button and catch a click event on it?
How to render Dialogflow intermediate transcripts dynamically in a text box?
The demo program connects all the servers components (NaoqiMicrophone
NaoqiMicrophoneSensor
(or local microphoneDesktopMicrophone
), WebserverWebserverComponent
,DialogflowService
, Dialogflowetc)
WebserverComponent
The design of html and Webserver
1. How to create a button and catch a click event on it?
In the html, we need to Add add a button and make it listen to the click event. Once the button is clicked, the isStreaming
isClicked
flag will be set to true, and it sends the flag to the Flask server via WebSocket.
Code Block |
---|
// Flag to track if the game should start var isStreamingisClicked = false; // EventGet a listenerreference forto the startstartButton buttonelement click var startButton = document.getElementById('startButton'); // Event listener for the start button click startButton.addEventListener('click', function() { isStreamingisClicked = true; // Set the flag to start streaming sendFlagToServer(isStreamingisClicked); }); // Function to send flag to Flask server function sendFlagToServer(flag) { socket.emit('streamingclicked_flag', flag); } |
On flask server the Flask server, we need to create
Creating the flask server in Webserver
...
register a handler to react to the "clicked_flag" event.
Code Block |
---|
# register clicked_flag event handler
@self.socketio.on('clicked_flag')
def handle_flag(flag):
if flag:
self.output_message(ButtonClicked(button=flag)) |
If the flag is set to true, a SICMessage
“ButtonClicked” will be sent to the SIC channel as output of this component. This output will be received on a callback in the demo side to determine if the button has been clicked or not.
2. How to render Dialogflow intermediate transcripts dynamically in a text box?
In the html, first add a textarea element inside the body section.
Code Block |
---|
<div>
<textarea id="textbox" rows="4" cols="50"></textarea>
</div> |
In Javascript, we create a function to update the content of the textbox .
Code Block |
---|
// Function to update the textbox
function updateTextbox(text) {
document.getElementById('textbox').value = text;
} |
When a web client receives an “update_textbox” event from the flask server, the event handler is triggered. Then it calls the updateTextbox(text)
function passing the received text
as an argument.
Code Block |
---|
// Event handler for receiving updated text
socket.on('update_textbox', function(text) {
updateTextbox(text);
}); |
On the Flask server, we emit the “update_textbox” event along with the transcript transcribed from Dialogflow.
Code Block |
---|
self.socketio.emit('update_textbox', self.transcript) |
The demo program connects all the components
There are many SIC components we need to connect to in this demo.
Components running on a pepper:
NaoqiMicrophoneSensor
NaoqiTextToSpeechActuator
NaoqiTabletComponent
Components running on a local machine:
WebserverComponent
DialogflowService
...