UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

72 lines (57 loc) 2.68 kB
var config, contactUser, escapeSpaces, logger, nodemailer, request, sendEmail, sendSMS, sendSMS_Clickatell; logger = require("winston"); nodemailer = require("nodemailer"); request = require("request"); config = require("./config/config"); config.nodemailer = config.get('nodemailer'); config.smsGateway = config.get('smsGateway'); sendEmail = function(contactAddress, title, messagePlain, messageHTML, callback) { var smtpTransport; logger.info(("Sending email to '" + contactAddress + "' using service ") + (config.nodemailer.service + " - " + config.nodemailer.auth.user)); smtpTransport = nodemailer.createTransport(config.nodemailer); return smtpTransport.sendMail({ from: config.nodemailer.auth.user, to: contactAddress, subject: title, text: messagePlain, html: messageHTML }, function(error, response) { return callback(typeof err !== "undefined" && err !== null ? err : null); }); }; sendSMS = function(contactAddress, message, callback) { if (config.smsGateway.provider === 'clickatell') { return sendSMS_Clickatell(contactAddress, message, callback); } else { return callback("Unknown SMS gateway provider '" + config.smsGateway.provider + "'"); } }; sendSMS_Clickatell = function(contactAddress, message, callback) { logger.info("Sending SMS to '" + contactAddress + "' using Clickatell"); return request(("http://api.clickatell.com/http/sendmsg?api_id=" + config.smsGateway.config.apiID + "&") + ("user=" + config.smsGateway.config.user + "&password=" + config.smsGateway.config.pass + "&") + ("to=" + contactAddress + "&text=" + (escapeSpaces(message))), function(err, response, body) { if (body != null) { logger.info("Received response from Clickatell: " + body); } return callback(err != null ? err : null); }); }; escapeSpaces = function(str) { return str.replace(' ', '+'); }; /* * Send a message to a user using a specific method. Current supported methods are 'email' and 'sms'. * contactAddress should contain an email address if the method is 'email' and an MSISDN if the method is 'sms'. * * The contents of the message should be passed via messagePlain. * messageHTML is optional and is only used by the 'email' method. */ exports.contactUser = contactUser = function(method, contactAddress, title, messagePlain, messageHTML, callback) { if (method === 'email') { return sendEmail(contactAddress, title, messagePlain, messageHTML, callback); } else if (method === 'sms') { return sendSMS(contactAddress, messagePlain, callback); } else { return callback("Unknown contact method '" + method + "'"); } }; //# sourceMappingURL=contact.js.map