Crash course on backend programming for Product Managers//Building a Slack app from scratch — Part 6

Maxim Bassin
7 min readNov 13, 2021

--

OK, this is the last part series where we are actually going to finish this app and make it available for all.

In this final part you will:

  1. Create a new collection in Mongo DB to store user credentials
  2. Using the website app, upon every app installation, store the user credentials in Mongo DB
  3. Using the worker app and the for loop, set the app to go over all the user info and send them a message once a week

Let’s finish this one!!

Push user data to a new collection in MongoDB

(7 steps)

  1. Create a new collection in MongoDB named users

2. Let’s install the npm package for MongoDB (to enable sending info to MongoDB). Enter the following line to the terminal in VSCode and click enter:

npm install mongodb

3. Copy the mongouri environment variable from Heroku’s slackappformedium to Heroku’s slackappwebsiteformedium to enable our service to connect to our database and place there the user information.

Go back to Heroku, choose the slackappformedium app, go to settings, and copy the value under the mongouri:

Now go to Heroku’s slackappwebsiteformedium, and create a new variable there named mongouri, with the value you’ve just copied:

4. Add these lines to the top part of the code to include the MongoDB capabilities, and establish the service that will use the connection string to our database

const MongoClient = require('mongodb').MongoClient
const mongouri = process.env.mongouri
const mongoclientinstance = new MongoClient(mongouri)

5. Store the access token and the user ID, by adding the following lines right after we get the user data. The following code establishes a connection to the slackapp database, and then inserts one doc to the users collection

MongoClient.connect(mongouri, {useUnifiedTopology: true}).then(mongoclientinstance => {let userscollection = mongoclientinstance.db("slackapp").collection("users")doc = {authedUserId: result.data.authed_user.id,access_token: result.data.access_token}userscollection.insertOne(doc)}).catch(error => console.error(error))

6. Save the server.js file, commit and push

7. Install the app again to see if our code indeed pushes the user info into MongoDB

Hooray!

Now we’ll use that data in our Slack app. So once a week, the app will get the list of all user ids and their tokens, find the latest message to send, send it, and update the messages collection for the sent message and find the message that should be sent next.

Retrieve user data from Mongo DB and send a message to each user

(11 steps)

  1. In VSCode, open the app.js file in the SlackApp project

2. Add this to the top part of the code:

let users;// the users parameters will hold the user data

3. Add the following lines to the code right next to where we retrieve the jokes data from MongoDB, to retrieve the users into and store it under the users variable.

//get users datausersCollection = db.collection('users')// set the reference to the messages collection as messagesCollectionusers=await usersCollection.find({}).toArray()// this line will find all the documents from the users collection and store each one as an object in an array

4. Since we will pull the user data from the database, let’s delete this hardcoded user data

and define a slackclient array variable that we’ll use later

let slackclient=[];

5. Using the for loop, we’ll go over the entire users array, and for each user, send the message of the week. Let’s wrap the existing code for sending a message with a for loop. For each loop iteration (every user), we’ll create a new client with the user credential and id.

for (let index=0; index<users.length; index++){
accessToken=users[index].access_token
userid=users[index].authedUserId
slackclient[index] = new WebClient(accessToken, {
// LogLevel can be imported and used to make debugging simpler
logLevel: LogLevel.DEBUG
});
// ID of the channel you want to send the message to
channelId = userid
//THE EXISITNG CODE FOR SENDING A MESSAGE

}

6. Change the result variable to be dynamic instead of constant, by makeing it the type of let instead of const

7. Change the client to slackclient[index] to hold the unique user data in each iteration

8. In MongoDB, in messages collection, set all messages for sent: false, and mark the first one as nextup: true

9. Set the cron job to run in 5 minutes from now. In my case it’s Saturday, 20:50: UTC time

10. Save the app.js file, commit and push

Got the message? AWESOME! That means that we successfully pulled the user info from MongoDB, and we’re ready for the masses.

11. Now, it would be a good idea to adjust the cron job timing, to pick the suitable day in the week and the time (remember the server time is UTC timezone) to send your weekly messages.

(Don’t forget to save the file, commit and push for every change)

Your app is now ready to be installed by others, and they all will get the weekly messages. Just don’t forget to always have enough messages in Mongo DB ahead.

Use the website link to share with the users. You can get it by entering the website app in Heroku and clicking “Open app”

AND WE’RE DONE!

Easy peasy:)

--

--