Last active
March 31, 2020 08:03
-
-
Save niks3089/14848e80ea837791ec1f84710d06b03b to your computer and use it in GitHub Desktop.
Currency Convertor for Commander
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
// 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