修改 Twilio 示例代码以附加语音邮件重新编码并通过电子邮件发送
Modify Twilio sample code to attach voicemail recoding and send by email
来自 Twilio 的以下 post 解释了如何编写和实现用于将录制消息的 link 发送到电子邮件的功能。
https://www.twilio.com/blog/forward-voicemail-recordings-to-email
我想附加到语音信箱录音而不只是发送 link。这是我修改他们的代码的尝试。不幸的是,它导致了 500 错误。我对 NodeJS 不是很了解,所以也许你可以提供帮助。
包括 SendGrid
和 FS
的依赖项,并且在我用 FS
、pathToAttachment
、附件变量和附件数组修改它之前,此函数运行完美。
//Initialize SendGrid Mail Client
const sgMail = require('@sendgrid/mail');
//FileSystem
const fs = require("fs");
// Define Handler function required for all Twilio Functions
exports.handler = function(context, event, callback) {
pathToAttachment = `${event.RecordingUrl}`;
attachment = fs.readFileSync(pathToAttachment).toString("base64");
// Build SG mail request
sgMail.setApiKey(context.SENDGRID_API_SECRET);
// Define message params
const msg = {
to: context.TO_EMAIL_ADDRESS,
from: context.FROM_EMAIL_ADDRESS,
fromname: 'Voicemail',
text: `URL is: ${event.RecordingUrl}`,
subject: `${event.From} (${event.country} / ${event.lang})`,
attachments: [
{
content: attachment,
filename: "Voicemail.wav",
disposition: "attachment"
}
]
};
// Send message
sgMail.send(msg)
.then(response => {
console.log("Neat.")
callback();
})
.catch(err => {
console.log("Not neat.")
callback(err);
});
};
这是您修改 Twilio 博客中的代码的方式:
使用 Studio、Functions 和 SendGrid 将语音邮件录音转发到电子邮件
https://www.twilio.com/blog/forward-voicemail-recordings-to-email
.. 附加 电子邮件记录而不是 link 以及如何添加 from name作为发件人电子邮件地址。
const request = require('request');
const sgMail = require('@sendgrid/mail');
exports.handler = function(context, event, callback) {
const fileUrl = event.RecordingUrl;
// Build SG mail request
sgMail.setApiKey(context.SENDGRID_API_SECRET);
request.get({ uri: fileUrl, encoding: null }, (error, response, body) => {
const msg = {
to: context.TO_EMAIL_ADDRESS,
from: {
"email": context.FROM_EMAIL_ADDRESS,
"name": `My company`
},
text: `Attached voicemail from ${event.From}.`,
subject: `${event.From}`,
attachments: [
{
content: body.toString('base64'),
filename: `MyRecording.wav`,
type: response.headers['content-type']
}
]
};
// Send message
sgMail.send(msg)
.then(response => {
console.log("Neat.")
callback();
})
.catch(err => {
console.log("Not neat.")
callback(err);
});
});
};
这就是你如何修改来自
的代码
来自 Twilio 的以下 post 解释了如何编写和实现用于将录制消息的 link 发送到电子邮件的功能。
https://www.twilio.com/blog/forward-voicemail-recordings-to-email
我想附加到语音信箱录音而不只是发送 link。这是我修改他们的代码的尝试。不幸的是,它导致了 500 错误。我对 NodeJS 不是很了解,所以也许你可以提供帮助。
包括 SendGrid
和 FS
的依赖项,并且在我用 FS
、pathToAttachment
、附件变量和附件数组修改它之前,此函数运行完美。
//Initialize SendGrid Mail Client
const sgMail = require('@sendgrid/mail');
//FileSystem
const fs = require("fs");
// Define Handler function required for all Twilio Functions
exports.handler = function(context, event, callback) {
pathToAttachment = `${event.RecordingUrl}`;
attachment = fs.readFileSync(pathToAttachment).toString("base64");
// Build SG mail request
sgMail.setApiKey(context.SENDGRID_API_SECRET);
// Define message params
const msg = {
to: context.TO_EMAIL_ADDRESS,
from: context.FROM_EMAIL_ADDRESS,
fromname: 'Voicemail',
text: `URL is: ${event.RecordingUrl}`,
subject: `${event.From} (${event.country} / ${event.lang})`,
attachments: [
{
content: attachment,
filename: "Voicemail.wav",
disposition: "attachment"
}
]
};
// Send message
sgMail.send(msg)
.then(response => {
console.log("Neat.")
callback();
})
.catch(err => {
console.log("Not neat.")
callback(err);
});
};
这是您修改 Twilio 博客中的代码的方式:
使用 Studio、Functions 和 SendGrid 将语音邮件录音转发到电子邮件 https://www.twilio.com/blog/forward-voicemail-recordings-to-email
.. 附加 电子邮件记录而不是 link 以及如何添加 from name作为发件人电子邮件地址。
const request = require('request');
const sgMail = require('@sendgrid/mail');
exports.handler = function(context, event, callback) {
const fileUrl = event.RecordingUrl;
// Build SG mail request
sgMail.setApiKey(context.SENDGRID_API_SECRET);
request.get({ uri: fileUrl, encoding: null }, (error, response, body) => {
const msg = {
to: context.TO_EMAIL_ADDRESS,
from: {
"email": context.FROM_EMAIL_ADDRESS,
"name": `My company`
},
text: `Attached voicemail from ${event.From}.`,
subject: `${event.From}`,
attachments: [
{
content: body.toString('base64'),
filename: `MyRecording.wav`,
type: response.headers['content-type']
}
]
};
// Send message
sgMail.send(msg)
.then(response => {
console.log("Neat.")
callback();
})
.catch(err => {
console.log("Not neat.")
callback(err);
});
});
};
这就是你如何修改来自
的代码