在发送给用户之前如何更新来自机器人的答案?
How do I update an answer from the bot prior to it being sent to the user?
我正在开发一个使用 Node.js 版本框架的 QnA Maker 服务的机器人。
我想通过包含动态内容来增强机器人提供的答案。我希望能够用动态生成的内容替换出现在答案中的 [shortcode-thing]
。我有代码可以实现短代码的识别和替换。我迷路的地方是将其添加到对话流程中。
我使用 QnAMakerDialog
class 作为机器人和 QnA Maker 服务之间交互的核心。这样我就可以及时提供多轮体验。
这可以使用中间件吗?如果是这样,我如何识别答案并更新答案的内容,以便将更新后的答案发送给用户?
或者,有没有办法扩展 QnAMakerDialog
来拦截发送答案的动作?
我找到了实现我想要的结果的方法。
首先,我实现了一个 class 作为我的中间件。例如:
class filterAnswer {
/*
* Called each time the bot receives a new request.
*/
async onTurn( context, next ) {
/*
* Called each time a set of activities is sent.
*/
context.onSendActivities( async function( _context, activities, next ) {
// Loop through all of the activities in the stack.
activities.forEach( activity => {
// Only examine messages, ignore other types.
if ( activity.type === "message" ) {
// Ignore a message if it doesn't have any text.
if ( activity.text !== undefined ) {
let fixedText = activity.text;
// Do stuff to the text.
// Assigm the text back to the activity.
activity.text = fixedText;
}
}
} );
// Continuing processing activities by other middleware.
await next();
} );
// Continue processing the request.
await next();
}
}
}
然后我可以像这样将我的自定义中间件添加到中间件列表中:
const adapter = new BotFrameworkAdapter( {
// bot configuration values
} );
// Add custom middleware
adapter.use( new filterAnswer() );
这是基于查看 Transcript Logger and the Spell Check Middleware. As well as the Middleware interface and SendActivitiesHandler 文档等示例。
正如其他人在评论中提到的,我可以在我的 QnAMakerDialog
中实现它。我正在关注 QnA Maker Sample,我认为它使我远离底层的 QnA Maker 交互。特别是我想利用 SDK classes 所做的所有很酷的事情来提供多轮体验。
我正在开发一个使用 Node.js 版本框架的 QnA Maker 服务的机器人。
我想通过包含动态内容来增强机器人提供的答案。我希望能够用动态生成的内容替换出现在答案中的 [shortcode-thing]
。我有代码可以实现短代码的识别和替换。我迷路的地方是将其添加到对话流程中。
我使用 QnAMakerDialog
class 作为机器人和 QnA Maker 服务之间交互的核心。这样我就可以及时提供多轮体验。
这可以使用中间件吗?如果是这样,我如何识别答案并更新答案的内容,以便将更新后的答案发送给用户?
或者,有没有办法扩展 QnAMakerDialog
来拦截发送答案的动作?
我找到了实现我想要的结果的方法。
首先,我实现了一个 class 作为我的中间件。例如:
class filterAnswer {
/*
* Called each time the bot receives a new request.
*/
async onTurn( context, next ) {
/*
* Called each time a set of activities is sent.
*/
context.onSendActivities( async function( _context, activities, next ) {
// Loop through all of the activities in the stack.
activities.forEach( activity => {
// Only examine messages, ignore other types.
if ( activity.type === "message" ) {
// Ignore a message if it doesn't have any text.
if ( activity.text !== undefined ) {
let fixedText = activity.text;
// Do stuff to the text.
// Assigm the text back to the activity.
activity.text = fixedText;
}
}
} );
// Continuing processing activities by other middleware.
await next();
} );
// Continue processing the request.
await next();
}
}
}
然后我可以像这样将我的自定义中间件添加到中间件列表中:
const adapter = new BotFrameworkAdapter( {
// bot configuration values
} );
// Add custom middleware
adapter.use( new filterAnswer() );
这是基于查看 Transcript Logger and the Spell Check Middleware. As well as the Middleware interface and SendActivitiesHandler 文档等示例。
正如其他人在评论中提到的,我可以在我的 QnAMakerDialog
中实现它。我正在关注 QnA Maker Sample,我认为它使我远离底层的 QnA Maker 交互。特别是我想利用 SDK classes 所做的所有很酷的事情来提供多轮体验。