首页 > 解决方案 > react-native 应用程序中的 Amazon Lex 应查询 SQL 数据库并回答用户问题

问题描述

我已经成功地将 Lex 集成到 react-native 应用程序中。按照以下步骤

https://aws-amplify.github.io/docs/js/interactions

下面是我的 React-native Lex 应用程序:

import React, { Component } from 'react';
import { StyleSheet, Text, SafeAreaView, Alert, StatusBar } from 'react-native';
import Amplify from 'aws-amplify';
import { ChatBot } from 'aws-amplify-react-native';
import voiceLibs from 'aws-amplify-react-native/dist/Interactions/ReactNativeModules'

import awsconfig from './aws-exports';

Amplify.configure(awsconfig);

const styles = StyleSheet.create({
  container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
      paddingTop: StatusBar.currentHeight,
  },
});

export default class App extends Component {

    state = {
        botName: 'SampleBotApp',
        welcomeMessage: 'Welcome, what would you like to do today?',
    };

    constructor(props) {
        super(props);
        this.handleComplete = this.handleComplete.bind(this);
    }

    handleComplete(err, confirmation) {
        if (err) {
        Alert.alert('Error', 'Bot conversation failed', [{ text: 'OK' }]);
        return;
        }

        Alert.alert('Done', JSON.stringify(confirmation, null, 2), [{ text: 'OK' }]);

        this.setState({
        botName: 'SampleBotApp',
        });

        return 'Thank you! what would you like to do next?';
    }

    render() {
        const { botName, showChatBot, welcomeMessage } = this.state;

        return (
        <SafeAreaView style={styles.container}>
            <ChatBot
            botName={botName}
            welcomeMessage={welcomeMessage}
            onComplete={this.handleComplete}
            clearOnComplete={false}
            styles={StyleSheet.create({
                itemMe: {
                color: 'red'
                }
            })}
          voiceEnabled={true}
          voiceLibs={voiceLibs}
          conversationModeOn={true}
            />
        </SafeAreaView>
        );
    }

}

我的要求是从用户那里获得输入,例如“有多少学生选择了英语科目?” 连接到 SQL 数据库查询结果和响应返回给用户说“有 20 名学生选择了英语科目。”。

我真的不知道如何连接到 SQL 数据库并将查询结果定向到 Lex,以便 Lex 给出正确的响应。

我有 POST api 将返回我 JSON 对象,我已经在 C# 中为.Net 项目连接过。但是在本机反应(javascript)中,我如何从 Amazon Lex 读取响应并查询到数据库并取回结果,Lex 将响应用户..

提前致谢....

标签: javascriptamazon-web-servicesreact-nativeaws-lambda

解决方案


推荐阅读