choco-bot
Version:
Whatsapp-bot
43 lines (38 loc) • 1.47 kB
JavaScript
function currency_converter(remainingMessage, msg){
const lol = remainingMessage.split(' ')
const amount = lol[0]
const fromCurrency = lol[1]
const toCurrency = lol[2]
const apiKey22 = 'npiAfgcFu114n7l8JXCBFOnjlWmyOl12';
if (!amount || !fromCurrency || !toCurrency) {
msg.reply('Please provide the amount, source currency, and target currency for the conversion. Example 100 USD NGN to convert 100 usd to ngn');
return;
}
try {
const response = axios.get(`https://api.apilayer.com/currency_data/convert?to=${toCurrency}&from=${fromCurrency}&amount=${amount}`, {
headers: {
'apikey': apiKey22,
'Content-Type': 'application/json',
},
});
const { data } = response;
console.log(data)
if (data.success) {
try {
const exchangeRate = data.result;
const replyMessage = `${amount} ${fromCurrency} is approximately ${exchangeRate} ${toCurrency}`;
msg.reply(replyMessage);
} catch (error){
msg.reply(error);
}
} else {
msg.reply('Unable to fetch exchange rate data. Please try again later.');
}
} catch (error) {
console.error('Error fetching exchange rate data:', error);
msg.reply('Unable to fetch exchange rate data. Please try again later.');
}
}
module.exports = {
currency_converter,
}