- 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を呼び出すために使います
- Credentials
Created
December 19, 2017 13:54
-
-
Save yyano/7c4b2ce15023c6c71ed555c78bcd275e to your computer and use it in GitHub Desktop.
Twilio FAX Receive by Twilio Function. FAX受信をTwilioで。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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