Slack Web API client
README.md

slack #

A Slack Web API client and command-line tool for OCaml.

slack gives typed bindings to the Slack Web API: channels, messages, users,
files, reactions, emoji, reminders, pins, stars and audit logs, over
requests for HTTP and Eio for concurrency. Slack formats messages in its own
markup, mrkdwn, rather than Markdown, so the library also parses mrkdwn and
converts between it and CommonMark. The slack command-line tool uses the
library to list channels, browse history with threaded replies, send and
delete messages, list users and join channels from a terminal.

Installation #

Install with opam:

$ opam install nox-slack

If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-slack

Usage #

Library #

let run ~token =
  Eio_main.run @@ fun env ->
  Eio.Switch.run @@ fun sw ->
  let api = Slack.Api.v ~sw env token in
  match Slack.Api.send_message ~channel:"C01EXAMPLE" ~text:"Hello!" api with
  | Error e -> Fmt.epr "send: %a@." Slack.Error.pp e
  | Ok _ts ->
      (match Slack.Api.list_channels api with
      | Error e -> Fmt.epr "list_channels: %a@." Slack.Error.pp e
      | Ok channels -> Fmt.pr "%d channels@." (List.length channels));
      (match Slack.Api.history ~channel:"C01EXAMPLE" ~limit:10 api with
      | Error e -> Fmt.epr "history: %a@." Slack.Error.pp e
      | Ok messages -> Fmt.pr "%d messages@." (List.length messages))

CLI #

export SLACK_TOKEN=xoxb-...
slack channels
slack history '#general' -n 20
slack send -c '#general' -m 'Hello from the CLI'
slack users
slack info '#general'
slack join '#random'

The send command accepts --format markdown (default) to convert CommonMark
input to Slack mrkdwn, or --format mrkdwn to send raw Slack markup.

API #

  • Slack.Api: the client, created from a token, with typed methods for chat,
    conversations, users, files, reactions, emoji, reminders, pins, stars, audit
    logs and admin operations.
  • Slack.Channel, Slack.Message and Slack.User: the main resource types,
    with their JSON codecs.
  • Slack.Mrkdwn: a parser from mrkdwn to a block and inline AST.
  • Slack.Markdown: conversion between CommonMark and mrkdwn.
  • Slack.History: terminal display of message history with threaded
    replies.
  • Slack.Sanitize: removal of terminal escapes and invisible characters
    before display.
  • Slack.Block, Slack.Attachment, Slack.File, Slack.Reaction,
    Slack.Reminder, Slack.Pin, Slack.Star, Slack.Emoji, Slack.Team and
    Slack.Audit: the other API resources.

Blocks are kept as unstructured JSON, and a few methods, such as
list_usergroups, report success without returning the data.

References #

Licence #

ISC. See LICENSE.md.