We just finished moving The Lab to a new location so I had to take a break from working on the bot (probably about a month now?). Getting back into the slackbot was kind of difficult. I found myself out of practicing and forgetting what I was working on and how to use the various APIs.
My task for this time was streamlining the translation process with the bot. My goal is to have a user type the command “/translate hello” and the slackbot will
- Check if the user exists / if not create user
Check if the user has a language preference
- If yes, continue with translation
- If no, ask user which language it would like translated to
Translate the text
- Prompt to save
- Prompt to translate into another language
- View list
I already had the check user function working, but I didn’t have the language preference working or translating via language preference selection.
One of the first problems I ran in to was forgetting to save my changes to mongoDB. That was a quick fix by just adding .save() to my currentUser.
The second problem, and the one that took me awhile to figure out was the “hasOwnProperty” was not working as I imagined. If my user already has the “translateTo” property, then the slackbot should automatically translate the text. The problem was that even though my user had that property it wouldn’t detect it. I tried reading through some of the docs about hasOwnProperty but couldn’t figure out why it wasn’t working.
So CHINGUS to the rescue. I mentioned that I joined a chingu coding cohort before, as part of the group there are just a bunch of people around willing to help.
So I jumped in the #help-with-code channel asked my question, and a fellow member responded with this link.
With that problem fixed, I was able to update my translate code to account for the user language preference and done!
My problem right now is with new Users, my bot won’t finish adding a new user before checking if the user has a language preference. So now I need to learn more about asynchronous functions and how to control when things happen.
Check it out. You can see how I have to type the command twice. The first time it creates the user. The second time it runs the language preference.