choco-bot
Version:
Whatsapp-bot
57 lines (52 loc) • 2.03 kB
JavaScript
function dictionary(remainingMessage, msg){
const word = remainingMessage;
const axios = require('axios');
chat = msg.getChat();
try {
if (!word) {
msg.reply('Please provide a word to look up.');
return;
}
try {
const response = axios.get(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = response.data[0];
if (data) {
let replyMessage = `Word: ${data.word}\n`;
if (data.phonetics && data.phonetics.length > 0) {
replyMessage += 'Phonetics:\n';
data.phonetics.forEach((phonetic) => {
replyMessage += `${phonetic.text}\n`;
});
}
if (data.meanings && data.meanings.length > 0) {
replyMessage += 'Meanings:\n';
data.meanings.forEach((meaning) => {
replyMessage += `${meaning.partOfSpeech}:\n`;
if (meaning.definitions && meaning.definitions.length > 0) {
meaning.definitions.forEach((definition, index) => {
replyMessage += `${index + 1}. Definition: ${definition.definition}\n`;
});
}
});
}
msg.reply(replyMessage);
if (data.phonetics.audio && data.phonetics.audio.trim() !== '') {
console.log(phonetic.audio)
const replyMedia = MessageMedia.fromUrl(phonetic.audio);
chat.sendMessage(replyMedia, { caption: `Pronounciation ${word}` });
}
} else {
msg.reply('No information found for the provided word.');
}
} catch (error) {
console.error('Error fetching word information:', error);
msg.reply('Unable to fetch word information. Please try again later.');
}
} catch (error){
console.log('Error fetching word information:', error);
msg.reply(error);
}
}
module.exports = {
dictionary,
}