class Line::Message::Builder::Actions::Postback
Represents a postback action for LINE messages.
A postback action sends a postback event to your bot’s webhook when a button associated with this action is tapped. The event contains the specified data payload. Optionally, display_text can be provided, which will be shown in the chat as a message from the user.
This action is useful for triggering specific backend logic or flows without necessarily displaying a message in the chat, or for displaying a different message than the data payload.
Example
Line::Message::Builder.with do text "What do you want to do?" quick_reply do button action: :postback, label: "Track Order", data: "action=track_order&order_id=123", display_text: "I want to track my order." end end
See also:
Attributes
The data payload to be sent in the postback event to the webhook. This is a required attribute. Max 300 characters.
Public Class Methods
Source
# File lib/line/message/builder/actions/postback.rb, line 85 def initialize(data, context: nil, **options, &) @data = data super(context: context, **options, &) end
Initializes a new Postback action.
- data
-
The data to be sent in the postback event (required)
- context
-
An optional context object (default:
nil) - options
-
Options for the action, including
:labeland:display_text
Raises RequiredError if data is nil (this check is done in to_h but data is conceptually required on initialization).
Example
postback = Postback.new( "action=buy&item=123", context: view_context, label: "Buy Now", display_text: "I want to buy this item" )
Line::Message::Builder::Base::new
Public Instance Methods
Source
# File lib/line/message/builder/actions/postback.rb, line 103 def to_h raise RequiredError, "data is required" if data.nil? return to_sdkv2 if context.sdkv2? to_api end
Converts the Postback action object to a hash suitable for the LINE Messaging API.
Returns a hash representing the postback action, including :type, :label (if set), :data, and :displayText (if set).
Raises RequiredError if data is nil.
Example
postback = Postback.new("action=track", label: "Track Order") postback.to_h # => { type: "postback", label: "Track Order", data: "action=track" }