Class: Line::Bot::Builder::RichMessage
- Inherits:
-
Object
- Object
- Line::Bot::Builder::RichMessage
- Defined in:
- lib/line/bot/builder/rich_message.rb
Instance Method Summary collapse
- #add_listener(attrs = {}) ⇒ Object
- #content ⇒ Object
- #event_type ⇒ Object
- #height ⇒ Object
-
#initialize(client) ⇒ RichMessage
constructor
A new instance of RichMessage.
- #markup_json ⇒ Object
-
#send(attrs = {}) ⇒ Net::HTTPResponse
send rich message to line server and to users.
- #set_action(attrs = {}) ⇒ Object
- #valid? ⇒ Boolean
- #validate_action_attributes(attrs = {}) ⇒ Object
- #validate_listener_attributes(attrs = {}) ⇒ Object
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 |
#content ⇒ Object
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_type ⇒ Object
83 84 85 |
# File 'lib/line/bot/builder/rich_message.rb', line 83 def event_type 138311608800106203 end |
#height ⇒ Object
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_json ⇒ Object
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
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.(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
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 |