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.
Add SBT dependency:
libraryDependencies += "com.github.gilbertw1" %% "slack-scala-client" % "0.1.8"The steps to send a notification to your slack channel is :
- 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:
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
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:
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 🙂