Class: Line::Message::Builder::Actions::Postback
- Defined in:
- lib/line/message/builder/actions/postback.rb
Overview
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
action: :postback,
label: "Track Order",
data: "action=track_order&order_id=123",
display_text: "I want to track my order."
end
end
See also:
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The data payload to be sent in the postback event to the webhook.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(data, context: nil, **options) ⇒ Postback
constructor
Initializes a new Postback action.
-
#to_h ⇒ Object
Converts the Postback action object to a hash suitable for the LINE Messaging API.
Methods inherited from Base
inherited, #method_missing, #quick_reply, #respond_to_missing?
Constructor Details
#initialize(data, context: nil, **options) ⇒ Postback
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 :label and :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"
)
85 86 87 88 89 |
# File 'lib/line/message/builder/actions/postback.rb', line 85 def initialize(data, context: nil, **, &) @data = data super(context: context, **, &) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base
Instance Attribute Details
#data ⇒ Object (readonly)
The data payload to be sent in the postback event to the webhook. This is a required attribute. Max 300 characters.
35 36 37 |
# File 'lib/line/message/builder/actions/postback.rb', line 35 def data @data end |
Instance Method Details
#to_h ⇒ Object
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" }
103 104 105 106 107 108 109 |
# 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 |