Class: Line::Bot::Builder::RichMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/line/bot/builder/rich_message.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RichMessage

Returns a new instance of RichMessage.



9
10
11
12
13
# File 'lib/line/bot/builder/rich_message.rb', line 9

def initialize client
  @actions ||= {}
  @listeners ||= []
  @client = client
end

Instance Method Details

#add_listener(attrs = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/line/bot/builder/rich_message.rb', line 36

def add_listener(attrs = {})
  tap {
    raise ArgumentError, 'Invalid arguments, :x [Fixnum], :y [Fixnum], :width [Fixnum], :height [Fixnum] keys.' unless validate_listener_attributes(attrs)

    listener = {
      type: 'touch', # Fixed "touch".
      params: [attrs[:x], attrs[:y], attrs[:width], attrs[:height]],
      action: attrs[:action],
    }
    @listeners << listener
  }
end

#contentObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/line/bot/builder/rich_message.rb', line 87

def content
  {
    contentType: Line::Bot::Message::ContentType::RICH_MESSAGE,
    toType: 1,
    contentMetadata: {
      DOWNLOAD_URL: @image_url,
      SPEC_REV: "1", # Fixed "1".
      ALT_TEXT: @alt_text,
      MARKUP_JSON: markup_json,
    },
  }
end

#event_typeObject



83
84
85
# File 'lib/line/bot/builder/rich_message.rb', line 83

def event_type
  138311608800106203
end

#heightObject



73
74
75
76
77
78
79
80
81
# File 'lib/line/bot/builder/rich_message.rb', line 73

def height
  height = 0
  @listeners.each { |listener|
    h = listener[:params][1] + listener[:params][3] # params.y + params.height
    height = h if height < h
  }

  height > 2080 ? 2080 : height
end

#markup_jsonObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/line/bot/builder/rich_message.rb', line 104

def markup_json
  {
    canvas: {
      height: height,
      width: 1040, # Integer fixed value: 1040
      initialScene: 'scene1',
    },
    images: {
      image1: {
        x: 0,
        y: 0,
        width: 1040, # Integer fixed value: 1040
        height: height
      },
    },
    actions: @actions,
    scenes: {
      scene1: {
        draws: {
          image: 'image1', # Use the image ID "image1".
          x: 0,
          y: 0,
          width: 1040, # Integer fixed value: 1040
          height: height
        },
        listeners: @listeners
      }
    }
  }.to_json
end

#send(attrs = {}) ⇒ Net::HTTPResponse

send rich message to line server and to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    line user’s mids

  • attrs (:image_url) (defaults to: {})
    String

    image file’s url

  • attrs (:alt_text) (defaults to: {})
    String

    alt text for image file’s url

Returns:

  • (Net::HTTPResponse)

    response for a request to line server

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :image_url, :preview_url keys.



66
67
68
69
70
71
# File 'lib/line/bot/builder/rich_message.rb', line 66

def send(attrs = {})
  @image_url = attrs[:image_url]
  @alt_text = attrs[:alt_text]

  @client.send_message(attrs[:to_mid], self)
end

#set_action(attrs = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/line/bot/builder/rich_message.rb', line 15

def set_action(attrs = {})
  tap {
    attrs.each { |key, value|
      raise ArgumentError, 'Invalid arguments, :text, :link_url keys.' unless validate_action_attributes(value)

      @actions[key.to_s] ||= {}
      @actions[key.to_s] = {
        type: 'web',
        text: value[:text].to_s,
        params: {
          linkUri: value[:link_url].to_s
        },
      }
    }
  }
end

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/line/bot/builder/rich_message.rb', line 100

def valid?
  @image_url && @alt_text
end

#validate_action_attributes(attrs = {}) ⇒ Object



32
33
34
# File 'lib/line/bot/builder/rich_message.rb', line 32

def validate_action_attributes(attrs = {})
  attrs[:text] && attrs[:link_url]
end

#validate_listener_attributes(attrs = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/line/bot/builder/rich_message.rb', line 49

def validate_listener_attributes(attrs = {})
  attrs[:action].instance_of?(String) &&
  attrs[:x].instance_of?(Fixnum) &&
  attrs[:y].instance_of?(Fixnum) &&
  attrs[:width].instance_of?(Fixnum) &&
  attrs[:height].instance_of?(Fixnum)
end