javascript - 在 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示例。此示例演示如何设置机器人以代表用户发送电子邮件。
使用该示例作为参考/模板,您可以推断此过程如何为您工作(如果您不使用 MS Graph)。此处的文档解释了如何将文件作为附件包含在电子邮件中。
如果您保留保存文件的位置,您应该能够从本地目录中读取文件,并使用上面引用的方法,在发送前附加文件。
希望得到帮助。
推荐阅读
- javascript - 在滚动位置添加一个类
- r - R中是否有一个函数可以让我创建一个新的数据框,其中包含第一个数据框中的重复值?
- javascript - 从一个角落到另一个角落的对角线动画
- jenkins - 如何让 Jenkins 从远程根目录执行管道步骤?
- java - 如何将@Transactional 与@KafkaListener 一起使用?
- mysql - Order by multiple columns not working as expected
- angular - 角度测试:模拟包含 blob 的 HttpResponse
- c# - xamarin,如何将内容顺利上传到 StackLayout
- java - Android Gradle jar 生成
- python - Python For 循环查询