An asynchronous Scala library to interact with the Slack API as of June 2018.
Add this dependency in your build.sbt
libraryDependencies += "com.lunatech" % "scala-slack-client" % "0.2.3"
- Create an app with slack here.
- Add permissions to your bot by going to OAuth & Permissions tab To run the Hello world code below add chat:write:bot in permissions scope.
- Go to Install App tab and install your bot to your workspace.
- Copy the OAuth Access Token access token, you will need it in your app to control the bot.
- Put the code below in your scala project and your bot should send an "Hello, world!" in your channel #General
val slackClient = SlackClient(<Bot token>)
slackClient.postMessage(ChatMessage("#general", "Hello, Wold!"))
To process the response of your bot features you can use our Parser.
SlashCommands :
Parser.slashCommand(body: Map[String, Seq[String]]): Try[SlashCommandPayload]
Interactive Components :
Parser.getPayload(body: Map[String, Seq[String]]): Try[Payload]
A slack bot can send two type of message : In channel and Ephemeral.
An ephemeral message can only be viewed by one User.
Our library uses postMessage and postEphemeral to send messages.
We provide builders to make your message easier to write.
If you want to write a message with buttons you can write it like this. (See slack documention for more information)
val message = ChatMessage(channel = "#general", text = "this is a message with ChatMessage")
.addAttachment(AttachmentField(fallback = "update your API", callback_id = "buttons")
.addAction(Button(name = "PrimaryButton", text = "Primary button").asPrimaryButton))
.addAction(Button(name = "DefaultButton", text = "Default button"))
.addAction(Button(name = "DangerButton", text = "DangerButton").asDangerButton.withConfirmation("Are you sure"))
.withPretext("Click one of these button")
)
slackClient.postMessage(message)
All methods can be used the same way.
MIT License
Copyright (c) 2018 Lunatech.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.