lib/bobot/page.rb in bobot-3.7.9 vs lib/bobot/page.rb in bobot-4.0.0

- old
+ new

@@ -33,53 +33,58 @@ # REQUESTS # ##################################### def deliver(payload_template:, to:) - raise Bobot::FieldFormat.new('payload_template is required') unless payload_template.present? - raise Bobot::FieldFormat.new('payload_template[:messaging_type] is required') unless payload_template.key?(:messaging_type) - raise Bobot::FieldFormat.new('payload_template[:messaging_type] is invalid, only "RESPONSE, UPDATE, MESSAGE_TAG" are permitted.', payload_template[:messaging_type]) unless %w[RESPONSE UPDATE MESSAGE_TAG].include?(payload_template[:messaging_type]) + if payload_template.present? + if payload_template.key?(:messaging_options) && !payload_template[:messaging_options].nil? && payload_template[:messaging_options].key?(:messaging_type) + if !%w[RESPONSE UPDATE MESSAGE_TAG].include?(payload_template[:messaging_options][:messaging_type]) + raise Bobot::FieldFormat.new('payload_template[:messaging_options][:messaging_type] is invalid, only "RESPONSE, UPDATE, MESSAGE_TAG" are permitted.', payload_template[:messaging_options][:messaging_type]) + elsif "MESSAGE_TAG" == payload_template[:messaging_options][:messaging_type] && !payload_template[:messaging_options].key?(:tag) + raise Bobot::FieldFormat.new('payload_template[:messaging_options][:key] is required when messaging_type is MESSAGE_TAG.', payload_template[:messaging_options][:tag]) + end + end + else + raise Bobot::FieldFormat.new('payload_template is required') + end + body = { recipient: { id: to }, messaging_type: "RESPONSE" }.merge(payload_template).merge(payload_template[:messaging_options] || {}) + query = { access_token: page_access_token } Bobot::Commander.deliver( - body: { - recipient: { id: to }, - messaging_type: "RESPONSE", - }.merge(payload_template), - query: { - access_token: page_access_token, - }, + body: body, + query: query, ) end - - def sender_action(sender_action:, to: nil, messaging_type: "RESPONSE") - deliver(payload_template: { sender_action: sender_action, messaging_type: messaging_type }, to: to) + + def sender_action(sender_action:, to: nil, messaging_options: nil) + deliver(payload_template: { sender_action: sender_action, messaging_options: messaging_options }, to: to) end - def show_typing(state:, to: nil, messaging_type: "RESPONSE") - sender_action(sender_action: (state ? 'typing_on' : 'typing_off'), messaging_type: messaging_type, to: to) + def show_typing(state:, to: nil, messaging_options: nil) + sender_action(sender_action: (state ? 'typing_on' : 'typing_off'), messaging_options: messaging_options, to: to) end - def mark_as_seen(to: nil, messaging_type: "RESPONSE") - sender_action(sender_action: 'mark_seen', messaging_type: messaging_type, to: to) + def mark_as_seen(to: nil, messaging_options: nil) + sender_action(sender_action: 'mark_seen', messaging_options: messaging_options, to: to) end - def send(payload_message:, to: nil, messaging_type: "RESPONSE") - deliver(payload_template: { message: payload_message, messaging_type: messaging_type }, to: to) + def send(payload_message:, to: nil, messaging_options: nil) + deliver(payload_template: { message: payload_message, messaging_options: messaging_options }, to: to) end - def send_text(text:, to: nil, messaging_type: "RESPONSE") + def send_text(text:, to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('text is required') unless text.present? raise Bobot::FieldFormat.new('text size is limited to 640.', "#{text} (#{text.size} chars)") if text.size > 640 send( payload_message: { text: text, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end - def send_attachment(url:, type:, to: nil, messaging_type: "RESPONSE") + def send_attachment(url:, type:, to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('url is required') unless url.present? raise Bobot::FieldFormat.new('type is required') unless type.present? raise Bobot::FieldFormat.new('type is invalid, only "image, audio, video, file" are permitted.', type) unless %w[image audio video file].include?(type) send( payload_message: { @@ -89,15 +94,15 @@ url: url, }.tap { |properties| properties.merge!(is_reusable: true) if type == 'image' }, }, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end - def send_youtube_video(url:, to: nil, messaging_type: "RESPONSE") + def send_youtube_video(url:, to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('url is required') unless url.present? raise Bobot::FieldFormat.new('url is not valid', url) unless url =~ %r{^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})} send( payload_message: { attachment: { @@ -109,46 +114,46 @@ ], }, }, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end - def send_image(url:, to: nil, messaging_type: "RESPONSE") - send_attachment(url: url, type: 'image', to: to, messaging_type: messaging_type) + def send_image(url:, to: nil, messaging_options: nil) + send_attachment(url: url, type: 'image', to: to, messaging_options: messaging_options) end - def send_audio(url:, to: nil, messaging_type: "RESPONSE") - send_attachment(url: url, type: 'audio', to: to, messaging_type: messaging_type) + def send_audio(url:, to: nil, messaging_options: nil) + send_attachment(url: url, type: 'audio', to: to, messaging_options: messaging_options) end - def send_video(url:, to: nil, messaging_type: "RESPONSE") - send_attachment(url: url, type: 'video', to: to, messaging_type: messaging_type) + def send_video(url:, to: nil, messaging_options: nil) + send_attachment(url: url, type: 'video', to: to, messaging_options: messaging_options) end - def send_file(url:, to: nil, messaging_type: "RESPONSE") - send_attachment(url: url, type: 'file', to: to, messaging_type: messaging_type) + def send_file(url:, to: nil, messaging_options: nil) + send_attachment(url: url, type: 'file', to: to, messaging_options: messaging_options) end - def send_quick_replies(text:, quick_replies:, to: nil, messaging_type: "RESPONSE") + def send_quick_replies(text:, quick_replies:, to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('text is required') unless text.present? raise Bobot::FieldFormat.new('text size is limited to 640.', "#{text} (#{text.size} chars)") if text.size > 640 raise Bobot::FieldFormat.new('quick_replies are required') unless quick_replies.present? raise Bobot::FieldFormat.new('quick_replies are limited to 11.', "#{quick_replies.size} quick replies") if quick_replies.size > 11 send( payload_message: { text: text, quick_replies: quick_replies, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end - def send_buttons(text:, buttons:, to: nil, messaging_type: "RESPONSE") + def send_buttons(text:, buttons:, to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('text is required') unless text.present? raise Bobot::FieldFormat.new('text size is limited to 640.', "#{text} (#{text.size} chars)") if text.size > 640 raise Bobot::FieldFormat.new('buttons are required') unless buttons.present? raise Bobot::FieldFormat.new('buttons are limited to 3', "#{buttons.size} buttons") if buttons.size > 3 send( @@ -161,15 +166,15 @@ buttons: buttons, }, }, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end - def send_generic(elements:, image_aspect_ratio: 'square', to: nil, messaging_type: "RESPONSE") + def send_generic(elements:, image_aspect_ratio: 'square', to: nil, messaging_options: nil) raise Bobot::FieldFormat.new('elements are required') if elements.nil? raise Bobot::FieldFormat.new('elements are limited to 10.', "#{elements.size} elements") if elements.size > 10 raise Bobot::FieldFormat.new('image_aspect_ratio is required') if image_aspect_ratio.nil? raise Bobot::FieldFormat.new('image_aspect_ratio is invalid, only "square, horizontal" are permitted.', image_aspect_ratio) unless %w[square horizontal].include?(image_aspect_ratio) send( @@ -182,10 +187,10 @@ elements: elements, }, }, }, to: to, - messaging_type: messaging_type, + messaging_options: messaging_options, ) end alias_method :send_carousel, :send_generic #####################################