So last time, I was just learning the basics of using mongoDB on my local device, but now I successfully was able to setup and connect to an online mongoDB database via mlab. For this blog, I figure I would share some of the problems I had getting to this point and how I was able to solve them.
Setting up unique users
Mongo DB sets up unique IDs for every object added to the database, the problem is I don’t really know an easy way to access them without being able to identify that object first. So the first thing I had to do was figure out how users should be identified. At first, username made the most sense, but then I thought what if multiple teams install this bot and they have share usernames. Then I decided to add both team names and usernames as identification for a unique user. To get this information, I initially was reading through slack’s documents about user.identity and what not and after a few hours of not knowing how that would work, I came across the fact that slapp api encodes this information in the msg. So I was easily able to get the info I needed like this:
The vocab schema
This was something that took me awhile to think through. I knew I wanted users to be able to translate to multiple languages if necessary, but I wasn’t sure how that data would be organized and separated. Initally, I though every word that was translated would just have its own language tags in one master list. Something like
But after thinking about that, that could get messy really fast. Also, it would force the user to organize the list properly and export them in a way that didn’t mix up multiple languages. After doing some thinking, I figured most flashcard apps and lists are only two languages, source and target. Therefore, I decided to create an array for language lists and the vocab words would be stored in that. That way, a user can easily export one list and immediately upload to the flashcard app of their choice. So my new vocab schema looks like this:
Problem kind of solved?
Connecting to an online database
So connecting to a mongodb isn’t that difficult. All you have to do is type the url of the database. It looks something like
But as you can see, I need to put in the username and password of my admin user. That sounds like a terrible idea if my code is going to be live on github. I was able to ask around chingucentral and got pointed to some information about environment variables. My bot is being hosted by beepboop, so what they do is if you edit your bot.yml to include some variables like this
It’ll show up on beepboop like this
Unfortunately using the variables wasn’t as easy as just saying MONGODB_USER. I sent an email off to beepboop but haven’t heard back yet. I did some googling about slackbots in general and saw another one on github. I did some poking around their bot.yml file and was able to see how they referenced their environment variables. So turns out if you do type process.env before the name of the variable, it works!
Check it out! User data saved online withouth showing off my password haha!
So for next steps, I just need to work out some of the functionality of my bot. I think when a user types “/translate word” for the first time, it should prompt the user with a dropdown list so they can select the target language to translate to. It should then show the translation, and then ask the user if they want to save the word using the language the person just selected. After I get that basic functions working, then I’ll work on exporting the list!
Talk to y’all later!