[英]How to send email from Microsoft Teams Bot using Microsoft Graph Client as a different user?
I have LUIS embedded with my bot.我的机器人嵌入了 LUIS。 If the LUIS cannot pick up or cannot understand the user's questions, I want the bot to send an email requesting for help from the concerned person.
如果 LUIS 无法接听或无法理解用户的问题,我希望机器人发送一封电子邮件,请求相关人员提供帮助。
This is what I have tried so far这是我迄今为止尝试过的
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;
I have authentication setup using TeamsBotSsoPrompt
and microsoft-graph-client
in another dialog and using the instance of graphClient
in the dialog I want to send the email from.我在另一个对话框中使用
TeamsBotSsoPrompt
和microsoft-graph-client
进行了身份验证设置, graphClient
在我想从中发送电子邮件的对话框中使用了graphClient
实例。 But I want the email to be sent from a common email address rather than the receiver receiving email from multiple users.但我希望电子邮件从一个公共电子邮件地址发送,而不是接收者从多个用户接收电子邮件。
To send an email from a common email address, you will need to first create an application registration in your Azure AD.若要从常用电子邮件地址发送电子邮件,首先需要在 Azure AD 中创建应用程序注册。
In the API permissions tab, select Microsoft Graph , then Application permissions and then add the Mail.Send permission.在API 权限选项卡中,选择Microsoft Graph ,然后选择应用程序权限,然后添加Mail.Send权限。
Note down the client ID of your app reg and then create a client secret by clicking Certificates and secrets , create a secret and then note that down.记下您的应用程序注册的客户端 ID,然后通过单击Certificates and secrets创建一个客户端机密,创建一个机密,然后记下。
I'm not sure how're your storing and retrieving client ID, secrets, keys etc. but in my environment I'm using the env file and I've also installed the @azure/identity package as well, so mine looks a bit like this.我不确定您如何存储和检索客户端 ID、机密、密钥等,但在我的环境中,我使用的是 env 文件,并且还安装了@azure/identity包,所以我的看起来像有点像这样。
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");
// 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,
});
With that in place, I retrieved the object ID of the user/mailbox I want to send the email from and make the graph call to send the email.有了这个,我检索了我想从中发送电子邮件的用户/邮箱的对象 ID,并进行图形调用以发送电子邮件。
await graphClient.api('https://graph.microsoft.com/v1.0/users/4ae760e0-8040-4cf1-a630-941c1240f7b0/sendMail')
.post(mail)
.then((res) => {
console.log(res);
})
.catch((res) => {
console.log(res);
});
I imagine there are a multitude of better ways of achieving this, but this works for me.我想有很多更好的方法可以实现这一点,但这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.