如果可能的话,我恳请您就这个话题提供帮助。
基于自定义问答的我的 Azure 机器人实际上已启动并运行: 根据知识固定问题我得到相应的答案。
这是我在 qnaBotWithMSI.js 文件中使用的实际代码:
const { ActivityHandler } = require('botbuilder');
class QnABotWithMSI extends ActivityHandler {
/**
*
* @param {ConversationState} conversationState
* @param {UserState} userState
* @param {Dialog} dialog
*/
constructor(conversationState, userState, dialog) {
super();
if (!conversationState) throw new Error('[QnABotWithMSI]: Missing parameter. conversationState is required');
if (!userState) throw new Error('[QnABotWithMSI]: Missing parameter. userState is required');
if (!dialog) throw new Error('[QnABotWithMSI]: Missing parameter. dialog is required');
this.conversationState = conversationState;
this.userState = userState;
this.dialog = dialog;
this.dialogState = this.conversationState.createProperty('DialogState');
this.onMessage(async (context, next) => {
console.log('Running dialog with Message Activity.');
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
// By calling next() you ensure that the next BotHandler is run.
await next();
});
// If a new user is added to the conversation:
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
const defaultWelcome = process.env.DefaultWelcomeMessage;
if (defaultWelcome !== '') await context.sendActivity(defaultWelcome);
else await context.sendActivity('Welcome to the QnA Maker');
}
}
await next();
});
}
async run(context) {
await super.run(context);
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}
主要目标是在 Slack 上提出问题并让机器人回复。为此,可以通过 Azure Bot 进行通道集成。 然而,答案不是在同一个相应的消息线程上提供的,而是作为在用于集成的同一频道上发布的新消息提供的,其中已经提出了问题。
如果可以的话,请您建议如何编辑代码,以便在同一个“问题线程”上获得答案,而不是在频道中弹出新消息?
提前感谢您的帮助:)
上面提供的所有详细信息,我实际上没有找到合适的解决方案
为了确保您的 Azure 机器人在提出问题的同一个 Slack 线程中进行回复,您需要修改机器人代码以在传出活动中包含
replyToId
属性:
this.onMessage(async (context, next) => {
console.log('Running dialog with Message Activity.');
await this.dialog.run(context, this.dialogState);
if (context.activity.conversation && context.activity.conversation.id) {
const threadId = context.activity.conversation.id;
context.activity.replyToId = threadId;
}
await next();
});
设置
replyToId
以确保响应作为 Slack 线程的一部分发送:
const response = await this.qnaMaker.getAnswers(context);
if (response.length > 0) {
const replyText = response[0].answer;
const replyActivity = {
type: ActivityTypes.Message,
text: replyText,
replyToId: context.activity.id
};
await context.sendActivity(replyActivity);
} else {
await context.sendActivity({
type: ActivityTypes.Message,
text: 'No answers were found.',
replyToId: context.activity.id
});
}
我参考了此链接,获取有关使用 Azure 自定义问答在机器人服务中回答问题的指南。
下面的代码创建一个机器人,可以使用常见问题解答中的内容回答问题。
机器人使用以下 Azure 认知服务自定义问答服务:
const path = require('path');
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
const restify = require('restify');
const {
ActivityTypes,
CloudAdapter,
ConfigurationBotFrameworkAuthentication
} = require('botbuilder');
const { CustomQABot } = require('./bots/CustomQABot');
const botFrameworkAuthentication = new ConfigurationBotFrameworkAuthentication(process.env);
const adapter = new CloudAdapter(botFrameworkAuthentication);
adapter.onTurnError = async (context, error) => {
const traceActivity = {
type: ActivityTypes.Trace,
timestamp: new Date(),
name: 'onTurnError Trace',
label: 'TurnError',
value: `${ error }`,
valueType: 'https://www.botframework.com/schemas/error'
};
console.error(`\n [onTurnError] unhandled error: ${ error }`);
await context.sendActivity(traceActivity);
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue ');
};
const bot = new CustomQABot();
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
});
server.post('/api/messages', async (req, res) => {
await adapter.process(req, res, (context) => bot.run(context));
});
输出: