我的机器人中嵌入了 LUIS。如果 LUIS 无法接听或无法理解用户的问题,我希望机器人发送一封电子邮件,请求相关人员提供帮助。
这是我迄今为止尝试过的
const { ComponentDialog, WaterfallDialog, ConfirmPrompt } = require('botbuilder-dialogs');
const { ActivityTypes } = require("botbuilder");
const path = require('path');
require('dotenv').config({
path: path.resolve('.env'),
});
const { ClientSecretCredential } = require("@azure/identity");
const { Client } = require("@microsoft/microsoft-graph-client");
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
let appInsights = require('applicationinsights');
appInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY).start();
const CONFIRM_PROMPT = 'ConfirmPrompt';
const HELP_DIALOG = 'WaterfallDialog'
class HelpDialog extends ComponentDialog {
constructor(id) {
super(id);
this.addDialog(
new WaterfallDialog(HELP_DIALOG ,[
this.getHelp.bind(this),
this.sendEmail.bind(this)
]))
.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.initialDialogId = HELP_DIALOG;
}
async getHelp(stepContext) {
await stepContext.context.sendActivity({ type: ActivityTypes.Typing });
return await stepContext.prompt(CONFIRM_PROMPT, "I'm not sure what you are asking for, would you like me to get extra help?", ['Yes', 'No']);
}
async sendEmail(stepContext) {
if(stepContext.result) {
// Create TokenCredential
const credential = new ClientSecretCredential(process.env.MicrosoftTenantID, process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
// Set scope for TokenCredential
const graphScope = 'https://graph.microsoft.com/.default';
const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: [graphScope] });
// Create client for TokenCredential
const graphClient = Client.initWithMiddleware({
debugLogging: true,
authProvider,
});
// Construct email object
const mail = {
subject: "Unknown Bot Questions",
toRecipients: [
{
emailAddress: {
address: "receiver@outlook.com",
},
}],
body: {
content: `<p>Hello,</p><p>I am having trouble querying the following questions to the Bot.</p><p>I need some assistance for the above queries.</p><p>Thanks</p>`,
contentType: "html",
},
};
//Send Email
await graphClient.api('https://graph.microsoft.com/v1.0/users/<ObjectId-Mailbox>/sendMail')
.post(mail)
.then((res) => {
console.log(res);
})
.catch((res) => {
console.log(res);
});
return await stepContext.endDialog(stepContext.options);
}
else
{
return await stepContext.endDialog(stepContext.options);
}
}
}
module.exports.HelpDialog = HelpDialog;
我在另一个对话框中使用TeamsBotSsoPrompt
和进行身份验证设置,并在我要从中发送电子邮件的对话框中microsoft-graph-client
使用 的实例。graphClient
但我希望从一个共同的电子邮件地址发送电子邮件,而不是接收来自多个用户的电子邮件。