The ecosystem · packages/chat
One handler, every chat platform
A message and where it came from, and what a bot does with it, the same on every platform. Each platform is a package of its own: Slack today.
A handler for every platform#
A handler takes the message and the platform's client, and calls the client's methods. It names no platform: each platform's plugin calls it with its own client, and Roc's static dispatch finds the methods.
answer! = |message, client| {
client.working!(message, Bool.True)?
_ = Chat.reply_long!(client, message, "Hi ${message.user.name}, you said: _${message.text}_")?
client.working!(message, Bool.False)
}
program = App.program({
init!: |_started| Ok({}),
plugins: [Slack.plugin({ on_message!: answer! })],
})Left unannotated, the handler's type is inferred with the methods it calls. Annotated, it names them in a where:
answer! : Chat.Message, c => Try({}, Str)
where [
c.working! : c, Chat.Message, Bool => Try({}, Str),
c.reply! : c, Chat.Message, Str => Try(Chat.Sent, Str),
c.max_length : c -> U64,
]A platform's package keeps its own API beside these, for what only it does: Slack's Web API through Slack.call!.
The methods#
| Method | What it does |
|---|---|
name, max_length | the platform ("slack"), and the longest text one message carries, in bytes |
reply!(message, text) | a reply, in the message's thread (at a direct message's top level when it has none); the text is Markdown, written as the platform writes it |
edit!(sent, text) | a message sent, rewritten |
react!(message, emoji) | an emoji on a message: "👀" |
working!(message, on) | whether the bot is working on its answer, as the platform shows it |
history!(message, limit) | the conversation the message is in, oldest first, the bot's own messages too |
download!(file) | a file's bytes |
Each answers a Try; an Err is the platform's reason, in words.
A message#
| Field | What it is |
|---|---|
id, space | the platform's id for it, and where it was written (a channel, a direct conversation) |
thread | the thread replies go in; "" for a direct message's top level |
user | { id, name }: who wrote it |
text | what it says, as Markdown, the bot's own mention taken out |
direct, mentioned, bot | written to the bot alone; naming the bot; written by the bot |
files | what came with it: { id, name, mime, size, url } |
raw | the platform's own JSON, for what this doesn't say |
Helpers#
Chat.reply_long!(client, message, text): as many replies as the text takes, each withinmax_length.Chat.split(text, max): pieces cut between lines, then words, then characters; a code block cut in two is closed and opened again.Chat.key(client, message):"<platform>:<space>:<thread>", where to keep a conversation (in Kv), so a thread finds what was said before.
A new platform#
A platform's package gives its client type these methods, with these types, and a plugin whose options take the app's handler for that type. Its plugin acknowledges the platform's events at once and calls the handler in a loop of its own, so a long answer never keeps the platform waiting. packages/slack is the one to copy; Telegram and Discord are next (the package's PROPOSALS.md).