1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = exports.ChatBot = void 0;
|
7 |
|
8 | var _react = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _reactNative = require("react-native");
|
11 |
|
12 | var _interactions = _interopRequireDefault(require("@aws-amplify/interactions"));
|
13 |
|
14 | var _awsAmplify = require("aws-amplify");
|
15 |
|
16 | var _AmplifyUI = require("../AmplifyUI");
|
17 |
|
18 | var _core = require("@aws-amplify/core");
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
21 |
|
22 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
23 |
|
24 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
25 |
|
26 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
27 |
|
28 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
29 |
|
30 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
31 |
|
32 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
33 |
|
34 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
35 |
|
36 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
37 |
|
38 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
39 |
|
40 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
41 |
|
42 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
43 |
|
44 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
45 |
|
46 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
47 |
|
48 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
49 |
|
50 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
51 |
|
52 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
53 |
|
54 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
55 |
|
56 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
57 |
|
58 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
59 |
|
60 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
61 |
|
62 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
63 |
|
64 | var Voice;
|
65 | var RNFS;
|
66 | var Sound;
|
67 |
|
68 | var Buffer = require('buffer/').Buffer;
|
69 |
|
70 | var logger = new _core.ConsoleLogger('ChatBot');
|
71 | var styles = {
|
72 | container: {
|
73 | flex: 1,
|
74 | flexDirection: 'column',
|
75 | backgroundColor: '#fff',
|
76 | alignItems: 'center',
|
77 | alignSelf: 'stretch',
|
78 | justifyContent: 'center'
|
79 | },
|
80 | list: {
|
81 | flex: 1,
|
82 | flexDirection: 'column',
|
83 | alignSelf: 'stretch',
|
84 | padding: 5
|
85 | },
|
86 | itemMe: {
|
87 | textAlign: 'right',
|
88 | alignSelf: 'flex-end',
|
89 | padding: 8,
|
90 | margin: 8,
|
91 | backgroundColor: '#CCCCCC',
|
92 | borderRadius: 15,
|
93 | overflow: 'hidden'
|
94 | },
|
95 | itemBot: {
|
96 | textAlign: 'left',
|
97 | alignSelf: 'flex-start',
|
98 | padding: 8,
|
99 | margin: 8,
|
100 | color: 'white',
|
101 | backgroundColor: '#0099FF',
|
102 | borderRadius: 15,
|
103 | overflow: 'hidden'
|
104 | },
|
105 | inputContainer: {
|
106 | flexDirection: 'row'
|
107 | },
|
108 | textInput: {
|
109 | flex: 1
|
110 | },
|
111 | buttonMic: {
|
112 | backgroundColor: '#ffc266'
|
113 | }
|
114 | };
|
115 | var STATES = {
|
116 | INITIAL: 'INITIAL',
|
117 | LISTENING: 'LISTENING',
|
118 | SENDING: 'SENDING',
|
119 | SPEAKING: 'SPEAKING'
|
120 | };
|
121 | var MIC_BUTTON_TEXT = {
|
122 | PASSIVE: '🎤',
|
123 | RECORDING: '🔴'
|
124 | };
|
125 | var timer = null;
|
126 |
|
127 | var ChatBot = function (_Component) {
|
128 | _inherits(ChatBot, _Component);
|
129 |
|
130 | var _super = _createSuper(ChatBot);
|
131 |
|
132 | function ChatBot(props) {
|
133 | var _this;
|
134 |
|
135 | _classCallCheck(this, ChatBot);
|
136 |
|
137 | _this = _super.call(this, props);
|
138 | _this.state = {
|
139 | dialog: [{
|
140 | message: _this.props.welcomeMessage || 'Welcome to Lex',
|
141 | from: 'system'
|
142 | }],
|
143 | inputText: '',
|
144 | inputEditable: true,
|
145 | micText: MIC_BUTTON_TEXT.PASSIVE,
|
146 | voice: false,
|
147 | conversationOngoing: false
|
148 | };
|
149 | _this.listItems = _this.listItems.bind(_assertThisInitialized(_this));
|
150 | _this.submit = _this.submit.bind(_assertThisInitialized(_this));
|
151 | _this.listItemsRef = _react["default"].createRef();
|
152 | _this.reset = _this.reset.bind(_assertThisInitialized(_this));
|
153 | _this.startRecognizing = _this.startRecognizing.bind(_assertThisInitialized(_this));
|
154 | _this.handleMicButton = _this.handleMicButton.bind(_assertThisInitialized(_this));
|
155 |
|
156 | if (_this.props.voiceEnabled) {
|
157 | if (!_this.props.voiceLibs) {
|
158 | throw new Error('Missing voiceLibs for voice interactions');
|
159 | }
|
160 |
|
161 | Voice = _this.props.voiceLibs.Voice;
|
162 | Sound = _this.props.voiceLibs.Sound;
|
163 | RNFS = _this.props.voiceLibs.RNFS;
|
164 |
|
165 | if (!Voice || typeof Voice.start !== 'function' || typeof Voice.stop !== 'function' || typeof Voice.isRecognizing !== 'function') {
|
166 | throw new Error('Missing react-native-voice');
|
167 | }
|
168 |
|
169 | if (!Sound) {
|
170 | throw new Error('Missing react-native-sound');
|
171 | }
|
172 |
|
173 | if (!RNFS || typeof RNFS.exists !== 'function' || typeof RNFS.unlink !== 'function' || typeof RNFS.writeFile !== 'function') {
|
174 | throw new Error('Missing react-native-fs');
|
175 | }
|
176 |
|
177 | Voice.onSpeechStart = _this.onSpeechStart.bind(_assertThisInitialized(_this));
|
178 | Voice.onSpeechEnd = _this.onSpeechEnd.bind(_assertThisInitialized(_this));
|
179 | Voice.onSpeechError = _this.onSpeechError.bind(_assertThisInitialized(_this));
|
180 | Voice.onSpeechResults = _this.onSpeechResults.bind(_assertThisInitialized(_this));
|
181 | }
|
182 |
|
183 | return _this;
|
184 | }
|
185 |
|
186 | _createClass(ChatBot, [{
|
187 | key: "listItems",
|
188 | value: function listItems() {
|
189 | var overrideStyles = this.props.styles;
|
190 | return this.state.dialog.map(function (m, i) {
|
191 | if (m.from === 'me') {
|
192 | return _react["default"].createElement(_reactNative.Text, {
|
193 | key: i,
|
194 | style: [styles.itemMe, overrideStyles.itemMe]
|
195 | }, m.message);
|
196 | } else if (m.from === 'system') {
|
197 | return _react["default"].createElement(_reactNative.Text, {
|
198 | key: i,
|
199 | style: [styles.itemBot, overrideStyles.itemBot]
|
200 | }, m.message);
|
201 | } else {
|
202 | return _react["default"].createElement(_reactNative.Text, {
|
203 | key: i,
|
204 | style: [styles.itemBot, overrideStyles.itemBot]
|
205 | }, m.message);
|
206 | }
|
207 | });
|
208 | }
|
209 | }, {
|
210 | key: "submit",
|
211 | value: function () {
|
212 | var _submit = _asyncToGenerator( regeneratorRuntime.mark(function _callee3(voiceResponse) {
|
213 | var _this2 = this;
|
214 |
|
215 | var response, interactionsMessage, path, data, speech;
|
216 | return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
217 | while (1) {
|
218 | switch (_context3.prev = _context3.next) {
|
219 | case 0:
|
220 | if (this.state.inputText) {
|
221 | _context3.next = 2;
|
222 | break;
|
223 | }
|
224 |
|
225 | return _context3.abrupt("return");
|
226 |
|
227 | case 2:
|
228 | _context3.next = 4;
|
229 | return new Promise(function (resolve) {
|
230 | return _this2.setState({
|
231 | dialog: [].concat(_toConsumableArray(_this2.state.dialog), [{
|
232 | message: _this2.state.inputText,
|
233 | from: 'me'
|
234 | }])
|
235 | }, resolve);
|
236 | });
|
237 |
|
238 | case 4:
|
239 | if (!(voiceResponse === true)) {
|
240 | _context3.next = 11;
|
241 | break;
|
242 | }
|
243 |
|
244 | interactionsMessage = {
|
245 | content: this.state.inputText,
|
246 | options: {
|
247 | messageType: 'text'
|
248 | }
|
249 | };
|
250 | _context3.next = 8;
|
251 | return _interactions["default"].send(this.props.botName, interactionsMessage);
|
252 |
|
253 | case 8:
|
254 | response = _context3.sent;
|
255 | _context3.next = 14;
|
256 | break;
|
257 |
|
258 | case 11:
|
259 | _context3.next = 13;
|
260 | return _interactions["default"].send(this.props.botName, this.state.inputText);
|
261 |
|
262 | case 13:
|
263 | response = _context3.sent;
|
264 |
|
265 | case 14:
|
266 | this.setState({
|
267 | dialog: [].concat(_toConsumableArray(this.state.dialog), [response && response.message && {
|
268 | from: 'bot',
|
269 | message: response.message
|
270 | }]).filter(Boolean),
|
271 | inputText: '',
|
272 | inputEditable: true,
|
273 | micText: MIC_BUTTON_TEXT.PASSIVE
|
274 | }, function () {
|
275 | setTimeout(function () {
|
276 | _this2.listItemsRef.current.scrollToEnd();
|
277 | }, 50);
|
278 | });
|
279 |
|
280 | if (!this.state.voice) {
|
281 | _context3.next = 22;
|
282 | break;
|
283 | }
|
284 |
|
285 | this.setState({
|
286 | voice: false
|
287 | });
|
288 | path = "".concat(RNFS.DocumentDirectoryPath, "/responseAudio.mp3");
|
289 | data = Buffer.from(response.audioStream).toString('base64');
|
290 | _context3.next = 21;
|
291 | return RNFS.writeFile(path, data, 'base64');
|
292 |
|
293 | case 21:
|
294 | speech = new Sound(path, '', function () {
|
295 | var _ref = _asyncToGenerator( regeneratorRuntime.mark(function _callee2(err) {
|
296 | return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
297 | while (1) {
|
298 | switch (_context2.prev = _context2.next) {
|
299 | case 0:
|
300 | if (!err) {
|
301 | speech.play( _asyncToGenerator( regeneratorRuntime.mark(function _callee() {
|
302 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
303 | while (1) {
|
304 | switch (_context.prev = _context.next) {
|
305 | case 0:
|
306 | speech.release();
|
307 | RNFS.exists(path).then(function (res) {
|
308 | if (res) {
|
309 | RNFS.unlink(path);
|
310 | }
|
311 | });
|
312 |
|
313 | if (!(response.dialogState === 'ElicitSlot' && _this2.props.conversationModeOn)) {
|
314 | _context.next = 5;
|
315 | break;
|
316 | }
|
317 |
|
318 | _context.next = 5;
|
319 | return _this2.startRecognizing();
|
320 |
|
321 | case 5:
|
322 | case "end":
|
323 | return _context.stop();
|
324 | }
|
325 | }
|
326 | }, _callee);
|
327 | })));
|
328 | } else {
|
329 | logger.error(err);
|
330 | }
|
331 |
|
332 | case 1:
|
333 | case "end":
|
334 | return _context2.stop();
|
335 | }
|
336 | }
|
337 | }, _callee2);
|
338 | }));
|
339 |
|
340 | return function (_x2) {
|
341 | return _ref.apply(this, arguments);
|
342 | };
|
343 | }());
|
344 |
|
345 | case 22:
|
346 | case "end":
|
347 | return _context3.stop();
|
348 | }
|
349 | }
|
350 | }, _callee3, this);
|
351 | }));
|
352 |
|
353 | function submit(_x) {
|
354 | return _submit.apply(this, arguments);
|
355 | }
|
356 |
|
357 | return submit;
|
358 | }()
|
359 | }, {
|
360 | key: "getOnComplete",
|
361 | value: function getOnComplete(fn) {
|
362 | var _this3 = this;
|
363 |
|
364 | return function () {
|
365 | var clearOnComplete = _this3.props.clearOnComplete;
|
366 | var message = fn.apply(void 0, arguments);
|
367 |
|
368 | _this3.setState({
|
369 | dialog: [].concat(_toConsumableArray(!clearOnComplete && _this3.state.dialog), [message && {
|
370 | from: 'bot',
|
371 | message: message
|
372 | }]).filter(Boolean)
|
373 | }, function () {
|
374 | setTimeout(function () {
|
375 | _this3.listItemsRef.current.scrollToEnd();
|
376 | }, 50);
|
377 | });
|
378 | };
|
379 | }
|
380 | }, {
|
381 | key: "componentDidMount",
|
382 | value: function componentDidMount() {
|
383 | var _this$props = this.props,
|
384 | onComplete = _this$props.onComplete,
|
385 | botName = _this$props.botName;
|
386 |
|
387 | if (onComplete && botName) {
|
388 |
|
389 | _interactions["default"].onComplete(botName, this.getOnComplete(onComplete, this));
|
390 | }
|
391 | }
|
392 | }, {
|
393 | key: "componentDidUpdate",
|
394 | value: function componentDidUpdate(prevProps) {
|
395 | var _this$props2 = this.props,
|
396 | onComplete = _this$props2.onComplete,
|
397 | botName = _this$props2.botName;
|
398 |
|
399 | if (botName !== prevProps.botName || onComplete !== prevProps.onComplete) {
|
400 |
|
401 | _interactions["default"].onComplete(botName, this.getOnComplete(onComplete, this));
|
402 | }
|
403 | }
|
404 | }, {
|
405 | key: "onSpeechStart",
|
406 | value: function onSpeechStart(e) {
|
407 | this.setState({
|
408 | currentConversationState: STATES.LISTENING
|
409 | });
|
410 | }
|
411 | }, {
|
412 | key: "onSpeechEnd",
|
413 | value: function () {
|
414 | var _onSpeechEnd = _asyncToGenerator( regeneratorRuntime.mark(function _callee4(e) {
|
415 | return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
416 | while (1) {
|
417 | switch (_context4.prev = _context4.next) {
|
418 | case 0:
|
419 | timer = null;
|
420 | this.setState({
|
421 | currentConversationState: STATES.SENDING
|
422 | });
|
423 | _context4.next = 4;
|
424 | return this.submit(true);
|
425 |
|
426 | case 4:
|
427 | case "end":
|
428 | return _context4.stop();
|
429 | }
|
430 | }
|
431 | }, _callee4, this);
|
432 | }));
|
433 |
|
434 | function onSpeechEnd(_x3) {
|
435 | return _onSpeechEnd.apply(this, arguments);
|
436 | }
|
437 |
|
438 | return onSpeechEnd;
|
439 | }()
|
440 | }, {
|
441 | key: "onSpeechError",
|
442 | value: function onSpeechError(e) {
|
443 | logger.error(e);
|
444 | this.setState({
|
445 | error: JSON.stringify(e.error)
|
446 | });
|
447 | }
|
448 | }, {
|
449 | key: "onSpeechResults",
|
450 | value: function onSpeechResults(e) {
|
451 | this.setState({
|
452 | inputText: e.value.join(' ')
|
453 | });
|
454 |
|
455 | if (timer !== null) {
|
456 | clearTimeout(timer);
|
457 | }
|
458 |
|
459 | timer = setTimeout( _asyncToGenerator( regeneratorRuntime.mark(function _callee5() {
|
460 | return regeneratorRuntime.wrap(function _callee5$(_context5) {
|
461 | while (1) {
|
462 | switch (_context5.prev = _context5.next) {
|
463 | case 0:
|
464 | _context5.next = 2;
|
465 | return Voice.stop();
|
466 |
|
467 | case 2:
|
468 | case "end":
|
469 | return _context5.stop();
|
470 | }
|
471 | }
|
472 | }, _callee5);
|
473 | })), this.state.silenceDelay);
|
474 | }
|
475 | }, {
|
476 | key: "startRecognizing",
|
477 | value: function () {
|
478 | var _startRecognizing = _asyncToGenerator( regeneratorRuntime.mark(function _callee6() {
|
479 | return regeneratorRuntime.wrap(function _callee6$(_context6) {
|
480 | while (1) {
|
481 | switch (_context6.prev = _context6.next) {
|
482 | case 0:
|
483 | this.setState({
|
484 | inputText: 'Speak into the mic...',
|
485 | inputEditable: false,
|
486 | micText: MIC_BUTTON_TEXT.RECORDING,
|
487 | voice: true
|
488 | });
|
489 |
|
490 | if (this.props.conversationModeOn) {
|
491 | this.setState({
|
492 | conversationOngoing: true
|
493 | });
|
494 | }
|
495 |
|
496 | _context6.prev = 2;
|
497 | _context6.next = 5;
|
498 | return Voice.start('en-US');
|
499 |
|
500 | case 5:
|
501 | _context6.next = 10;
|
502 | break;
|
503 |
|
504 | case 7:
|
505 | _context6.prev = 7;
|
506 | _context6.t0 = _context6["catch"](2);
|
507 | logger.error(_context6.t0);
|
508 |
|
509 | case 10:
|
510 | case "end":
|
511 | return _context6.stop();
|
512 | }
|
513 | }
|
514 | }, _callee6, this, [[2, 7]]);
|
515 | }));
|
516 |
|
517 | function startRecognizing() {
|
518 | return _startRecognizing.apply(this, arguments);
|
519 | }
|
520 |
|
521 | return startRecognizing;
|
522 | }()
|
523 | }, {
|
524 | key: "handleMicButton",
|
525 | value: function () {
|
526 | var _handleMicButton = _asyncToGenerator( regeneratorRuntime.mark(function _callee7() {
|
527 | return regeneratorRuntime.wrap(function _callee7$(_context7) {
|
528 | while (1) {
|
529 | switch (_context7.prev = _context7.next) {
|
530 | case 0:
|
531 | _context7.t0 = this.state.conversationOngoing;
|
532 |
|
533 | if (_context7.t0) {
|
534 | _context7.next = 5;
|
535 | break;
|
536 | }
|
537 |
|
538 | _context7.next = 4;
|
539 | return Voice.isRecognizing();
|
540 |
|
541 | case 4:
|
542 | _context7.t0 = _context7.sent;
|
543 |
|
544 | case 5:
|
545 | if (!_context7.t0) {
|
546 | _context7.next = 10;
|
547 | break;
|
548 | }
|
549 |
|
550 | _context7.next = 8;
|
551 | return this.reset();
|
552 |
|
553 | case 8:
|
554 | _context7.next = 12;
|
555 | break;
|
556 |
|
557 | case 10:
|
558 | _context7.next = 12;
|
559 | return this.startRecognizing();
|
560 |
|
561 | case 12:
|
562 | case "end":
|
563 | return _context7.stop();
|
564 | }
|
565 | }
|
566 | }, _callee7, this);
|
567 | }));
|
568 |
|
569 | function handleMicButton() {
|
570 | return _handleMicButton.apply(this, arguments);
|
571 | }
|
572 |
|
573 | return handleMicButton;
|
574 | }()
|
575 | }, {
|
576 | key: "reset",
|
577 | value: function () {
|
578 | var _reset = _asyncToGenerator( regeneratorRuntime.mark(function _callee8() {
|
579 | return regeneratorRuntime.wrap(function _callee8$(_context8) {
|
580 | while (1) {
|
581 | switch (_context8.prev = _context8.next) {
|
582 | case 0:
|
583 | this.setState({
|
584 | inputText: '',
|
585 | inputEditable: true,
|
586 | micText: MIC_BUTTON_TEXT.PASSIVE,
|
587 | voice: false,
|
588 | conversationOngoing: false
|
589 | });
|
590 | _context8.next = 3;
|
591 | return Voice.stop();
|
592 |
|
593 | case 3:
|
594 | case "end":
|
595 | return _context8.stop();
|
596 | }
|
597 | }
|
598 | }, _callee8, this);
|
599 | }));
|
600 |
|
601 | function reset() {
|
602 | return _reset.apply(this, arguments);
|
603 | }
|
604 |
|
605 | return reset;
|
606 | }()
|
607 | }, {
|
608 | key: "render",
|
609 | value: function render() {
|
610 | var _this4 = this;
|
611 |
|
612 | var overrideStyles = this.props.styles;
|
613 | return _react["default"].createElement(_reactNative.KeyboardAvoidingView, {
|
614 | style: [styles.container, overrideStyles.container],
|
615 | behavior: "padding",
|
616 | enabled: true
|
617 | }, _react["default"].createElement(_reactNative.ScrollView, {
|
618 | ref: this.listItemsRef,
|
619 | style: [styles.list, overrideStyles.list],
|
620 | contentContainerStyle: {
|
621 | flexGrow: 1
|
622 | }
|
623 | }, this.listItems()), _react["default"].createElement(ChatBotInputs, {
|
624 | micText: this.state.micText,
|
625 | voiceEnabled: this.props.voiceEnabled,
|
626 | textEnabled: this.props.textEnabled,
|
627 | styles: styles,
|
628 | overrideStyles: overrideStyles,
|
629 | onChangeText: function onChangeText(inputText) {
|
630 | return _this4.setState({
|
631 | inputText: inputText
|
632 | });
|
633 | },
|
634 | inputText: this.state.inputText,
|
635 | onSubmitEditing: this.submit,
|
636 | editable: this.state.inputEditable,
|
637 | handleMicButton: this.handleMicButton,
|
638 | submit: this.submit
|
639 | }));
|
640 | }
|
641 | }]);
|
642 |
|
643 | return ChatBot;
|
644 | }(_react.Component);
|
645 |
|
646 | exports.ChatBot = ChatBot;
|
647 |
|
648 | function ChatBotInputs(props) {
|
649 | var voiceEnabled = props.voiceEnabled;
|
650 | var textEnabled = props.textEnabled;
|
651 | var styles = props.styles;
|
652 | var overrideStyles = props.overrideStyles;
|
653 | var onChangeText = props.onChangeText;
|
654 | var inputText = props.inputText;
|
655 | var onSubmitEditing = props.onSubmitEditing;
|
656 | var editable = props.editable;
|
657 | var handleMicButton = props.handleMicButton;
|
658 | var micText = props.micText;
|
659 | var submit = props.submit;
|
660 | var placeholder;
|
661 |
|
662 | if (voiceEnabled && textEnabled) {
|
663 |
|
664 | placeholder = 'Type your message or tap 🎤';
|
665 | }
|
666 |
|
667 | if (voiceEnabled && !textEnabled) {
|
668 |
|
669 | placeholder = 'Tap the mic button';
|
670 | editable = false;
|
671 | }
|
672 |
|
673 | if (!voiceEnabled && textEnabled) {
|
674 |
|
675 | placeholder = 'Type your message here';
|
676 | }
|
677 |
|
678 | if (!voiceEnabled && !textEnabled) {
|
679 | return _react["default"].createElement(_reactNative.Text, null, "No Chatbot inputs enabled. Set at least one of voiceEnabled or textEnabled in the props.", ' ');
|
680 | }
|
681 |
|
682 | return _react["default"].createElement(_reactNative.View, {
|
683 | style: [styles.inputContainer, overrideStyles.inputContainer]
|
684 | }, _react["default"].createElement(ChatBotTextInput, {
|
685 | styles: styles,
|
686 | overrideStyles: overrideStyles
|
687 | ,
|
688 | placeholder: _awsAmplify.I18n.get(placeholder),
|
689 | onChangeText: onChangeText,
|
690 | inputText: inputText,
|
691 | returnKeyType: "send",
|
692 | onSubmitEditing: onSubmitEditing,
|
693 | blurOnSubmit: false,
|
694 | editable: editable
|
695 | }), _react["default"].createElement(ChatBotMicButton, {
|
696 | handleMicButton: handleMicButton,
|
697 | styles: styles,
|
698 | overrideStyles: overrideStyles,
|
699 | micText: micText,
|
700 | voiceEnabled: voiceEnabled
|
701 | }), _react["default"].createElement(ChatBotTextButton, {
|
702 | submit: submit,
|
703 | type: "submit",
|
704 | styles: styles,
|
705 | overrideStyles: overrideStyles,
|
706 | text: _awsAmplify.I18n.get('Send'),
|
707 | textEnabled: textEnabled
|
708 | }));
|
709 | }
|
710 |
|
711 | function ChatBotTextInput(props) {
|
712 | var styles = props.styles;
|
713 | var overrideStyles = props.overrideStyles;
|
714 | var onChangeText = props.onChangeText;
|
715 | var inputText = props.inputText;
|
716 | var onSubmitEditing = props.onSubmitEditing;
|
717 | var editable = props.editable;
|
718 | var placeholder = props.placeholder;
|
719 | return _react["default"].createElement(_reactNative.TextInput, {
|
720 | style: [styles.textInput, overrideStyles.textInput],
|
721 | placeholder: _awsAmplify.I18n.get(placeholder),
|
722 | onChangeText: onChangeText,
|
723 | value: inputText,
|
724 | returnKeyType: "send",
|
725 | onSubmitEditing: onSubmitEditing,
|
726 | blurOnSubmit: false,
|
727 | editable: editable
|
728 | });
|
729 | }
|
730 |
|
731 | function ChatBotTextButton(props) {
|
732 | var textEnabled = props.textEnabled;
|
733 | var styles = props.styles;
|
734 | var overrideStyles = props.overrideStyles;
|
735 | var submit = props.submit;
|
736 |
|
737 | if (!textEnabled) {
|
738 | return null;
|
739 | }
|
740 |
|
741 | return _react["default"].createElement(_AmplifyUI.AmplifyButton, {
|
742 | onPress: submit
|
743 | ,
|
744 | type: "submit",
|
745 | style: [styles.button, overrideStyles.button],
|
746 | text: _awsAmplify.I18n.get('Send')
|
747 | });
|
748 | }
|
749 |
|
750 | function ChatBotMicButton(props) {
|
751 | var voiceEnabled = props.voiceEnabled;
|
752 | var styles = props.styles;
|
753 | var overrideStyles = props.overrideStyles;
|
754 | var handleMicButton = props.handleMicButton;
|
755 | var micText = props.micText;
|
756 |
|
757 | if (!voiceEnabled) {
|
758 | return null;
|
759 | }
|
760 |
|
761 | return _react["default"].createElement(_AmplifyUI.AmplifyButton, {
|
762 | onPress: handleMicButton,
|
763 | style: [styles.buttonMic, overrideStyles.buttonMic],
|
764 | text: micText
|
765 | });
|
766 | }
|
767 |
|
768 |
|
769 | ChatBot.defaultProps = {
|
770 | botName: undefined,
|
771 | onComplete: undefined,
|
772 | clearOnComplete: false,
|
773 | styles: {},
|
774 | silenceDelay: 1000,
|
775 | conversationModeOn: false,
|
776 | voiceEnabled: false,
|
777 | textEnabled: true
|
778 | };
|
779 | var _default = ChatBot;
|
780 | exports["default"] = _default; |
\ | No newline at end of file |