SlackKit

Build Slack apps, in Swift

Github星跟踪图

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

主要指标

概览
名称与所有者pvzig/SlackKit
主编程语言Swift
编程语言Swift (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2015-09-30 23:28:39
推送于2022-12-02 13:54:50
最后一次提交
发布数57
最新版本名称4.8.0 (发布于 )
第一版名称0.9.0 (发布于 )
用户参与
星数1k
关注者数18
派生数138
提交数516
已启用问题?
问题数107
打开的问题数0
拉请求数94
打开的拉请求数0
关闭的拉请求数12
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?