Currency Convertor for Commander
// jshint esversion: 9 | |
async function install(pkgs) { | |
pkgs = pkgs.join(' '); | |
return new Promise((resolve, reject) => { | |
const { exec } = require('child_process'); | |
exec(`npm install ${pkgs}`, (err, stdout, stderr) => { | |
if (err) reject(err); | |
else resolve(); | |
}); | |
}); | |
} | |
/** | |
* @description null | |
* @param {ParamsType} params list of command parameters | |
* @param {?string} commandText text message | |
* @param {!object} [secrets = {}] list of secrets | |
* @return {Promise<SlackBodyType>} Response body | |
*/ | |
async function _command(params, commandText, secrets = {}) { | |
const { | |
from, | |
to | |
} = params; | |
let packages = [ 'exchange-rates-api' ]; | |
await install(packages); | |
const { convert } = require('exchange-rates-api'); | |
let amount = await convert(1, from, to); | |
return { | |
response_type: 'in_channel', // or `ephemeral` for private response | |
text: '1 ' + from + ' is equal to ' + amount + ' ' + to | |
}; | |
} | |
/** | |
* @typedef {object} SlackBodyType | |
* @property {string} text | |
* @property {'in_channel'|'ephemeral'} [response_type] | |
*/ | |
const main = async (args) => ({ | |
body: await _command(args.params, args.commandText, args.__secrets || {}).catch(error => ({ | |
response_type: 'ephemeral', | |
text: `Error: ${error.message}` | |
})) | |
}); | |
module.exports = main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment