Parsing Slack /remind command
Have you ever wondered how Slack /remind commands works under the hood?
You just type
/remind @sibelius work tomorrow
and Slack knows that it should remind @sibelius to work tomorrow.
It feels Slack understanding natural language.
However, this is not a fancy machine learning algorithm, neither GPT3.
You can achieve the same behavior creating a simple parser in a few lines
Understanding Slack /remind command structure
The /remind command has 4 parts:
- /remind: word that tells slack that we want to remind someone or a channel about something in the future
- @someone or #channel: who or which channel we want to remind
- what: what do we want to remind someone or some channel
- when: when do we want the reminder to happen
Basic Grammar concepts
Grammar is a group of rules.
A rule can be a terminal or a non-terminal
A terminal rule does not expand anymore and does not use other rules example:
when -> (“today” | “tomorrow” | “next week”)
The rule above tells us that when
can be today
or tomorrow
or next week
.
A non-terminal rule is build of other rules, example:
someone_channel -> someone:? channel:?
In the rule above someone_channel can be something produced by someone
rule or something produced by channel
.
Building a Simple Grammar to parse it
main -> "/remind" _ someone_channel _ what _ when
Our main rule (non-terminal) will just describe the /remind
command.
The remind
commands need the /remind
string, followed by a space
(_ represents an obligatory space), followed by a someone_channel
, then space
, then what
, then space
then when
.
Everything that is not inside quotes or _
needs to be defined by other rules.
Let's take a look at the definition of the rest of the rules:
Let's explain each of those.
someone_channel
needs someone
or channel
, both are optional (?), you need to make sure the input has at least one of them.
someone
is a "@" followed by a nickname, in this case, we are using Regex to match 1 or more letters
channel
is a "#" followed by a channel name, that followed the same pattern as the nickname.
what
is a regex matching, it can be improved to match numbers and more than one word
when
for now only matches today
tomorrow
next week
, feel free to improve it.
Give a Try
You can give a try to a working version of this grammar in this repository slack-reminder-grammar.
You can give a try right now from your terminal
npx slack-reminder-grammar "/remind #general party next week"
Further Reading
Parsing absolutely anything in JavaScript using Earley algorithm
What's Next?
You can build your own simple languages to power your SaaS business or just for fun.
Ping me on twitter if you wanna talk https://twitter.com/sseraphini
Subscribe to my newsletter here https://sibelius.substack.com/, where I talk about productivity, startups, and tech.