Kogno
  • Introduction
  • Getting Started
    • Configuration
    • Starting the Server
    • Messenger Configuration
    • Telegram Configuration
    • WhatsApp Configuration
    • NLP Configuration
  • Conversation Class
  • Contexts
    • Blocks
      • before_anything
      • postback
      • deep_link
      • command
      • any_attachment
      • regular_expression
      • keyword
      • any_number
      • any_text
      • intent
      • entity
      • membership
      • recurring_notification
      • everything_else
      • after_all
    • Sub Contexts
    • Routing
    • Sequences
    • Conversational Forms
  • Replies / Notifications
    • text
    • button
    • quick_reply
    • raw
    • list
    • carousel
    • url
    • typing
    • image
    • video
    • html
    • markdown
    • contact
    • location
    • recurring_notification_request
    • messenger_generic_template
    • whatsapp_template
  • Templates
  • Models
    • User model
  • Scheduled Messages
  • Telegram Inline Query
  • Command Line
  • Global Methods
  • Internationalization
Powered by GitBook
On this page
  • postback(payload=String|Array, &block)
  • Platforms
  • Usage
  • Reading params
  • any_postback block
  • Usage

Was this helpful?

  1. Contexts
  2. Blocks

postback

Captures a click event that contains one or more payloads configured.

Previousbefore_anythingNextdeep_link

Last updated 2 years ago

Was this helpful?

The click event that is performed by a user can come from a , or .

postback(payload=String|Array, &block)

Platforms

Platform
Supported

Messenger

WhatsApp

Telegram

Usage

The example below shows how 3 different click events will be handled:

  • postback"get_started": Click event on .

  • postback"yes": Click event on "Of course!" button that has been sent as reply in the previous block.

  • postback"no": Click event on "Not really 🤷🏻‍♂️" button that has been sent as reply in the first block.

class MainContext < Conversation

  def blocks

    postback "get_started" do
    
      @reply.text("Hello!")
      @reply.quick_reply(
        "Is it clear to you how postbacks work?",
        [
          {
            title: "Of course!",
            payload: "yes"
          },
          {
            title: "Not really 🤷🏻‍♂️",
            payload: "no"
          }
        ]
      )   
                
    end
    
    postback "yes" do
    
      @reply.text "Awesome!"
      @reply.text "We've put a lot of effort into writing this documentation 💪"
      
    end
    
    postback "no" do 
    
      @reply.text "No problem, continue reading and you will got it..."
      
    end

  end
  
end

Reading params

The postback block can receive parameters that are sent as part of the payload.

Next we will implement the same example as above, but using parameters:

class MainContext < Conversation

  def blocks

    postback "get_started" do
    
      @reply.text("Hello!")
      @reply.quick_reply(
        "Is it clear to you how postbacks work?",
        [
          {
            title: "Of course!",
            payload: set_payload("understood", {response: "yes"})
          },
          {
            title: "Not really 🤷🏻‍♂️",
            payload: set_payload("understood", {response: "no"})
          }
        ]
      )  
                 
    end
    
    postback "understood" do |params|
    
      response = params[:response]
      if response == 'yes'
      
        @reply.text "Awesome!"
        @reply.text "We've put a lot of effort into writing this documentation 💪"

      elsif response == 'no'
        @reply.text "No problem, continue reading and you will got it..."
      end
    end

  end
  
end

set_payload()

any_postback block

Catch any postback received by a context and returns two parameters payload and payload_params.

Usage

class MainContext < Conversation

  def blocks

    any_postback do |payload, payload_params|

      if payload == "get_started"
    
        @reply.text("Hello!")
        @reply.quick_reply(
          "Is it clear to you how postbacks work?",
          [
            {
              title: "Of course!",
              payload: "yes"
            },
            {
              title: "Not really 🤷🏻‍♂️",
              payload: "no"
            }
          ]
        )  

      elsif payload == "yes" 

        @reply.text "Awesome!"
        @reply.text "We've put a lot of effort into writing this documentation 💪"

      elsif payload == "no"

        @reply.text "No problem, continue reading and you will got it..."
        
      end
                
    end

  end
  
end

Route to context

A payload can include a route to a postback located in a different context than the current context.

The format of a payload containing a route is as follows:

"context_name/payload"

It is a global method of the framework, it is used to generate a payload with parameters. .

Read more about this in .

button
quick_reply
list
Messenger's Get Started button
Read more
Context Routing chapter