Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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#

MethodWhat it does
name, max_lengththe 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#

FieldWhat it is
id, spacethe platform's id for it, and where it was written (a channel, a direct conversation)
threadthe thread replies go in; "" for a direct message's top level
user{ id, name }: who wrote it
textwhat it says, as Markdown, the bot's own mention taken out
direct, mentioned, botwritten to the bot alone; naming the bot; written by the bot
fileswhat came with it: { id, name, mime, size, url }
rawthe platform's own JSON, for what this doesn't say

Helpers#

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).