Skip to content

Instantly share code, notes, and snippets.

@sweetpi
Last active August 29, 2015 14:01
Show Gist options
  • Save sweetpi/492d63290260823cef6c to your computer and use it in GitHub Desktop.
Save sweetpi/492d63290260823cef6c to your computer and use it in GitHub Desktop.
providing actions
class AnswerActionHandler extends env.actions.ActionHandler
constructor: (@framework) ->
executeAction: (simulate) =>
if simulate
return Promise.resolve(__("would log 42"))
else
env.logger.info "42"
return Promise.resolve(__("logged 42"))
class AnswerActionProvider extends env.actions.ActionProvider
constructor: (@framework) -> # nop
# ### executeAction()
###
This function handles action in the form of `the answer`
###
parseAction: (input, context) =>
# `input` is the string that could be an action that should be handled by the provider.
# With the matcher you can easily check if the input matches some strings:
m = M(input, context)
.match("the ", optional: yes)
.match("answer")
# If we have a match
if m.hadMatch()
match = m.getFullMatch()
# then return a info object with some infos
# and a corresponding ActionHandler
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new AnswerActionHandler(@framework)
}
else
return null
class YourPlugin extends env.plugins.Plugin
init: (app, @framework, config) =>
# [...]
@framework.ruleManager.addActionProvider(new AnswerActionProvider(@framework))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment