如何在 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?

查看:25
本文介绍了如何在 Twilio 函数中使用 Node.js 检索 Twilio 传真 PDF 并将其附加到电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 Twilio 的无服务器选项来检索之前传真的 PDF 并将其附加到电子邮件中?

Is there any way I can use Twilio's serverless options to retrieve a PDF that was faxed earlier and attach it to an email?

通过查看示例,我已经学会了如何在我自己的个人 Web 服务器上的 WordPress 中使用 PHP 执行此操作.下面是一段 WordPress PHP 代码,它检索使用 Twilio 传真的 PDF,然后将 PDF 作为附件发送电子邮件:

I've learned how to do this in PHP in WordPress on my own personal web server by looking at examples. Here's a snippet of WordPress PHP code that retrieves a PDF that was faxed using Twilio and then sends an email with the PDF as an attachment:

<?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——其中包含以下代码:

In case someone is learning about these things, I have the above code saved in a twilio-fax-receive.php file on my web server. And to run it every time a fax comes in, I have a TwiML Bin set up on Twilio -- I called it receive-fax -- with this code in it:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Receive action="https://www.somewhere.com/twilio-fax-receive.php" method="GET"/>
</Response>

然后,在接收传真的传真号码的配置"页面上,我选择了 TwiML,其中显示A FAX COMES IN",然后选择了我的接收传真 TwiML Bin.

Then, on the "Configure" page for the fax number that receives faxes, I selected TwiML where it says "A FAX COMES IN" and then selected my receive-fax TwiML Bin.

但回到我的问题.

我可以在 Twilio 函数中使用 Node.js 复制它吗?还是仅使用 Twilio 而没有我自己的 Web 服务器的其他方式?有没有办法获取 PDF 的内容,使用 base64 对其进行编码,然后使用 SendGrid 或 Node.js 中的其他一些服务即时附加到电子邮件中?

Can I replicate that using Node.js inside a Twilio function? Or some other way using only Twilio, without my own web server? Is there a way to get the contents of the PDF, encode it with base64 and attach to an email using SendGrid or some other service on the fly in Node.js?

有人有工作的例子吗?我已经尝试了很多我在网上找到的涉及 request.get 和 got.stream 和管道以及缓冲区和 fs 的东西,但都无济于事...

Does anybody have a working example? I've tried a lot of things I found on the Web that involved request.get and got.stream and pipe and Buffer and fs, but to no avail...

我不是开发人员,而且我认为我已经无法理解了.非常感谢您的帮助.

I am not a developer, and I think I am in way over my head. Your help would be very much appreciated.

推荐答案

Twilio 开发人员布道者在这里.

Twilio developer evangelist here.

是的,您可以在 Twilio 函数.以下是使用 SendGrid 发送电子邮件的方法:

Yes, you can replicate this using Node.js in a Twilio Function. Here's how using SendGrid to send the email:

  1. request 添加到您的运行时依赖项.我使用了 2.88.0
  2. 版本
  3. 将以下环境变量添加到您的函数配置:
    • TO_EMAIL_ADDRESS:您要将传真发送到的电子邮件地址.
    • FROM_EMAIL_ADDRESS:您希望接收传真的电子邮件地址.
    • SENDGRID_API_KEY:您的 SendGrid API 密钥
  1. Add request to your Runtime dependencies. I used version 2.88.0
  2. Add the following environment variables to your Functions config:
    • TO_EMAIL_ADDRESS: the email address you want to deliver faxes to.
    • FROM_EMAIL_ADDRESS: the email address you want to receive faxes from.
    • SENDGRID_API_KEY: Your SendGrid API key

创建一个新函数并添加以下代码:

Create a new function and add the following code:

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);
          }
        }
      }
    );
  });
};

  • 给函数一个路径并保存它.

  • Give the Function a path and save it.

    如果这对您有用,请告诉我,我会在有时间的时候更详细地写下代码的工作原理.

    Let me know if this works for you, I'll write up how the code works in more detail when I get the time.

    这篇关于如何在 Twilio 函数中使用 Node.js 检索 Twilio 传真 PDF 并将其附加到电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆