Class: Line::Message::Builder::Actions::Message
- Defined in:
- lib/line/message/builder/actions/message.rb
Overview
Represents a message action for LINE messages.
A message action sends a specified text message to the chat from the user when a button associated with this action is tapped. It's commonly used in quick replies or other interactive message components.
Example
Line::Message::Builder.with do
text "Select your favorite food:"
quick_reply do
# When this button is tapped, the user sends "Pizza"
action: :message, label: "Pizza", text: "Pizza"
# When this button is tapped, the user sends "Sushi"
action: :message, label: "Sushi", text: "Sushi"
end
end
See also:
Instance Attribute Summary collapse
-
#text ⇒ Object
readonly
The text that is sent as a message from the user when the action is performed.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(text, context: nil, **options) ⇒ Message
constructor
Initializes a new Message action.
-
#to_h ⇒ Object
Converts the Message 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(text, context: nil, **options) ⇒ Message
Initializes a new Message action.
[text]
The text to be sent when the action is performed (required)
[context]
An optional context object for resolving method calls within a block
[options]
A hash of options to set instance variables (can include :label)
[block]
An optional block to be instance-eval'd (not commonly used for simple actions)
Raises RequiredError if text is nil (this check is done in to_h
but text is conceptually required on initialization).
Example
action = Message.new("Hello, world!")
action = Message.new("Pizza", label: "Select Pizza")
69 70 71 72 73 |
# File 'lib/line/message/builder/actions/message.rb', line 69 def initialize(text, context: nil, **, &) @text = text 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
#text ⇒ Object (readonly)
The text that is sent as a message from the user when the action is performed. This is a required attribute.
34 35 36 |
# File 'lib/line/message/builder/actions/message.rb', line 34 def text @text end |
Instance Method Details
#to_h ⇒ Object
Converts the Message action object to a hash suitable for the LINE Messaging API.
The returned hash includes :type, :label (if set), and :text.
Raises RequiredError if text is nil.
Example
action = Message.new("Pizza")
action.to_h
# => { type: "message", text: "Pizza" }
[return] A hash representing the message action
90 91 92 93 94 95 96 |
# File 'lib/line/message/builder/actions/message.rb', line 90 def to_h raise RequiredError, "text is required" if text.nil? return to_sdkv2 if context.sdkv2? to_api end |