SlackKit is now using .xcframeworks. When building your dependencies with carthage, please specify a platform: carthage bootstrap --use-xcframeworks --platform macos
CocoaPods
Add SlackKit to your Podfile:
pod 'SlackKit'
Usage
To use the library in your project import it:
import SlackKit
The Basics
Create a bot user with an API token:
import SlackKit
let bot = SlackKit()
bot.addRTMBotWithAPIToken("xoxb-SLACK-BOT-TOKEN")
// Register for event notifications
bot.notificationForEvent(.message) { (event, _) in
// Your bot logic here
print(event.message)
}
To respond to message actions, add a RequestRoute with MessageActionMiddleware using your app’s verification token to your SlackKitResponder:
let response = ResponseMiddleware(token: "SLACK_APP_VERIFICATION_TOKEN", response: SKResponse(text: "Hello, world!"))
let actionRoute = MessageActionRoute(action: helloAction, middleware: response)
let actionMiddleware = MessageActionMiddleware(token: "SLACK_APP_VERIFICATION_TOKEN", routes:[actionRoute])
let actions = RequestRoute(path: "/actions", middleware: actionMiddleware)
let responder = SlackKitResponder(routes: [actions])
slackkit.addServer(responder: responder)
OAuth
Slack has many different oauth scopes that can be combined in different ways. If your application does not request the proper OAuth scopes, your API calls will fail.
If you authenticate using OAuth and the Add to Slack or Sign in with Slack buttons this is handled for you.
For local development of things like OAuth, slash commands, and message buttons, you may want to use a tool like ngrok.
Advanced Usage
Don’t need the whole banana? Want more control over the low-level implementation details? Use the extensible frameworks SlackKit is built on:
This library is no longer under active development.
SlackKit
Installation
Swift Package Manager
Add
SlackKit
to yourPackage.swift
When built using Swift Package Manager, SlackKit includes the vapor websocket framework by default which requires libressl.
You can install it with homebrew:
brew install libressl
For additional details, see the SKRTMAPI readme.
Carthage
Add
SlackKit
to yourCartfile
:SlackKit is now using .xcframeworks. When building your dependencies with carthage, please specify a platform:
carthage bootstrap --use-xcframeworks --platform macos
CocoaPods
Add
SlackKit
to yourPodfile
:Usage
To use the library in your project import it:
The Basics
Create a bot user with an API token:
or create a ready-to-launch Slack app with your application’s
Client ID
andClient Secret
:or just make calls to the Slack Web API:
Slash Commands
After configuring your slash command in Slack (you can also provide slash commands as part of a Slack App), create a route, response middleware for that route, and add it to a responder:
When a user enters that slash command, it will hit your configured route and return the response you specified.
Message Buttons
Add message buttons to your responses for additional interactivity.
To send messages with actions, add them to an attachment and send them using the Web API:
To respond to message actions, add a
RequestRoute
withMessageActionMiddleware
using your app’s verification token to yourSlackKitResponder
:OAuth
Slack has many different oauth scopes that can be combined in different ways. If your application does not request the proper OAuth scopes, your API calls will fail.
If you authenticate using OAuth and the Add to Slack or Sign in with Slack buttons this is handled for you.
For local development of things like OAuth, slash commands, and message buttons, you may want to use a tool like ngrok.
Advanced Usage
Don’t need the whole banana? Want more control over the low-level implementation details? Use the extensible frameworks SlackKit is built on:
Examples
You can find the source code for several example applications here.