Getting Started

Hooks

Advanced

Miscellaneous

General Hooks

Common hooks implementation

This section will show how the general hooks are processed, These hooks include:

  • on-receive - [POST] logic to process after user responds / sends a message
  • on-generate - [PRE] logic to process before a template is processed for rendering to user
  • template - [PRE] usually used to dynamically create a template message body
  • middleware - [POST] middleware logic to process mid-flight
  • validator - [POST] logic specifically for handling any user data validation

Consider an example for on-receive below

The example below works for on-receive, on-generate, middleware, validator hooks

On-Receive

Imagine you ask for user consent to receive an email or Slack notification. You can use this hook for just that.

1000:
  type: text
  on-receive: "rest:https://example.com/api/notify"
  message: "Type `yes` to receive a Slack notification from us"
  routes:
    "yes": "next-stage"

When user responds and on-receive is defined, the hook is processed.

Consider a sample rest hook

on-receive hook: Notify
# ...
# using FAST API

@app.post("/notify")
def slack_notify(args: HookArgs):
    print("Received args: {}".format(args))

    # assume user once provided their slack email before
    # & is stored in session as a prop
    slack_id = get_slack_id_from_props(session_id=args.channelUser.waId)

    # check if user answered yes
    if args.userInput.lower() == "yes":
        send_slack_notification(id=slack_id)

    return args