Hey All,
I’m working on creating a translation slackbot. Users use the command “/translate word” and it will return the translated word and then prompt the user to save it to mongoDB.
I have this weird bug where when I send the new word to be saved to the db, the function only sends the first word saved. Ex:
/translate word
word: từ in VNese
Saving word as từ.
Then if i translate another word it will say something like
/translate word two
word two is từ hai
Saving word as từ (it won’t send the updated word even though it will display the updated word.
//TODO not getting updated
console.log('original before sending is ' + original);
slapp.action('yesno_callback', 'answer', (msg, value) => {
if (value == 'yes') {
console.log('sending original ' + original);
oombawDB.saveVocab(res, msg, original, translated);
msg.respond(msg.body.response_url, `:thumbsup:`);
} else {
console.log('not saving');
msg.respond(msg.body.response_url, `:thumbsdown:`);
}
});
The console.log before slapp.action displays the updated original but slapp.action only sends the FIRST original word.
Here’s my code in its entirety.
module.exports = function(slapp) {
slapp.command('/translate', (msg, text) => {
// returns current user, if doesn't exist then adds user and returns
oombawDB.checkAddUser(msg).then(currentUser => {
let userObj = currentUser.toObject();
if (userObj.hasOwnProperty('translateTo')) {
//console.log('has property');
translateWord(currentUser, text);
} else {
msg.respond({
"text": "What language would you like to translate to?",
"response_type": "ephemeral",
"attachments": [{
//"text": "Choose a language to translate to",
"fallback": "",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "language_selection",
"actions": [{
"name": "language_choice",
"text": "Pick a language...",
"type": "select",
"options": [{
"text": "Afrikaans",
"value": "af"
},
{
"text": "Albanian",
"value": "sq"
},
{
"text": "Arabic ",
"value": "ar"
},
{
"text": "Azerbaijani",
"value": "az"
},
{
"text": "Basque",
"value": "eu"
},
{
"text": "English",
"value": "en"
},
{
"text": "Vietnamese",
"value": "vi"
}
]
}]
}]
});
slapp.action('language_selection', 'language_choice', (msg, value) => {
msg.respond(msg.body.response_url, ` `);
oombawDB.addUserPref(msg, value).then(response => {
translateWord(response, text);
});
});
} // end else
function translateWord(currentUser, text) {
console.log(text);
var original;
var translated;
translate(text, {
to: currentUser.translateTo
})
.then(res => {
original = text.toLowerCase();
translated = res.text.toLowerCase();
console.log(original + ' is ' + translated);
// console.log(res.text);
// => I speak English
// console.log(res.from.language.iso);
// => nl
msg.respond(original + ': ' + translated);
translate('Do you want to save this?', {
to: currentUser.translateTo
}).then(translatedMessage => {
msg.respond({
text: '',
attachments: [{
text: translatedMessage.text,
fallback: 'Yes or No?',
callback_id: 'yesno_callback',
actions: [{
name: 'answer',
text: ':thumbsup:',
type: 'button',
value: 'yes'
},
{
name: 'answer',
text: ':thumbsdown:',
type: 'button',
value: 'no'
}
]
}]
});
//TODO not getting updated
console.log('original before sending is ' + original);
slapp.action('yesno_callback', 'answer', (msg, value) => {
if (value == 'yes') {
console.log('sending original ' + original);
oombawDB.saveVocab(res, msg, original, translated);
msg.respond(msg.body.response_url, `:thumbsup:`);
} else {
console.log('not saving');
msg.respond(msg.body.response_url, `:thumbsdown:`);
}
});
})
.catch(err => {
console.error(err);
});
});
}
});
});
};