Skip to content

Slack message generator and API client, written in Swift with Result Builders and Concurrency

License

Notifications You must be signed in to change notification settings

MPLew-is/slack-message-client

Repository files navigation

Slack Message Client

This package provides a Swift object model for a Slack Block Kit message, as well as a Result Builder convenience interface for easy message creation. Also provides an asynchronous API client for sending messages based on the async-http-client from the Swift Server Workgroup.

This is still in extremely early development, and only currently supports sending messages and a narrow subset of the full Block Kit API.

Quick Start

See the command-line interface example for an example implementation runnable from the command line, with configurable message text.

Add to your Package.Swift:

...
	dependencies: [
		...
		.package(url: "https://github.com/MPLew-is/slack-message-client", branch: "main"),
	],
	targets: [
		...
		.target(
			...
			dependencies: [
				...
				.product(name: "BlockKitMessage", package: "slack-message-client"),
				.product(name: "SlackMessageClient", package: "slack-message-client"),
			]
		),
		...
	]
]

Create and send a message:

import BlockKitMessage
import SlackMessageClient

@main
struct SlackMessageExample {
	static func main() async throws {
		let message = Message.build {
			Header("Header")

			Section(mrkdwn: "Section")

			Context.build {
				Image(url: "https://example.com", alternateText: "Alt text")
				Mrkdwn("User")
			}
		}

		let client = SlackMessageClient(authToken: "YOUR_SLACK_BOT_TOKEN")
		try await client.post(message, to: "YOUR_SLACK_CHANNEL_ID")
	}
}

(See the command-line interface example for more detailed instructions on how to set up a Slack app and get the required authentication/configuration values)

Targets provided

  • BlockKitMessage: object model for a Slack Block Kit message, if you just want to create messages but not send them

    • A Message object conforms to Codable, so you can use this to just generate the JSON expected by Slack, for instance
  • SlackApiClient: a thin wrapper around an AsyncHTTPClient which auto-injects the correct headers needed for the Slack API

    • You can use this by itself if you want to perform actions against the Slack API other than chat.postMessage
  • SlackMessageClient: full client integrating the previous two targets into a simple interface for sending messages to a channel

Features supported and planned

If you don't see something on this list, it's not currently planned. Feel free to file an issue/PR to change that though.

Block Kit

  • Top-level message objects

    • Basic integration (blocks argument)
    • Alternate text (text argument)
  • Mrkdwn result builder

    • Basic support for mrkdwn embedded in a string is already present, this will just provide a better way to build complicated messages
  • Header blocks

  • Section blocks

    • Basic support (text)
    • Fields and accessories not currently planned
  • Context blocks

  • Divider blocks

  • Image blocks

  • Actions, file, and input blocks not currently planned

Slack message client

  • chat.postMessage method integration

    • channel argument
    • Attachments and optional arguments not currently planned
  • Sending Message object

  • Sending raw JSON blocks generated externally

  • Optional credentials verification on initialization to fail quickly (api.test method)

  • Better parsing and handling of HTTP and Slack API errors

About

Slack message generator and API client, written in Swift with Result Builders and Concurrency

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages