How We Built a Slack Bot for Time Tracking with Ruby on Rails and Vue.js
Tags: showdev, webdev, ruby, vue
Published: 2019-05-01 08:08:40

---
title: How We Built a Slack Bot for Time Tracking with Ruby on Rails and Vue.js
published: true
description: Time tracking is a part of everyday life in any software development project. Today we're going to show you how we built the time tracking tool via Slack bot
tags: showdev, webdev, ruby, vue 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/vuy93sip5odupvqpiq70.jpg
canonical_url: https://www.codica.com/blog/time-tracking-with-slack-bot/
---

*This article was originally published on [Codica Blog](https://www.codica.com/blog/time-tracking-with-slack-bot/).*

In this article, we want to tell you about our internal time-tracking solution or how we simplified massive routine processes.

Now, this time-tracking chatbot gives us an opportunity to verify and keep in check the company’s development workflow and specify each member performance. 

Let’s get started and see how we developed so-called **Timebot** and what advantages it gives to our dev team.
## The challenge
At Codica, we use Slack to deliberate various project issues. And the Slack App provides an opportunity to create a chatbot and thus an idea emerged: Why not to create a time-tracking Slack bot?

Alongside, another part of the reason to build such a tool using Slack was that web solutions are pretty time-consuming. We had an idea to create a more efficient and tailored option for time-tracking. 
## The solution
### Goals
At the very beginning, our primary goal was to meet the following requirements:

* Streamline the tracking process for the team
* Provide comprehensive and effective reports to our customers automatically
* Provide the opportunity to clearly assess the overall workload
* Record each corporate’s employee contribution to the projects

After several discussions, we came to the realization that the following list of features will help us achieve the determined goals: 

* **Time tracking**
* **Transparent reporting system** 
* **Bundling with Trello**
* **Administration panel**

### Development

#### 1. Defining the technology stack

After considering the required functionality, we turned to technical implementation. For development purposes, we have created two applications:
* [Slack bot (Timebot)](https://github.com/codica2/timebot)
* [Interactive Administration panel](https://github.com/codica2/vue-timebot)

We have chosen the following tools for **Slack bot** development:
* <code>Rails 5<c/code>
* <code>Ruby 2.4</code>
* <code>PostgreSQL</code>

And these tools were used during the development of the **Admin panel**:
* <code>Vue</code>
* <code>Vue-router</code>
* <code>Vuex</code>
* <code>Vue-element-admin (admin panel template)</code>
* <code>JWT (JSON Web Token)</code>

#### 2. Bot creation and integration

Firstly, we got through the process of Bot creation at [Slack API](https://my.slack.com/services/new/bot) following the prompts. There, you specify your workspace, define Bot’s name, upload or choose the profile image and get a special API token.  

Then, in order to manage API for our Timebot, we have used [Slack Ruby Client](https://github.com/slack-ruby/slack-ruby-client) to adopt the special interface. 

Thus, we have inserted the Slack API token into the <code>.env</code> file as follows:

```javascript
Slack.configure do |config|
  config.token = ENV['SLACK_API_TOKEN']
End
```

Thus, we have integrated Slack API and now we can configure our Timebot.

#### 3. Creating the list of commands

When the Slack Bot was ready for configuration, we have turned to create the available commands. All of them were created through if..else conditional statements. Here are some commands from the list:


**Commands** | **Description**
--- | ---
Help | print help.
Projects | print all available projects.
find project SEARCH_QUERY | find a specific project.
PROJECT_NAME HOURS:MINUTES COMMENT | log time
/logtime | log time via interactive dialogue. 
edit NEW_DATE(OPTIONAL) TIME_ENTRY_ID HOURS:MINUTES COMMENT | edit an existing time entry.
update(OPTIONAL) DAY.MONTH.YEAR PROJECT_NAME HOURS:MINUTES COMMENT | create an entry for the specific date.
add project PROJECT_NAME | add a new project.
show day/show week/show month | get a report for this day/week/month.
show absence/show absence last year | display absences.
Every team member has a special ID to be identified by the Slack bot. When a user enters a command, the bot checks whether it’s a valid command.

Initially, we’ve defined the <code>Event Handler</code>:

```javascript
class EventHandler
 include Message::Conditions
 include Message::Logger

 attr_reader :client, :data, :sender, :messages, :public_channels

 def initialize(client, data, messages, public_channels)
   @client          = client
   @data            = data
   @public_channels = public_channels
   @sender          = Message::Sender.new
   @messages        = messages
 end

 def handle_message
   return if message_is_not_processable
   user = User.find_by(uid: data.user)
   log_incoming_message(user, data.text)
   if message_is_enter_time
     CreateEntry.call(user, data.text, messages)
   elsif message_is_specify_project
     SpecifyProject.call(user, data.text, messages)
   elsif message_is_request_for_help
     ShowHelp.call(user)
   elsif message_is_remove_entry
     RemoveEntry.call(user, data.text)
   elsif message_is_show_reports
     ShowReport.call(user, data.text)
   elsif message_is_enter_time_for_day
     CreateEntryForDay.call(user, data.text)
   elsif message_is_request_for_project
     ShowProjects.call(user)
...
   else
     DoNotUnderstand.call(user, messages)
   end
 end
End
``` 

Next, we’ve defined <code>Classes</code> for every bot’s command, for example:

```javascript
class AddProject < BaseService
 include ServiceHelper

 attr_reader :user, :text

 def initialize(user, text)
   @user = user
   @text = text
   super()
 end

 def call
   project_name = text.match(Message::Conditions::ADD_PROJECT_REGEXP)[1]
   project = find_project_by_name(project_name)

   if project
     sender.send_message(user, "Project with name #{project.name} already exists.")
     return
   end

   if project_name.length < Project::MINIMUM_PROJECT_NAME_LENGTH
     text = "Project name is too short - must be at least #{Project::MINIMUM_PROJECT_NAME_LENGTH} characters."
     sender.send_message(user, text)
     return
   end

   project = Project.create!(name: project_name)
   sender.send_message(user, "Project with name #{project.name} is created.")
 end
end
```
We have a possiblity to create new commands at any moment.
### Examples of our Timebot workflow

**Time tracking** gives an opportunity to monitor the number of hours spent on a particular task. This data is available on each employee’s timesheet, which can be viewed and edited.

To add a new time record, you need to enter the project name, time spent for a specific task, and add comments to specify the details.

![](https://thepracticaldev.s3.amazonaws.com/i/2prmx3yorwzga2x5z5j4.gif)

**Absences** give an opportunity to track members’ vacation, illnesses, and other reasons for absence.

![](https://thepracticaldev.s3.amazonaws.com/i/0bjf3tdqoh14b0ixzoss.gif)

**Reports** are developed in order to gather particular data for future statistics and analysis generation.

![](https://thepracticaldev.s3.amazonaws.com/i/l3wpzvke5trlcji6e97a.gif)

**Projects** are defined to let the users track, add, and use the project for time logging. 

![](https://thepracticaldev.s3.amazonaws.com/i/tnnec2i5eldi780k0q32.gif)

## What we achieved
To summarize, we have presented you how we developed a SlackBot and we believe this guide will be valuable and useful for you and your business.

Here is the list of some of the features and process improvements that our SlackBot brings:
* Efficient time-tracking tool 
* Tracking both work hours and absences (vacation, sick leave)
* Notifications with a meeting time that pops up in 10 minutes before the meeting
* Overall picture of the performance and velocity of the whole company 

To read the full list of features, please check our article: [Building a Slack Bot for Internal Time Tracking](https://www.codica.com/blog/time-tracking-with-slack-bot/).