Introduction to slack-scala-client

slack-scala-client
Reading Time: 3 minutes

Here, we are going to see how we can communicate on slack with the help of slack-scala-client.

What is Slack?

A messaging app for teams. Slack’s channels help you focus by enabling you to separate messages, discussions and notifications by purpose, department or topic.

However, if you need privacy, Slack provides that as well with invite-only channels.

Slack integration with our application

Integration is what takes Slack from a normal online instant messaging system to a solution that enables you to centralize all your notifications, from sales to tech support, social media and more, into one searchable place where your team can discuss and take action on each.

How Do I Use Slack for My Team?

As a simple instant messaging system, Slack has all the collaboration features you need in place. It’s when you throw in integration that things really get exciting. Imagine your:

  • Marketing team getting an alert in Slack when your company is mentioned on social media;
  • Support group receiving notifications when a Zendesk ticket is created;
  • Sales manager seeing every point-of-sale transaction made;

and much more.

For this we will be using scala-slack-client.

What is slack-scala-client?

A Scala library for interacting with the Slack API and real time messaging interface.

Installation:

Add SBT dependency:

libraryDependencies += "com.github.gilbertw1" %% "slack-scala-client" % "0.1.8"
The steps to send a notification to your slack channel is :

  1. Create and regenerate an API token for your slack team.

What is a token and why do we need it?

Authentication tokens are password-like strings that users can generate that allow bots, scripts, or other programs to integrate with their Slack team.

Most of Slack’s customers will never even see an authentication token, let alone run the risk of accidentally posting it online. Remember, anyone who has access to your authentication token can perform whatever actions were scoped for that token. In other words, it can be exactly like sharing your Slack password on the internet. So never share your token in any form.

You can visit the following link to generate an access token:

Generate slack token

Once you have generated your token, use it to create the SlackApiClient.

API Client Usage

There are two different API clients, one exposing an asynchronous interface and the other exposing a synchronous interface. They can be imported from the slack.api package:

import slack.api.SlackApiClient          // Async
import slack.api.BlockingSlackApiClient  // Blocking

But here we will be using the first version.

Creating client instance:

Creating an instance of either client simply requires passing in a Slack API token:

val token = "<Your Token Here>"
 val client = SlackApiClient(token)

The async client return futures as the result of each of its API functions.

Once you have created the slackApiClient instance, use it to list the available channels for your team.

val res = client.listChannels() // => Future[Seq[Channel]]

This method returns all the public channels created on your team and hence you can communicate to one of your public channels via this SlackApi. Once all the public channels are obtained, you can perform the desired operation you want to perform on your channel.

For example, consider the following method call :

def getChannelId(channelName: String) = Future[Option[String]]

here we will supply the channel name where we want to perform the operation and get the id of the channel and then further use it in the following call:

client.postChatMessage(channelId, msgBody) 

where

channelId is the String id of the channel and

msgBody is the String message you want to post to your channel.

Here is a very simple example to send message on a channel. Have a look and explore what all you can do with your channel 🙂

References:


KNOLDUS-advt-sticker

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

3 thoughts on “Introduction to slack-scala-client3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading