如何在 Twilio 函数内使用 Node.js 检索 Twilio 传真 PDF 并将其附加到电子邮件?
How do I retrieve the Twilio fax PDF and attach it to an email using Node.js inside a Twilio function?
有什么方法可以使用 Twilio 的无服务器选项来检索之前传真的 PDF 并将其附加到电子邮件中?
我已经通过查看示例在我自己的个人 Web 服务器上的 WordPress PHP 中了解了如何执行此操作。下面是一段 WordPress PHP 代码,它检索使用 Twilio 传真的 PDF,然后将 PDF 作为附件发送电子邮件:
<?php
$mediaurl = $_GET["MediaUrl"];
$path = '/some/path/on/your/web/server/where/to/save/the/PDF';
$attachment = $filename = $path . $_GET["FaxSid"] . '.pdf';
require_once('wp-load.php');
$response = wp_remote_get( $mediaurl, array( 'timeout' => '300', 'stream' => true, 'filename' => $filename ) );
wp_mail( 'somebody@somewhere.com', 'You have a fax', 'See attached PDF', 'From: <someone@someplace.com>', $attachment );
?>
如果有人正在了解这些东西,我将上面的代码保存在我的网络服务器上的 twilio-fax-receive.php
文件中。 运行 每次收到传真时,我都会在 Twilio 上设置一个 TwiML Bin——我称之为 receive-fax
——其中包含以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="https://www.somewhere.com/twilio-fax-receive.php" method="GET"/>
</Response>
然后,在接收传真的传真号码的 "Configure" 页面上,我选择了显示 "A FAX COMES IN" 的 TwiML,然后选择了我的接收传真 TwiML Bin。
但回到我的问题。
我可以在 Twilio 函数中使用 Node.js 复制它吗?还是仅使用 Twilio 而无需我自己的 Web 服务器的其他方式?有没有办法获取 PDF 的内容,用 base64 对其进行编码,并使用 SendGrid 或其他一些服务在 Node.js 中即时附加到电子邮件?
有人有工作示例吗?我已经尝试了很多我在网络上找到的涉及 request.get 和 got.stream 以及管道和缓冲区和 fs 的东西,但无济于事...
我不是开发人员,我觉得我有点不知所措。非常感谢您的帮助。
这里是 Twilio 开发人员布道者。
是的,您可以在 Twilio Function 中使用 Node.js 复制它。以下是使用 SendGrid 发送电子邮件的方法:
- 将
request
添加到您的 Runtime dependencies。我用的版本 2.88.0
- 将以下环境变量添加到您的Functions config:
TO_EMAIL_ADDRESS
: 您要将传真发送到的电子邮件地址。
FROM_EMAIL_ADDRESS
: 您要从中接收传真的电子邮件地址。
SENDGRID_API_KEY
:您的 SendGrid API 键
- 保存配置部分
创建一个新函数并添加以下代码:
const request = require('request');
exports.handler = function(context, event, callback) {
const faxUrl = event.MediaUrl;
const email = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New fax from ${event.From}`,
content: [
{
type: 'text/plain',
value: 'Your fax is attached.'
}
],
attachments: []
};
request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
if (!error && response.statusCode == 200) {
email.attachments.push({
content: body.toString('base64'),
filename: `${event.FaxSid}.pdf`,
type: response.headers['content-type']
});
}
request.post(
{
uri: 'https://api.sendgrid.com/v3/mail/send',
body: email,
auth: {
bearer: context.SENDGRID_API_KEY
},
json: true
},
(error, response, body) => {
if (error) {
return callback(error);
} else {
if (response.statusCode === 202) {
return callback(null, new Twilio.twiml.VoiceResponse());
} else {
return callback(body);
}
}
}
);
});
};
为函数指定路径并保存。
- 将路径作为
action
属性添加到 TwiML bin 中的 <Receive>
元素。
- 发送传真,然后看着它到达您的收件箱。
如果这对您有用,请告诉我,我会在有时间的时候更详细地写下代码的工作原理。
如果您使用的是 Gmail,还有另一种简单的方法。
创建一个 google 应用程序脚本,使用 MailApp 将电子邮件发送到您想要的任何地方。
使用“MediaUrl”参数将其作为附件发送。
function doGet(e) {
var emailAddress = "EMAIL_ADDR_TO_SEND_TO";
var from = e.parameter.From;
var subject = "New Fax from " + from;
var body = "New fax attached";
var fileUrl = e.parameter.MediaUrl;
var file = UrlFetchApp.fetch(fileUrl);
MailApp.sendEmail(emailAddress, subject, body, {
attachments: [file.getAs(MimeType.PDF)]
});
}
将其部署为 public.
的 Web 应用程序
复制 Url 并将其插入 TwiML <Receive/>
标记的 "action"
属性中。
确保将 "method"
设置为 "GET"
TwiML 应该是这样的
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="YOUR_GOOGLE_WEBAPP_URL" method="GET"/>
</Response>
然后将您的 Twilio 编号配置到该 TwiML 文件
有什么方法可以使用 Twilio 的无服务器选项来检索之前传真的 PDF 并将其附加到电子邮件中?
我已经通过查看示例在我自己的个人 Web 服务器上的 WordPress PHP 中了解了如何执行此操作。下面是一段 WordPress PHP 代码,它检索使用 Twilio 传真的 PDF,然后将 PDF 作为附件发送电子邮件:
<?php
$mediaurl = $_GET["MediaUrl"];
$path = '/some/path/on/your/web/server/where/to/save/the/PDF';
$attachment = $filename = $path . $_GET["FaxSid"] . '.pdf';
require_once('wp-load.php');
$response = wp_remote_get( $mediaurl, array( 'timeout' => '300', 'stream' => true, 'filename' => $filename ) );
wp_mail( 'somebody@somewhere.com', 'You have a fax', 'See attached PDF', 'From: <someone@someplace.com>', $attachment );
?>
如果有人正在了解这些东西,我将上面的代码保存在我的网络服务器上的 twilio-fax-receive.php
文件中。 运行 每次收到传真时,我都会在 Twilio 上设置一个 TwiML Bin——我称之为 receive-fax
——其中包含以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="https://www.somewhere.com/twilio-fax-receive.php" method="GET"/>
</Response>
然后,在接收传真的传真号码的 "Configure" 页面上,我选择了显示 "A FAX COMES IN" 的 TwiML,然后选择了我的接收传真 TwiML Bin。
但回到我的问题。
我可以在 Twilio 函数中使用 Node.js 复制它吗?还是仅使用 Twilio 而无需我自己的 Web 服务器的其他方式?有没有办法获取 PDF 的内容,用 base64 对其进行编码,并使用 SendGrid 或其他一些服务在 Node.js 中即时附加到电子邮件?
有人有工作示例吗?我已经尝试了很多我在网络上找到的涉及 request.get 和 got.stream 以及管道和缓冲区和 fs 的东西,但无济于事...
我不是开发人员,我觉得我有点不知所措。非常感谢您的帮助。
这里是 Twilio 开发人员布道者。
是的,您可以在 Twilio Function 中使用 Node.js 复制它。以下是使用 SendGrid 发送电子邮件的方法:
- 将
request
添加到您的 Runtime dependencies。我用的版本2.88.0
- 将以下环境变量添加到您的Functions config:
TO_EMAIL_ADDRESS
: 您要将传真发送到的电子邮件地址。FROM_EMAIL_ADDRESS
: 您要从中接收传真的电子邮件地址。SENDGRID_API_KEY
:您的 SendGrid API 键
- 保存配置部分
创建一个新函数并添加以下代码:
const request = require('request'); exports.handler = function(context, event, callback) { const faxUrl = event.MediaUrl; const email = { personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }], from: { email: context.FROM_EMAIL_ADDRESS }, subject: `New fax from ${event.From}`, content: [ { type: 'text/plain', value: 'Your fax is attached.' } ], attachments: [] }; request.get({ uri: faxUrl, encoding: null }, (error, response, body) => { if (!error && response.statusCode == 200) { email.attachments.push({ content: body.toString('base64'), filename: `${event.FaxSid}.pdf`, type: response.headers['content-type'] }); } request.post( { uri: 'https://api.sendgrid.com/v3/mail/send', body: email, auth: { bearer: context.SENDGRID_API_KEY }, json: true }, (error, response, body) => { if (error) { return callback(error); } else { if (response.statusCode === 202) { return callback(null, new Twilio.twiml.VoiceResponse()); } else { return callback(body); } } } ); }); };
为函数指定路径并保存。
- 将路径作为
action
属性添加到 TwiML bin 中的<Receive>
元素。 - 发送传真,然后看着它到达您的收件箱。
如果这对您有用,请告诉我,我会在有时间的时候更详细地写下代码的工作原理。
如果您使用的是 Gmail,还有另一种简单的方法。
创建一个 google 应用程序脚本,使用 MailApp 将电子邮件发送到您想要的任何地方。 使用“MediaUrl”参数将其作为附件发送。
function doGet(e) {
var emailAddress = "EMAIL_ADDR_TO_SEND_TO";
var from = e.parameter.From;
var subject = "New Fax from " + from;
var body = "New fax attached";
var fileUrl = e.parameter.MediaUrl;
var file = UrlFetchApp.fetch(fileUrl);
MailApp.sendEmail(emailAddress, subject, body, {
attachments: [file.getAs(MimeType.PDF)]
});
}
将其部署为 public.
的 Web 应用程序复制 Url 并将其插入 TwiML <Receive/>
标记的 "action"
属性中。
确保将 "method"
设置为 "GET"
TwiML 应该是这样的
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="YOUR_GOOGLE_WEBAPP_URL" method="GET"/>
</Response>
然后将您的 Twilio 编号配置到该 TwiML 文件