我创建了一个聊天机器人,它会通知用户我的(大)家庭成员以及他们住在哪里。我用 MySQL 创建了一个小型数据库,其中存储了这些数据,并在适当的时候使用 PHP 脚本获取它们,具体取决于用户与聊天机器人的交互。
除了 Default Fallback Intent 之外,我的聊天机器人还包含两个意图。和 Default Welcome Intent :
Names Location_context Names ) 由诸如“谁是约翰·史密斯 (John Smith)?”之类的短语进行训练。并有一个输出上下文(称为 context,持续时间为 10 个问题)。这个问题的一个可能答案是“约翰是我的叔叔。”。第二个意图 ( Location_context ) 由诸如“他住在哪里?”之类的短语进行训练。并有一个输入上下文(来自 Names )。这个问题的一个可能答案是“约翰住在纽约”。Names意图包含两个参数:Location_context意图不包含任何参数。<?php
$dbServername = '******************';
$dbUsername = '******************';
$dbPassword = '******************';
$dbName = '******************';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
// error_reporting(E_ALL);
// ini_set('display_errors', 'on');
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if($method == 'POST'){
$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody);
$action = $json->result->action;
$first_name = $json->result->contexts[0]->parameters->{'given-name'};
$last_name = $json->result->contexts[0]->parameters->{'last-name'};
$lifespan = $json->result->contexts[0]->lifespan;
$sql = "SELECT * FROM family WHERE name LIKE '%$first_name%$last_name%';";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$person = $row;
}
switch ($action) {
case 'Name':
$speech= "$first_name is my" . $person["name"] . ".";
break;
case 'Location':
$speech = "$first_name is living in {$person["location"]}.";
break;
default:
$speech = "Please ask me something more relevant to my family";
break;
}
}
else {
$speech = "Sorry, $first_name $last_name is not a member of my family.";
}
$response = new \stdClass();
$response->speech = $speech;
$response->displayText = $speech;
$response->source = "agent";
echo json_encode($response);
}
else
{
echo "Method not allowed";
}
?>
{
"id": "*****************************",
"timestamp": "2018-04-04T08:26:39.993Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Where is he living"
"action": "Location",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "context",
"parameters": {
"given-name.original": "John",
"last-name.original": "Smith",
"given-name": "John",
"last-name": "Smith"
},
"lifespan": 9
}
],
"metadata": {
"intentId": "*****************************",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 93,
"intentName": "Location_context"
},
"fulfillment": {
"speech": "John is living in New York.”,
"displayText": "John is living in New York.",
"messages": [
{
"type": 0,
"speech": "John is living in New York."
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "*****************************"
}
Talk to my test app 之后)时,我在第一个问题中得到相同的答案,但我得到的是“住在洛杉矶”。对于第二个问题。请注意此答案中的两件事。首先,变量$first_name没有任何值(因为它未设置)并且位置“洛杉矶”是数据库中最后一个家庭成员的位置。因此返回此位置是因为 $first_name和 $last_name在 mysql 查询中没有分配值(因为它们没有设置),并且由于某种原因返回了数据库中最后一个人的位置。$lifespan 中始终为 0(在第一个问题和第二个问题中)并且 $first_name和 $last_name在第二个问题的 json 响应中根本没有设置,即使在 Dialoglow 中它们已设置并且它们包含全名,如上面我发布的 json 响应中所示。 Google Assistant 也返回 actions_capability_screen_output为 $json->result->contexts[0]->name在两个问题中,而显然在 Dialogflow 中 $json->result->contexts[0]->name是 context (上下文的名称)。contexts Google Assistant 中第二个问题中 json 响应的分支似乎是这样的:"contexts": [
{
"name": "actions_capability_screen_output",
"parameters": {},
"lifespan": 0
}
]
contexts DIAlogflow 中第二个问题中 json 响应的分支是:"contexts": [
{
"name": "context",
"parameters": {
"given-name.original": "John",
"last-name.original": "Smith",
"given-name": "John",
"last-name": "Smith"
},
"lifespan": 9
}
]
context并且它不处理与 Dialoglow 所示相同的 json 响应? 最佳答案
除了你的问题,这里还有很多事情要做。让我们试着一点一点地分解它们。
为什么我最初在响应中收到错误?
因为 ini_set('display_errors', 'on');将任何错误发送到标准输出,这是您发送回 Dialogflow 的内容。 Dialogflow 向 Google 发送信息的解析器是严格的,因此额外的输出在这里造成了问题。
我怎样才能看到发生了什么?
你应该使用类似 error_log() 的东西来记录事情。 .这会将您想要的任何内容记录在一个文件中,因此您可以看到来自 Dialogflow 的确切 JSON 以及您认为要发回的确切内容。
您可以将其与类似的东西一起使用
error_log( $request_body );
originalRequest通过 JSON 传递的对象。context与 Actions on Google 的上下文? context[0]未命名为“上下文”。您应该记录整个 context阵列以查看所有类似的东西error_log( $json->result->context );
actions_capability_screen_output它由 Actions on Google 创建,用于表明您正在运行的设备可以显示在屏幕上。它可能还有一个名为 actions_capability_audio_output表示它可以说出结果。它还应该包括一个名为 context 的文件。 ,这是您设置的那个。given-name和 last-name参数设置? context数组并查找与您期望的名称匹配的名称。然后,您可以从那里获取所需的参数。关于php - Google Assistant 出错,而 Dialogflow 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49626687/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee