如何将用户名从 React Native 应用程序发送到 Dialogflow 以供机器人在响应中使用

How to send username from React Native app to Dialogflow for bot to use in responses

我正在使用 React Native 和 dialogflow 构建一个聊天机器人应用程序。我有通用的聊天机器人在工作(下面的代码)。我的下一步是我想将用户名(使用本机反应)存储在他们的设备上(我知道该怎么做)并将此名称发送到 dialogflow,以便机器人可以用用户名响应(不知道如何执行最后一步)。我已经对如何实现这一点做了很多研究,也许我对 dialogflow 太陌生了,但我没能弄清楚如何做到这一点。根据我收集到的信息,我相信我需要从我的 react.js 文件中设置一个实体,然后可以在 dialogflow 中将其作为参数访问。我看到 react native 模块 dialogflow 有一个 setEntity 方法,但是对于 dialogflow_V2,它已经从 /setEntity 移动到 sessions.setEntityType。我对如何在 dialogflow_V2 中实现这一点感到非常困惑(以及“/”与 "sessions." 有何不同)。我注意到当我发送查询请求时,返回了一个具有会话密钥的 java 对象。我是否需要以某种方式使用此会话密钥来使用 setEntityType,如果是这样,这是否不仅适用于特定查询?任何澄清或指导将不胜感激。谢谢。

我的聊天机器人的主屏幕在下面的代码中提供(注意它使用新的功能组件样式,还不像使用 类 那样常见)。要 运行 这个,必须使用 react-navigation 将此屏幕连接到 app.js 文件,并创建一个 env.js 文件(敏感信息),可以从 dialogflow 访问该文件。关于如何使用 类 执行此操作的一个很好的教程在 link 中:https://blog.jscrambler.com/build-a-chatbot-with-dialogflow-and-react-native/

import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import { Dialogflow_V2 } from 'react-native-dialogflow-text'; //correct module for using expo
import {dialogflowconfig} from './env';

const TalkScreen = props => {
  const BOT_USER = {
    _id: 2,
    name: 'FAQ Bot',
    avatar: 'https://i.imgur.com/7k12EPD.png'
  }

  const [messages, setMessages] = useState(
    [
      {
        _id: 1,
        text: `Hi! I am the FAQ bot  from Jscrambler.\n\nHow may I help you with today?`,
        createdAt: new Date(),
        user: BOT_USER
      }
    ]
  );

  useEffect(() => {
    Dialogflow_V2.setConfiguration(
      dialogflowconfig.client_email,
      dialogflowconfig.private_key,
      Dialogflow_V2.LANG_ENGLISH_US,
      dialogflowconfig.project_id
    )
  })

  const sendBotResponse = (text, message) => {
    let msg = {
      _id: messages.length + 1,
      text,
      createdAt: new Date(),
      user: BOT_USER
    };
    let newMessage = GiftedChat.append(messages, message);
    setMessages(GiftedChat.append(newMessage, [msg]));
  }

  const handleGoogleResponse = (result, message) => {
    //let text = result.queryResult.fulfillmentMessages[0].text.text[0];
    let text = result.queryResult.fulfillmentText;
    sendBotResponse(text, message);
  }

  const onSend = (message) => {
      setMessages(GiftedChat.append(messages, message));
      let mesg = message[0].text;
      Dialogflow_V2.requestQuery(
        mesg,
        result => handleGoogleResponse(result,message),
        error => console.log(error)
      )
  }

  // Not currently used in code, but anticipated for sending username to dialogflow
  const entities = [{
    "name":"username",
    //"extend":true,
    //"entries":[
    //    {
    //        "value":"Media Markt",
    //        "synonyms":["Media Markt"]
    //    }
    //]
   }];

  return (
    <View style={styles.container}>
        <Text style={{ fontSize: 15, color: '#aaa', textAlign: 'center' }}>
             Let's catch up!
        </Text>
      <GiftedChat
        messages={messages}
        onSend={messages => onSend(messages)}
        user={{
          _id: 1
        }}
      />
    </View>
  );
}

export const screenOptions = {
    headerTitle: "My Best Friend",
    headerStyle: {
        backgroundColor: '#ffedff',
        shadowColor: 'transparent',
    },
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
});

export default TalkScreen;

实体可能不是您想要的。这些类型用于确定匹配 Intents 中参数的可能值。

相反,您可以传递 fulfillment webhook 可能用作 Context parameter.

的附加值

因此您的代码可能类似于

const contexts = [{
  name: "userinfo",
  lifespan: 1,
  parameters: {
      "name": username
  }
}];
Dialogflow_V2.setContexts(contexts);

Dialogflow_V2.requestQuery(
  mesg,
  result => handleGoogleResponse(result,message),
  error => console.log(error)
)

该文档还讨论了 "permanent context",但不清楚需要在哪里设置它。