/SlackKit

a Slack client library for iOS and OS X written in Swift

Primary LanguageSwiftMIT LicenseMIT

SlackKit ##iOS, OS X, and tvOS Slack Client Library ###Description This is a Slack client library for OS X, iOS, and tvOS written 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 by bot users.

####Building the SlackKit Framework To build the SlackKit project directly, first build the dependencies using Carthage or CocoaPods. To use the framework in your application, install it in one of the following ways:

###Installation ####CocoaPods Add the pod to your podfile:

pod 'SlackKit'

and run

pod install

####Carthage

Add SlackKit to your Cartfile:

github "pvzig/SlackKit" ~> 1.0

and run

carthage bootstrap

Note: SlackKit currently takes a long time for the compiler to compile with optimizations turned on. I'm currently exploring a potential fix for this issue. In the meantime, you may want to skip the waiting and build it in the debug configuration instead:

carthage bootstrap --configuration "Debug"

Drag the built SlackKit.framework into your Xcode project.

####Swift Package Manager Add SlackKit to your Package.swift

import PackageDescription

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

Run swift build on your application’s main directory.

To use the library in your project import it:

import SlackKit

###Usage To use SlackKit you'll need a bearer token which identifies a single user. You can generate a full access token or create one using OAuth 2.

Once you have a token, initialize a client instance using it:

let client = Client(apiToken: "YOUR_SLACK_API_TOKEN")

If you want to receive messages from the Slack RTM API, connect to it.

client.connect()

You can also set options for a ping/pong interval, timeout interval, and automatic reconnection:

client.connect(pingInterval: 2, timeout: 10, reconnect: false)

Once connected, the client will begin to consume any messages sent by the Slack RTM API.

####Web API Methods SlackKit currently supports the a subset of the Slack Web APIs that are available to bot users:

  • api.test
  • auth.test
  • channels.history
  • channels.info
  • channels.list
  • channels.mark
  • channels.setPurpose
  • channels.setTopic
  • chat.delete
  • chat.postMessage
  • chat.update
  • emoji.list
  • files.comments.add
  • files.comments.edit
  • files.comments.delete
  • files.delete
  • files.info
  • files.upload
  • groups.close
  • groups.history
  • groups.info
  • groups.list
  • groups.mark
  • groups.open
  • groups.setPurpose
  • groups.setTopic
  • im.close
  • im.history
  • im.list
  • im.mark
  • im.open
  • mpim.close
  • mpim.history
  • mpim.list
  • mpim.mark
  • mpim.open
  • pins.add
  • pins.list
  • pins.remove
  • reactions.add
  • reactions.get
  • reactions.list
  • reactions.remove
  • rtm.start
  • stars.add
  • stars.remove
  • team.info
  • users.getPresence
  • users.info
  • users.list
  • users.setActive
  • users.setPresence

They can be accessed through a Client object’s webAPI property:

client.webAPI.authenticationTest({
(authenticated) -> Void in
		print(authenticated)
	}){(error) -> Void in
	    print(error)
}

####Delegate methods

To receive delegate callbacks for certain events, register an object as the delegate for those events:

client.slackEventsDelegate = self

There are a number of delegates that you can set to receive callbacks for certain events.

#####SlackEventsDelegate

func clientConnectionFailed(error: SlackError)
func clientConnected()
func clientDisconnected()
func preferenceChanged(preference: String, value: AnyObject)
func userChanged(user: User)
func presenceChanged(user: User?, presence: String?)
func manualPresenceChanged(user: User?, presence: String?)
func botEvent(bot: Bot)

#####MessageEventsDelegate

func messageSent(message: Message)
func messageReceived(message: Message)
func messageChanged(message: Message)
func messageDeleted(message: Message?)

#####ChannelEventsDelegate

func userTyping(channel: Channel?, user: User?)
func channelMarked(channel: Channel, timestamp: String?)
func channelCreated(channel: Channel)
func channelDeleted(channel: Channel)
func channelRenamed(channel: Channel)
func channelArchived(channel: Channel)
func channelHistoryChanged(channel: Channel)
func channelJoined(channel: Channel)
func channelLeft(channel: Channel)

#####DoNotDisturbEventsDelegate

doNotDisturbUpdated(dndStatus: DoNotDisturbStatus)
doNotDisturbUserUpdated(dndStatus: DoNotDisturbStatus, user: User?)

#####GroupEventsDelegate

func groupOpened(group: Channel)

#####FileEventsDelegate

func fileProcessed(file: File)
func fileMadePrivate(file: File)
func fileDeleted(file: File)
func fileCommentAdded(file: File, comment: Comment)
func fileCommentEdited(file: File, comment: Comment)
func fileCommentDeleted(file: File, comment: Comment)

#####PinEventsDelegate

func itemPinned(item: Item?, channel: Channel?)
func itemUnpinned(item: Item?, channel: Channel?)

#####StarEventsDelegate

func itemStarred(item: Item, star: Bool)

#####ReactionEventsDelegate

func reactionAdded(reaction: String?, item: Item?, itemUser: String?)
func reactionRemoved(reaction: String?, item: Item?, itemUser: String?)

#####TeamEventsDelegate

func teamJoined(user: User)
func teamPlanChanged(plan: String)
func teamPreferencesChanged(preference: String, value: AnyObject)
func teamNameChanged(name: String)
func teamDomainChanged(domain: String)
func teamEmailDomainChanged(domain: String)
func teamEmojiChanged()

#####SubteamEventsDelegate

func subteamEvent(userGroup: UserGroup)
func subteamSelfAdded(subteamID: String)
func subteamSelfRemoved(subteamID: String)

###Examples ####Leaderboard Included in the OSX-Sample is an example application of a bot you might make using SlackKit. It’s a basic leaderboard scoring bot, in the spirit of PlusPlus.

To configure it, enter your bot’s API token in AppDelegate.swift for the Leaderboard bot:

let learderboard = Leaderboard(token: "SLACK_AUTH_TOKEN")

It adds a point for every @thing++, subtracts a point for every @thing--, and shows a leaderboard when asked @botname leaderboard.

###Get In Touch @pvzig

peter@launchsoft.co