Templates

It calls a template with extension ".erb" and executes it. There may be just one or a serie of replies.

template(route=String, params=Hash)

Usage

@reply.template "main/menu", { title: "But, I can help you with this" }

File Location

bot/templates/main/menu.erb

Templates are found in sub-directories under bot/templates/ and each sub-directory within has the same name as an existing context in a given project.

For example: bot/templates/context_name/template_name.erb.

File Content

The code in the template must be written between the chars <% %>.

<%
  @reply.quick_reply(
    title,
    [
      {
        title: "Subscribe",
        payload: "profile/sign_up"
      },
      {
        title: "Follow US",
        payload: :twitter
      },
      {
        title: "Contact US",
        payload: :contact_us        
      }
    ]
  )
%>

params argument

The params argument can contain various elements which are accessed as a local variable within the template. In the example above: title.

Arguments

Template reuse example

In the example below, the "main/menu" template will be called in 3 different situations in the conversation:

  1. When the user sends a greeting..

  2. When the user thanks..

  3. When the app cannot understand what the user has said.

class MainContext < Conversation

  def actions

    intent :gretting do
      @reply.text "Hello!"
      @reply.template "main/menu", { title: "How can I help you?" }
    end

    intent :thanks do
      @reply.text "You're welcome!"
      @reply.template "main/menu", { title: "Is there anything else I can help you with?" }
    end

    everything_else do
      @reply.text "Sorry, but I don't understand what you said."
      @reply.template "main/menu", { title: "But, I can help you with this" }
    end

  end

end

Last updated