Slack bot for slack channel and Harvest Forecast App
Tags: slack, forecast, node, harvest
Published: 2020-10-14 13:25:52

Purpose of this project is to fetch the details from the Harvest Forecast app and automate the details to sales and consulting teams to see who are allocated to which project before making daily call. It helps to include only to whom the allocation is necessary which reduces the call time and use the time and resources effectively.

Creating a slack bot for a slack channel using the harvest forecast app. Before getting into the details of creating slack bot would like to give a foreground about the harvest forecast app.

Harvest Forecast app can be accessed using forecast-api which is an npm package. To get desired results as per requirement we need to understand that Harvest Forecast is like a database with tables. Will show you later how to make changes to the forecast-api and embed in the docker before creating your docker image if you are using docker.

Now, let's jump into the prerequisites to develop slack bot.

1. Slack account 
2. Forecast account
3. NodeJs

_Note_: If you don't have slack account, you can [download](https://slack.com/intl/en-de/downloads/mac?geocode=en-de). Set up your account and create a workspace.

###__1. Creating slack  Channel, slack App  and configuration__: 

__Step1__: Once you have a slack workspace, now let's create a slack channel. In your slack workspace click on "+" icon next to channels as shown in the image below.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mpbrziht4eizc7kbd8xj.png)

On click you will get a drop down 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/49nq19ek4oxtvlsg5831.png)
click on "create channel", give name and description as desire for your channel.

__Step2__: Now, as you have a dedicated channel, let's create a slack bot to communicate with the external api and push data into slack channel.

Let's see how to build a slack bot, we have [slack-api](https://api.slack.com) for slack to do different integrations and apps to build. There is lot of  scope and information to dive in, let me help you with building slack bot easily.

Go to [creating slack app url](https://api.slack.com/start/overview#creating) and scroll down to creating and managing slack app section.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kqwy4kwwe5034rcc41b1.png)

On click green button "Create a Slack app" you will be redirected to pop up as shown in the image below, Fill in your desired App name and select the workspace name where you have created the slack channel in step1.



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nj7pkl53v5a4x20hvlmp.png)


__Step3__: Now, You need to configure your slack app by adding features and functionality. Once you create slack app, you will be redirected to page with "Basic Information" which has many sections.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ph6xvj314wxuei5mi524.png)

For forecast data communication to slack, we need to allow the external sources into the slack, hence enable the webhooks. click on "Incoming Webhooks" and you will be redirected to page as show below. 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jbw65lgl6hj59yo2dsrx.png)

Now, enable the webhooks and you can see  some curl script down the page once it finishes reload.

Go back to Basic Information by clicking on the menu in left panel, where you will see "Basic Information" Page.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0q8ra4zwgm3jiunr3s2t.png)

As we enable the *Incoming webhooks*, we see "Bots" and "Permissions" are been enabled in the image below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0lmrwd0ht2n9rtr1lpko.png)

Now, click on Bots and it redirects to "App Home", enable Bot to show as online always. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4n2m7gptupdv0az66xkk.png)

then, In "Basic Information" page you will see  next section  "Install App in your workspace", click on it and select the dedicated channel that was created in Step1.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dw74zwc0bnpykttn3g1m.png)

Now, Check your slack app, you can see the slack bot that was created for slack channel under Apps Section,

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hjiqltb6jix5hoc8rtg7.png)

Now, We need to add the scope of the slackApp, In "Basic Information" page  under  Add  features and functionality we have "Permissions", On click it redirects to OAuth&Permissions page , you can also navigate to this page from the left panel under Features.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n3lcqr99587dc1j42lgd.png) or ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0dsjqhws2h961n6fvfq6.png)

In OAuth&Permissions  section we need to enable permissions under Scopes, We have here  two types of Scopes:
 
   1.Bot token scopes:  select app_mentions:read which enables to view messages directly mention  @AppName in conversation that App in 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/28zrq4nx8802lidm37co.png)
 
   2.User Token Scopes: select channels:write option which manages a user's public channels and create new ones on a user's behalf.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9i7orritgv7ehddic14d.png)

For now,  we  are done with the setting up slack bot.



*Note*:  Please check the next post for configuring Harvest Forecast  App