Skip to content

Instantly share code, notes, and snippets.

@yyano
Created December 19, 2017 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyano/7c4b2ce15023c6c71ed555c78bcd275e to your computer and use it in GitHub Desktop.
Save yyano/7c4b2ce15023c6c71ed555c78bcd275e to your computer and use it in GitHub Desktop.
Twilio FAX Receive by Twilio Function. FAX受信をTwilioで。
exports.handler = function(context, event, callback) {
console.log("event:",event);
if ("received" != event.FaxStatus) {
console.log("not fax?");
console.log("FaxStatus:" + event.FaxStatus);
console.log("ErrorCode:" + event.ErrorCode);
console.log("From:" + event.From);
//callback(null, "not fax?");
}
else {
sendMailPdf(event.MediaUrl);
postSlack(event.MediaUrl);
}
//callback(null, "finished");
/*** sendmail ***/
function sendMailPdf(url) {
let nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: context.FaxMailSmtpServer,
port: 587,
secure: false,
auth: {
user: context.FaxMailSmtpUser,
pass: context.FaxMailSmtpPass
}
});
let mailOptions = {
from: context.FaxMailFromAddress,
to: context.FaxMailToAddress,
subject: 'Fax received, from:' + event.From,
text: 'Fax received, from:' + event.From,
attachments: [{ // use URL as an attachment
filename: 'fax.pdf',
path: url
}]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
}
else {
console.log('Mail Message sent: %s', info.messageId);
}
});
}
/*** post to slack ***/
function postSlack(urlpdf) {
let rp = require('request-promise');
let fs = require('fs');
let util = require('util');
let optionpdf = {
method: 'GET',
uri: urlpdf,
encoding: null
};
let tmpfile = util.format("/tmp/%s.pdf", Math.floor(10000 * 10000 * Math.random()).toString(16));
console.log("tmpfile:", tmpfile);
rp(optionpdf)
.then(function(parsedBodyA) {
fs.writeFileSync(tmpfile, parsedBodyA);
})
.catch(function(errA) {
console.log("Ae:", errA);
})
.finally(function() {
// pdf download, success
let optionslack = {
method: 'POST',
uri: "https://slack.com/api/files.upload",
formData: {
token: context.FaxSlackAccessToken,
file: fs.createReadStream(tmpfile),
filetype: "application/pdf",
channels: context.FaxSlackChannel,
initial_comment: 'Fax received, from:' + event.From
}
};
rp(optionslack)
.then(function(parsedBodyB) {
console.log("Bs:", parsedBodyB);
})
.catch(function(errB) {
console.log("Be:", errB);
})
.finally(function() {
fs.unlink(tmpfile, function(errC) {
console.log("Bf:", errC);
});
console.log("erase temfile:", tmpfile);
});
});
}
};
  • Twilio FunctionsのConfigurationの設定
    • Credentials
      Enable ACCOUNT_SID and AUTH_TOKEN : OFF
    • Environmental Variables
      • FaxMailSmtpServer …… メール送信時のSMTPサーバのアドレス
      • FaxMailSmtpPass …… メール送信時のSMTPアカウントのID
      • FaxMailSmtpUser …… メール送信時のSMTPアカウントのパスワード
      • FaxMailFromAddress …… メール送信時の送信メールアドレス
      • FaxMailToAddress …… メール送信時の送信メールアドレス
      • FaxSlackAccessToken …… Slack投稿時のAccessToken
      • FaxSlackChannel …… Slack投稿時の#チャンネル
    • Dependencies
      • nodemailer:4.4.0 …… メール送信用に使います
      • request-promise:4.2.2 …… 外部REST APIを呼び出すために使います
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="https://ceaseless-trees-XXXX.twil.io/fax.received" />
</Response>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment