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

Inherits:
Base
  • Object
show all
Defined in:
lib/line/message/builder/actions/uri.rb

Overview

Represents a URI action for LINE messages.

A URI action opens the given URI when the component it is attached to is tapped. Unlike Postback, it needs no webhook handling, which makes it the action to reach for when a component should simply link somewhere.

The available schemes are http, https, line and tel.

A separate URI can be opened on desktop by setting alt_uri_desktop, which maps to the altUri.desktop property. When it is set, LINE for macOS and Windows ignores uri. This is supported in Flex Messages but has no effect in a quick reply.

Example: Linking a Flex button to a web page

Line::Message::Builder.with do
flex alt_text: "Event" do
  bubble do
    footer do
      button style: :primary do
        uri "https://example.com/event", label: "View event"
      end
    end
  end
end
end

Example: Opening a different page on desktop

uri "https://example.com/mobile",
  label: "Open",
  alt_uri_desktop: "https://example.com/desktop"

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(uri, context: nil, **options) ⇒ Uri

Initializes a new Uri action.

[uri] The URI to open when the action is performed (required) [context] An optional context object (default: nil) [options] Options for the action, including :label and :alt_uri_desktop [block] An optional block to be instance-eval'd

Example

Uri.new("https://example.com", label: "Open")


84
85
86
87
88
# File 'lib/line/message/builder/actions/uri.rb', line 84

def initialize(uri, context: nil, **options, &)
  @uri = uri

  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

#uriObject (readonly)

The URI opened when the action is performed. This is a required attribute. Max 1000 characters.



45
46
47
# File 'lib/line/message/builder/actions/uri.rb', line 45

def uri
  @uri
end

Instance Method Details

#to_hObject

Converts the Uri action object to a hash suitable for the LINE Messaging API.

Raises RequiredError if uri is nil.

Example

Uri.new("https://example.com", label: "Open").to_h
# => { type: "uri", label: "Open", uri: "https://example.com" }

[return] A hash representing the URI action

Raises:



102
103
104
105
106
107
108
# File 'lib/line/message/builder/actions/uri.rb', line 102

def to_h
  raise RequiredError, "uri is required" if uri.nil?

  return to_sdkv2 if context.sdkv2?

  to_api
end