Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions packages/aws-amplify-react/src/Interactions/ChatBot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../AmplifyUI';
import { Input, Button } from '../AmplifyTheme';

import { I18n } from '@aws-amplify/core';
import { I18n, AudioRecorder } from '@aws-amplify/core';
import { Interactions } from '@aws-amplify/interactions';
import { ConsoleLogger as Logger } from '@aws-amplify/core';
import { chatBot } from '../Amplify-UI/data-test-attributes';
Expand Down Expand Up @@ -55,13 +55,11 @@ const STATES = {

const defaultVoiceConfig = {
silenceDetectionConfig: {
time: 2000,
time: 1500,
amplitude: 0.2,
},
};

let audioControl;

export interface IChatBotProps {
botName?: string;
clearOnComplete?: boolean;
Expand Down Expand Up @@ -94,13 +92,14 @@ export interface IChatBotState {

export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {
private listItemsRef: any;
private audioRecorder: AudioRecorder;

constructor(props) {
super(props);
if (this.props.voiceEnabled) {
require('./aws-lex-audio');
// @ts-ignore
audioControl = new global.LexAudio.audioControl();
this.audioRecorder = new AudioRecorder(
this.props.voiceConfig.silenceDetectionConfig
);
}
if (!this.props.textEnabled && this.props.voiceEnabled) {
STATES.INITIAL.MESSAGE = 'Click the mic button';
Expand Down Expand Up @@ -158,22 +157,18 @@ export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {
micButtonDisabled: false,
},
() => {
audioControl.startRecording(
this.onSilenceHandler,
null,
this.props.voiceConfig.silenceDetectionConfig
);
this.audioRecorder.startRecording(this.onSilenceHandler);
}
);
}
}

onSilenceHandler() {
audioControl.stopRecording();
this.audioRecorder.stopRecording();
if (!this.state.continueConversation) {
return;
}
audioControl.exportWAV(blob => {
this.audioRecorder.exportWAV(blob => {
this.setState(
{
currentVoiceState: STATES.SENDING,
Expand Down Expand Up @@ -235,7 +230,7 @@ export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {
return;
}
if (this.state.lexResponse.contentType === 'audio/mpeg') {
audioControl.play(this.state.lexResponse.audioStream, () => {
this.audioRecorder.play(this.state.lexResponse.audioStream, () => {
if (
this.state.lexResponse.dialogState === 'ReadyForFulfillment' ||
this.state.lexResponse.dialogState === 'Fulfilled' ||
Expand All @@ -257,11 +252,7 @@ export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {
micButtonDisabled: false,
},
() => {
audioControl.startRecording(
this.onSilenceHandler,
null,
this.props.voiceConfig.silenceDetectionConfig
);
this.audioRecorder.startRecording(this.onSilenceHandler);
}
);
}
Expand All @@ -288,7 +279,7 @@ export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {
micButtonDisabled: false,
},
() => {
audioControl.clear();
this.audioRecorder.clear();
}
);
}
Expand Down Expand Up @@ -424,7 +415,6 @@ export class ChatBot extends React.Component<IChatBotProps, IChatBotState> {

render() {
const { title, theme, onComplete } = this.props;

return (
<FormSection theme={theme}>
{title && (
Expand Down
Loading