public void onMessage()

in src/main/java/com/amazonaws/lex/twilio/sample/server/AudioStream.java [84:141]


    public void onMessage(Message message) {
        //LOG.info("message ..." + message);
        if (message.eventType().equals(MessageType.CONNECTED)) {
            // first message, does not contain anything useful
            // apart from some meta data.
        } else if (message.eventType().equals(MessageType.START)) {
            //going to start getting media, this message contains
            // stream id, account id, call id. remember to update them
            // later
            StartMessage startMessage = message.asStartMessage();
            LOG.info("got a start message from twilio:" + startMessage);

            CallIdentifier callIdentifier = startMessage.getCallIdentifier();
            this.twilioCallOperator = new TwilioCallOperator(callIdentifier, session);
            try {
                this.botConversation = new LexBidirectionalStreamingClient().startConversation(twilioCallOperator);
            } catch (URISyntaxException e) {
                LOG.error(e);
            }
        } else if (message.eventType().equals(MessageType.MEDIA)) {
            // contains audio data, decode for inbound audio
            // and send it to bot
            MediaMessage mediaMessage = message.asMediaMessage();

            byte[] uLawEncodedByte = mediaMessage.getDecodedPayload();
            byte[] uncompressedBytes = DecompressInputStream.decompressULawBytes(uLawEncodedByte);
            byte[] copiedBytes = Arrays.copyOf(uncompressedBytes, uncompressedBytes.length);
            //might need to split into smaller events of max size 320, if server throws an error.
            this.botConversation.writeUserInputAudio(ByteBuffer.wrap(uncompressedBytes));

            // uncomment this to keep adding incoming bytes to memory as well.
            // this is useful to captures raw audio that Twilio sends to this application
            // see counterpart method (persistBytesToDisk)

            //persistBytesInMemory(mediaMessage.getStreamSid(), copiedBytes);
        } else if (message.eventType().equals(MessageType.STOP)) {
            StopMessage stopMessage = message.asStopMessage();
            LOG.info("got a stop message from twilio:" + stopMessage);

            this.botConversation.stopConversation();

            //persistBytesToDisk(stopMessage.getCallIdentifier().getStreamSid());

        } else if (message.eventType().equals(MessageType.MARK)) {
            MarkMessage markMessage = message.asMarkMessage();
            LOG.info("got a mark message from twilio:" + markMessage);

            if (this.twilioCallOperator.getCurrentPlaybackLabel().isPresent() && this.twilioCallOperator.getCurrentPlaybackLabel().get().equals(markMessage.getMarkName())) {
                botConversation.informPlaybackFinished();
            }
            if (botConversation.isConversationStopped()) {
                // this notification is for  message for which dialog state is closed, and has played back to user.
                // by this time, we have already stopped sending events to bot (events publisher has been stopped)
                // only pending this is to hangup the ongoing call.
                twilioCallOperator.hangUp(false);
            }
        }
    }