Building a Slackbot 7: Getting back into it


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

  1. Check if the user exists / if not create user
  2. Check if the user has a language preference
    1. If yes, continue with translation
    2. If no, ask user which language it would like translated to
  3. Translate the text
    1. Prompt to save
    2. Prompt to translate into another language
    3. 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.