salix/back/methods/sms/sendSms.js

32 lines
801 B
JavaScript
Raw Normal View History

const got = require('got');
const isProduction = require('vn-loopback/server/boot/isProduction');
const {models} = require('vn-loopback/server/server');
module.exports = async(destination, message) => {
const smsConfig = await models.SmsConfig.findOne();
const params = {
api_key: smsConfig.apiKey,
messages: [{
from: smsConfig.title,
to: destination,
text: message
}]
};
let response;
try {
if (!isProduction(false))
response = {result: [{status: 'ok'}]};
else {
const jsonTest = {
json: params
};
response = await got.post(smsConfig.uri, jsonTest).json();
}
} catch (e) {
response = e;
}
return response;
};