Class: Line::Message::Builder::Actions::Message

Inherits:
Base
  • Object
show all
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"
  button action: :message, label: "Pizza", text: "Pizza"
  # When this button is tapped, the user sends "Sushi"
  button action: :message, label: "Sushi", text: "Sushi"
end
end

See also:

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Instance Method Summary collapse

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, **options, &)
  @text = text

  super(context: context, **options, &)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base

Instance Attribute Details

#textObject (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_hObject

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

Raises:



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