Swiftpack.co - Package - pvzig/SlackKit

SlackKit

Build Status

Swift Version Plaforms License MIT SwiftPM compatible Carthage compatible CocoaPods compatible

SlackKit: Slack Apps in Swift

Description

SlackKit makes it easy to build Slack apps in Swift.

It's intended to expose all of the functionality of Slack's Real Time Messaging API as well as the web APIs that are accessible to bot users. SlackKit also supports Slack’s OAuth 2.0 flow including the Add to Slack and Sign in with Slack buttons, incoming webhooks, slash commands, and message buttons.

Installation

Swift Package Manager

Add SlackKit to your Package.swift

let package = Package(
	dependencies: [
		.package(url: "https://github.com/pvzig/SlackKit.git", .upToNextMinor(from: "4.3.0"))
	]
)

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 your Cartfile:

github "pvzig/SlackKit"

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)
}

or create a ready-to-launch Slack app with your application’s Client ID and Client Secret:

import SlackKit

let bot = SlackKit()
let oauthConfig = OAuthConfig(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
bot.addServer(oauth: oauthConfig)

or just make calls to the Slack Web API:

import SlackKit

let bot = SlackKit()
bot.addWebAPIAccessWithToken("xoxb-SLACK-BOT-TOKEN")
bot.webAPI?.authenticationTest(success: { (success) in
	print(success)
}, failure: nil)

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:

let slackkit = SlackKit()
let middleware = ResponseMiddleware(token: "SLASH_COMMAND_TOKEN", response: SKResponse(text: "👋"))
let route = RequestRoute(path: "/hello", middleware: middleware)
let responder = SlackKitResponder(routes: [route])
slackkit.addServer(responder: 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:

let helloAction = Action(name: "hello", text: "🌎")
let attachment = Attachment(fallback: "Hello World", title: "Welcome to SlackKit", callbackID: "hello_world", actions: [helloAction])
slackkit.webAPI?.sendMessage(channel: "CXXXXXX", text: "", attachments: [attachment], success: nil, failure: nil)

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:

| Framework | Description | | ------------- |------------- | | SKClient | Write your own client implementation| | SKRTMAPI | Connect to the Slack RTM API| | SKServer | Spin up a server for a Slack app| | SKWebAPI | Access the Slack Web API|

Examples

You can find the source code for several example applications here.

Tutorials

Get In Touch

Twitter: @pvzig

Email: peter@launchsoft.co

Github

link
Stars: 894

Used By

Total: 0

Releases

4.5.1 - 2019-12-20 05:08:55

  • Update for Xcode 11.3 and Swift 5.1.3

4.5.0 - 2019-10-15 02:00:57

Update for Swift 5.1 and Xcode 11 (#168)

  • Rebuild dependencies with 5.1

  • Import FoundationNetworking if found

4.4.0 - 2019-07-26 01:24:46

  • Add support for blocks
  • Add users.lookupByEmail endpoint

4.3.1 - 2019-04-07 01:00:13

  • SPM dependency fix

4.3.0 - 2019-04-05 01:45:58

  • Xcode 10.2 and Swift 5

4.2.2 - 2019-03-06 04:26:18

New

  • Add support for member_joined_channel and member_left_channel RTM events (https://github.com/pvzig/SlackKit/pull/150)

Fixed

  • Bug in decoding the user field of a MessageActionRequest (https://github.com/pvzig/SlackKit/pull/149)

4.2.1 - 2019-02-20 04:28:37

  • Fix podspec

4.2.0 - 2019-02-11 19:47:24

  • Dependency manager updates

4.1.0 - 2018-02-19 04:08:57

  • Update to Swift 4
  • Multiple workspace support

4.0.1 - 2017-07-14 20:52:40

  • Keep Package.swift up to date by using majorVersion: 4

4.0.0 - 2017-06-04 02:20:22

4.0.0

Universal

SlackKit is now universal! This release supports linux, macOS, iOS, and tvOS in a unified codebase.

Modular

SlackKit is now modular! It has been split into a few components so that you only need to pull in exactly what you need. You can see them on the organization page.

Extensible

SlackKit is now Extensible! Don't like the defaults? Bring your preferred web socket client or server along by conforming to SlackKitServer and RTMWebSocket. Write your own client implementation with SKClient.

3.1.12 - 2017-04-09 21:16:05

  • Swift 3.1 update

3.1.11 - 2017-03-19 23:12:15

  • NetworkingInterface compilation fixes
  • Dependency fixes

3.1.10 - 2017-03-18 15:16:37

  • Fix dependencies

3.1.9 - 2017-01-25 17:33:55

  • Fix podspec
  • Attachment bug fixes
  • Pass scope for oauthURLRequest

3.1.8 - 2017-01-17 02:38:06

  • Unsupported RTM events are no longer logged as errors.
  • Improved project structure when using SPM.

0.0.6 - Linux Pre-Release - 2017-01-06 02:01:20

  • Use URLComponents for links
  • Add back error RTM event type
  • Code quality improvements

3.1.7 - 2017-01-05 03:10:52

  • Use URLComponents for URLs
  • WebAPI code quality improvements

3.1.6 - 2017-01-04 02:20:44

  • Swift 3 style improvements

0.0.5 - Linux Pre-Release - 2016-12-30 04:32:00

  • Update the linux branch for Swift 3

3.1.5 - 2016-11-24 17:20:32

  • Fix iOS memory leak crash that occurred on device
  • Lowercase enums to match Swift 3 style
  • Minor code quality improvements

3.1.4 - 2016-11-17 21:32:47

  • Fix a bug where boolean value parameters weren't being passed in web API calls

3.1.3 - 2016-11-17 20:04:56

  • Add support for chat.meMessage web API call

3.1.2 - 2016-10-12 01:24:27

  • Restore CocoaPods support
  • Fix Carthage build errors

3.1.1 - 2016-10-05 14:54:02

  • Add back SPM support

2.3.0 - 2016-09-26 04:09:20

  • Support official release of Swift 2.3

3.1.0 - 2016-09-26 04:07:52

  • Support for the release version of Swift 3

Swift 3 Pre-release: Xcode 8 beta 4 - 2016-08-04 02:49:15

3.0.3 - 2016-08-03 03:25:03

  • Include style in Action JSON dictionary

2.0.3 - 2016-08-03 03:22:49

  • Include style in Action JSON dictionary