首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从对话框Web演示中获取文本

如何从对话框Web演示中获取文本
EN

Stack Overflow用户
提问于 2019-01-13 18:25:52
回答 1查看 1.5K关注 0票数 0

我试图从Dialog的iframe中提取服务器响应文本

下面是我的代码--目前,输出是null

代码语言:javascript
复制
<!DOCTYPE html>
<html lang=\"en\">
<head><meta name=\"viewport\" content=\"width=device-width, initial- scale=1, user-scalable=no\"/>
<title>Dialog</title>
</head>
<body>
<div align="center">
<iframe
    id="dialog"
    allow="microphone;"
    width="350"
    height="430"
    src="https://console.dialogflow.com/api-client/demo/embedded/xxxxxxxxxxxxxxxxxxxxxxxxxxx">  
</iframe>
</div>
<div align="center">
<script>
     var dialogFrame = document.getElementById("dialog");
     var dialogDocument = dialogFrame.contentWindow.document;
     var dialogResponse = dialogDocument.getElementById("server-response");
     document.write(dialogResponse);
</script>
</div>
</body>
</hmtl>

我希望提取由对话框响应的文本,但结果是null

EN

回答 1

Stack Overflow用户

发布于 2019-01-31 14:47:25

无法从HTML <iframe>元素检索数据,该元素只是一个显示块。可以通过API调用检索数据;在对话框中,您可以使用实事获取这些数据。

检查下面的flow,在app上部署Node应用程序,在默认端点上提供对话流聊天,并在/fulfillment端点上发布实现数据。

( A) 配置实现

注意以下几点:

  • URL:输入HTTPS端点(例如ID].apppot.com/履行)
  • 保留所有其他字段为空
  • 域:为所有域启用web钩子
  • 一旦创建并保存,您仍然需要为您所使用的每个意图(以及它们的后续行动)启用实现。

( B)申请

  • 从控制台在App Engine中创建Node App

然后部署这个应用程序,用您的应用程序替换<iframe>

app.yaml

代码语言:javascript
复制
runtime: nodejs8

package.json

代码语言:javascript
复制
{
  "dependencies": {
    "actions-on-google": "^2.5.0",
    "dialogflow-fulfillment": "^0.5.0",
    "express": "^4.16.4"
  },
  "scripts": {
    "start": "node index.js"
  }
}

index.js

代码语言:javascript
复制
'use strict';

const {WebhookClient} = require('dialogflow-fulfillment');
const express = require('express');
const bodyParser = require('body-parser');
var path = require('path');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

function WebhookProcessing(req, res) {
    const agent = new WebhookClient({request: req, response: res});

    console.log('Dialogflow Request headers: ' + JSON.stringify(req.headers));
    console.log('Dialogflow Request body: ' + JSON.stringify(req.body));

    console.log(JSON.stringify(req.body.queryResult.queryText));
    console.log(JSON.stringify(req.body.queryResult.fulfillmentText));

}

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname + '/index.html'));

});

app.post('/fulfillment', function (req, res) {
    WebhookProcessing(req, res);
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

index.html

代码语言:javascript
复制
<!DOCTYPE html>
<html lang=\"en\">
<head><meta name=\"viewport\" content=\"width=device-width, initial- scale=1, user-scalable=no\"/>
<title>Dialog</title>
</head>
    <body>
        <div align="center">
            <iframe
                allow="microphone;"
                width="350"
                height="430"
                src="https://console.dialogflow.com/api-client/demo/embedded/xxxxxxxxxxxxxxxxxxxxxxxxxxx">  
            </iframe>
        </div>
    </body>
</hmtl>

当用户在聊天中启动会话时,函数WebhookProcessing输出每个请求(用户发送的每条消息)的正文和头的日志,并提取和记录queryText (用户消息)和fulfillmentText (Bot的回复)字段的值。

注意,这只是App集成的一个例子,您还可以考虑部署一个云函数,如果您使用Firebase数据库,那么您可以使用使用内联编辑器并从对话框中部署函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54171882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档