在 Microsoft Bot Framework 中接收和发送附件作为电子邮件
Receive and Send Attachment as Email within Microsoft Bot Framework
我目前正在构建一个能够接收附件并将其保存到本地目录的聊天机器人。
我想了解如何使用相同的附件并通过电子邮件发送它。
async downloadAttachmentAndWrite(attachment) {
// Retrieve the attachment via the attachment's contentUrl.
const url = attachment.contentUrl;
console.log(attachment)
// Local file path for the bot to save the attachment.
const localFileName = path.join(__dirname, attachment.name);
try {
// arraybuffer is necessary for images
const response = await axios.get(url, { responseType: 'arraybuffer' });
console.log('#####')
console.log(response.data)
// If user uploads JSON file, this prevents it from being written as "{"type":"Buffer","data":[123,13,10,32,32,34,108..."
if (response.headers['content-type'] === 'application/json') {
response.data = JSON.parse(response.data, (key, value) => {
return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
});
}
fs.writeFile(localFileName, response.data, (fsError) => {
console.log(localFileName)
console.log(response.data)
if (fsError) {
throw fsError;
}
});
} catch (error) {
console.error(error);
return undefined;
}
// If no error was thrown while writing to disk, return the attachment's name
// and localFilePath for the response back to the user.
return {
fileName: attachment.name,
localPath: localFileName
};
}
这是目前接收并保存到目录的功能,但我如何真正捕获附件并将其发送到另一个功能?
查看 BotBuilder-Samples 存储库中的 24.bot-authentication-msgraph 示例。此示例演示了如何设置机器人代表用户发送电子邮件。
将该示例用作 reference/template,您可以推断出此过程如何为您工作(如果您未使用 MS Graph)。文档 here 解释了如何将文件作为附件包含在电子邮件中。
如果您保留保存文件的位置,您应该能够从本地目录读取文件,并使用上述方法在发送前附加文件。
希望得到帮助。
我目前正在构建一个能够接收附件并将其保存到本地目录的聊天机器人。 我想了解如何使用相同的附件并通过电子邮件发送它。
async downloadAttachmentAndWrite(attachment) {
// Retrieve the attachment via the attachment's contentUrl.
const url = attachment.contentUrl;
console.log(attachment)
// Local file path for the bot to save the attachment.
const localFileName = path.join(__dirname, attachment.name);
try {
// arraybuffer is necessary for images
const response = await axios.get(url, { responseType: 'arraybuffer' });
console.log('#####')
console.log(response.data)
// If user uploads JSON file, this prevents it from being written as "{"type":"Buffer","data":[123,13,10,32,32,34,108..."
if (response.headers['content-type'] === 'application/json') {
response.data = JSON.parse(response.data, (key, value) => {
return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
});
}
fs.writeFile(localFileName, response.data, (fsError) => {
console.log(localFileName)
console.log(response.data)
if (fsError) {
throw fsError;
}
});
} catch (error) {
console.error(error);
return undefined;
}
// If no error was thrown while writing to disk, return the attachment's name
// and localFilePath for the response back to the user.
return {
fileName: attachment.name,
localPath: localFileName
};
}
这是目前接收并保存到目录的功能,但我如何真正捕获附件并将其发送到另一个功能?
查看 BotBuilder-Samples 存储库中的 24.bot-authentication-msgraph 示例。此示例演示了如何设置机器人代表用户发送电子邮件。
将该示例用作 reference/template,您可以推断出此过程如何为您工作(如果您未使用 MS Graph)。文档 here 解释了如何将文件作为附件包含在电子邮件中。
如果您保留保存文件的位置,您应该能够从本地目录读取文件,并使用上述方法在发送前附加文件。
希望得到帮助。