精确关键字匹配与问题转移
预期结果
- 当用户输入问题(即您是否支持WhatsApp Chatbot Solution?)时,FAQ模块会扫描整个数据源以查看关键字组中是否有完全匹配。
Data Source Example of FAQ Chatbot with Diversion
- 一旦 Stella 将用户输入与 FAQ 条目匹配,聊天机器人将显示相应条目的答案(在这种情况下,文本响应。)。
Example of FAQ Chatbot with Diversion 1
- 如果有多个词条与所有关键字组完全匹配,则聊天机器人将显示所有匹配词条的所有相应问题(转移)。
Example of FAQ Chatbot with Diversion 2
FAQ 数据源格式
FAQ数据源格式请参考一级程序。
动手实践
示例树结构
Tree Structure of FAQ Chatbot with Exact Keyword Match & Diversion
编辑常见问题模块的树节点
在继续之前,您应该已经完成了[具有精确关键字匹配的一级常见问题聊天机器人](/docs/zh-CN/standard-procedures-FAQ-tree-with-exact-keyword-match#getting-hands- 上)。 您可以直接编辑一级树或复制为新树。
编辑FAQ Module树节点“Advanced”中的transformed response。 替换为以下代码(添加了匹配的问题选项显示以进行转移):
return new Promise((resolve, reject) => {
console.log("in FAQ Keyword")
let ans = this.member.botMeta.tempData.faqAns || []
console.log("ans", ans)
if (ans.length > 1) {
const questionList = ans.map((a, i) => {
let t
switch(i) {
case 0:
t = "1️⃣"
break
case 1:
t = "2️⃣"
break
case 2:
t = "3️⃣"
break
case 3:
t = "4️⃣"
break
case 4:
t = "5️⃣"
break
case 5:
t = "6️⃣"
break
case 6:
t = "7️⃣"
break
case 7:
t = "8️⃣"
break
case 8:
t = "9️⃣"
break
case 9:
t = "🔟"
break
case 10:
t = "1️⃣1️⃣"
break
case 11:
t = "1️⃣2️⃣"
break
case 12:
t = "1️⃣3️⃣"
break
case 13:
t = "1️⃣4️⃣"
break
case 14:
t = "1️⃣5️⃣"
break
default:
}
return `${t} ${a["Question"]}`
})
let response = {
type: "TEXT",
text: `Which questions below fit your meaning the best? Please select the option.\n\n${questionList.join("\n")}`,
}
console.log("response", response)
resolve(response)
} else if (ans.length == 1) {
let response = {}
switch (ans[0].Type) {
case "Text":
response.type = "TEXT"
response.text = ans[0].Text
if (ans[0].Preview === true || ans[0].Preview === "TRUE") {
response.preview_url = true
}
break
case "Image":
case "Image_Text":
response.type = "IMAGE"
response.url = ans[0].URL
response.text = ans[0].Caption
break
case "Video":
case "GIF":
case "File":
response.type = "FILE"
response.url = ans[0].URL
response.text = ans[0].Caption
response.filename = ans[0]["File Name"]
break
case "Audio":
response.type = "AUDIO"
response.url = ans[0].URL
break
default:
response = null
break;
}
resolve(response)
} else {
resolve({
type: "TEXT",
text: "Sorry, we don't have the answer for you at the moment. You may try to ask us in another way or reach our support team at hello@sanuker.com."
})
}
}
)
- 用下面的代码创建一个“post-action”(这是为了处理导流逻辑):
return new Promise((resolve, reject) => {
let ans = this.member.botMeta.tempData.faqAns
console.log("if ans exist, save compositeId")
console.log("ans", ans)
console.log("ans.length", this.lodash.size(ans))
this.member.botMeta.tempData.listLength = 0
if (this.lodash.size(ans) > 1) {
this.member.botMeta.nodeCompositeId = this.node.compositeId
this.member.botMeta.tree = this.node.tree
this.member.botMeta.tempData.listLength = this.lodash.size(ans)
}
resolve({
member: this.member,
})
})
创建一个用于转移的树节点
4.创建一个树节点进行导流。
5.在这个树节点中创建一个触发器,条件如下:
Trigger for Diversion
第一个条件 - 输入文本:
this.messageEvent.type === "TEXT"
第二个条件 - 列表长度内的数字:
this.member.botMeta.tempData.listLength ? new RegExp("\\b" + [...Array(this.member.botMeta.tempData.listLength + 1).keys()].slice(1).join("\\b|\\b") + "\\b").test(this.messageEvent.data.text) : false
6、在这个树节点中创建一个pre-action(这是为了处理导流逻辑),代码如下:
return new Promise((resolve) => {
let match = this.messageEvent.data.text.match(new RegExp("\\b" + [...Array(this.member.botMeta.tempData.listLength + 1).keys()].slice(1).join("\\b|\\b") + "\\b"))
let indexStr = match[0]
let index = parseInt(indexStr) - 1
let faqAns = this.member.botMeta.tempData.faqAns
let faq = faqAns[index]
this.member.botMeta.tempData.faq = faq
resolve({ member: this.member })
})
7.在这个树节点的“Advanced”中创建一个transformed响应,用于根据用户选择的转向选项显示聊天机器人的答案,代码如下:
return new Promise(async (resolve, reject) => {
let result = this.member.botMeta.tempData.faq
let response = {}
switch (result.Type) {
case "Text":
response.type = "TEXT"
response.text = result.Text
if (result.Preview === true || result.Preview === "TRUE") {
response.preview_url = true
}
break
case "Image":
case "Image_Text":
response.type = "IMAGE"
response.url = result.URL
response.text = result.Caption
break
case "Video":
case "GIF":
case "File":
response.type = "FILE"
response.url = result.URL
response.text = result.Caption
response.filename = result["File Name"]
break
case "Audio":
response.type = "AUDIO"
response.url = result.URL
break
default:
response = null
break;
}
console.log("response", response)
resolve(response)
})
- 检查您是否可以产生预期结果.