首页 > 解决方案 > 如何将用户名从 React Native 应用程序发送到 Dialogflow 以供机器人在响应中使用

问题描述

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

我的聊天机器人的主屏幕以下面的代码提供(注意它使用新的功能组件样式,还没有像使用类那样普遍)。要运行它,必须使用 react-navigation 将此屏幕连接到 app.js 文件,并创建一个 env.js 文件(敏感信息),可以从 dialogflow 访问该文件。关于如何使用类执行此操作的一个很好的教程在此链接中: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;

标签: javascriptreactjsreact-nativedialogflow-es

解决方案


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

相反,您可以传递履行 webhook 可能用作Context 参数的其他值。

所以你的代码可能看起来像

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)
)

该文档还谈到了“永久上下文”,但不清楚需要在哪里设置。


推荐阅读