Module: Bobot::Event::Common

Included in:
AccountLinking, Delivery, Message, Optin, Postback, Read, Referral
Defined in:
lib/bobot/events/common.rb

Overview

Common attributes for all incoming data from Facebook.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delay_optionsObject

Returns the value of attribute delay_options.



6
7
8
# File 'lib/bobot/events/common.rb', line 6

def delay_options
  @delay_options
end

#messagingObject (readonly)

Returns the value of attribute messaging.



5
6
7
# File 'lib/bobot/events/common.rb', line 5

def messaging
  @messaging
end

#payloads_sentObject

Returns the value of attribute payloads_sent.



7
8
9
# File 'lib/bobot/events/common.rb', line 7

def payloads_sent
  @payloads_sent
end

Instance Method Details

#access_tokenObject



165
166
167
# File 'lib/bobot/events/common.rb', line 165

def access_token
  Bobot.config.find_page_by_id(recipient["id"]).try(:page_access_token)
end

#add_delivery(payload:) ⇒ Object



15
16
17
# File 'lib/bobot/events/common.rb', line 15

def add_delivery(payload:)
  @payloads_sent << payload
end

#delay(wait: 0, wait_until: nil) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/bobot/events/common.rb', line 27

def delay(wait: 0, wait_until: nil)
  raise Bobot::FieldFormat.new('wait has to be positive integer.') unless wait.present?
  if Bobot.config.async
    @delay_options[:wait] = wait if wait >= 0
    @delay_options[:wait_until] = wait_until if wait_until.present?
  else
    warn "delay is ignored since you configured Bobot.config.async to 'false'"
  end
  self
end

#deliver(payload_template:) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bobot/events/common.rb', line 42

def deliver(payload_template:)
  raise Bobot::FieldFormat.new('payload_template is required.') unless payload_template.present?
  @payloads_sent << payload_template
  job = Bobot::DeliverJob
  if Bobot.config.async
    job = job.set(wait: @delay_options[:wait]) if @delay_options[:wait] > 0
    job = job.set(wait: @delay_options[:wait_until]) if @delay_options[:wait_until].present?
    job.perform_later(sender: sender, access_token: access_token, payload_template: payload_template)
  else
    job.perform_now(sender: sender, access_token: access_token, payload_template: payload_template)
  end
end

#initialize(messaging) ⇒ Object



9
10
11
12
13
# File 'lib/bobot/events/common.rb', line 9

def initialize(messaging)
  @messaging = messaging
  @delay_options = { wait: 0, wait_until: nil }
  @payloads_sent = []
end

#mark_as_seenObject



63
64
65
# File 'lib/bobot/events/common.rb', line 63

def mark_as_seen
  sender_action(sender_action: 'mark_seen')
end

#recipientObject



23
24
25
# File 'lib/bobot/events/common.rb', line 23

def recipient
  @messaging['recipient']
end

#reply(payload_message:) ⇒ Object



67
68
69
# File 'lib/bobot/events/common.rb', line 67

def reply(payload_message:)
  deliver(payload_template: { message: payload_message })
end

#reply_with_attachment(url:, type:) ⇒ Object

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bobot/events/common.rb', line 81

def reply_with_attachment(url:, type:)
  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.') unless %w[image audio video file].include?(type)
  reply(
    payload_message: {
      attachment: {
        type: type,
        payload: {
          url: url,
        }.tap { |properties| properties.merge!(is_reusable: true) if type == 'image' },
      },
    },
  )
end

#reply_with_audio(url:) ⇒ Object



101
102
103
# File 'lib/bobot/events/common.rb', line 101

def reply_with_audio(url:)
  reply_with_attachment(url: url, type: 'audio')
end

#reply_with_buttons(text:, buttons:) ⇒ Object

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bobot/events/common.rb', line 126

def reply_with_buttons(text:, buttons:)
  raise Bobot::FieldFormat.new('text is required.') unless text.present?
  raise Bobot::FieldFormat.new('text length is limited to 640.') if text.size > 640
  raise Bobot::FieldFormat.new('buttons are required.') unless buttons.present?
  raise Bobot::FieldFormat.new('buttons are limited to 3.') if buttons.size > 3
  reply(
    payload_message: {
      attachment: {
        type: 'template',
        payload: {
          template_type: 'button',
          text: text,
          buttons: buttons,
        },
      },
    },
  )
end

#reply_with_file(url:) ⇒ Object



109
110
111
# File 'lib/bobot/events/common.rb', line 109

def reply_with_file(url:)
  reply_with_attachment(url: url, type: 'file')
end

#reply_with_generic(elements:, image_aspect_ratio: 'square') ⇒ Object Also known as: reply_with_carousel

Raises:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bobot/events/common.rb', line 145

def reply_with_generic(elements:, image_aspect_ratio: 'square')
  raise Bobot::FieldFormat.new('elements are required.') if elements.nil?
  raise Bobot::FieldFormat.new('elements are limited to 10.') 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.') unless %w[square horizontal].include?(image_aspect_ratio)
  reply(
    payload_message: {
      attachment: {
        type: 'template',
        payload: {
          template_type: 'generic',
          image_aspect_ratio: image_aspect_ratio,
          elements: elements,
        },
      },
    },
  )
end

#reply_with_image(url:) ⇒ Object



97
98
99
# File 'lib/bobot/events/common.rb', line 97

def reply_with_image(url:)
  reply_with_attachment(url: url, type: 'image')
end

#reply_with_quick_replies(text:, quick_replies:) ⇒ Object

Raises:



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bobot/events/common.rb', line 113

def reply_with_quick_replies(text:, quick_replies:)
  raise Bobot::FieldFormat.new('text is required.') unless text.present?
  raise Bobot::FieldFormat.new('text length is limited to 640.') 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.') if quick_replies.size > 11
  reply(
    payload_message: {
      text: text,
      quick_replies: quick_replies,
    },
  )
end

#reply_with_text(text:) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/bobot/events/common.rb', line 71

def reply_with_text(text:)
  raise Bobot::FieldFormat.new('text is required.') unless text.present?
  raise Bobot::FieldFormat.new('text length is limited to 640.') if text.size > 640
  reply(
    payload_message: {
      text: text,
    },
  )
end

#reply_with_video(url:) ⇒ Object



105
106
107
# File 'lib/bobot/events/common.rb', line 105

def reply_with_video(url:)
  reply_with_attachment(url: url, type: 'video')
end

#senderObject



19
20
21
# File 'lib/bobot/events/common.rb', line 19

def sender
  @messaging['sender']
end

#sender_action(sender_action:) ⇒ Object



55
56
57
# File 'lib/bobot/events/common.rb', line 55

def sender_action(sender_action:)
  deliver(payload_template: { sender_action: sender_action })
end

#sent_atObject



38
39
40
# File 'lib/bobot/events/common.rb', line 38

def sent_at
  Time.zone.at(@messaging['timestamp'] / 1000)
end

#show_typing(state:) ⇒ Object



59
60
61
# File 'lib/bobot/events/common.rb', line 59

def show_typing(state:)
  sender_action(sender_action: state ? 'typing_on' : 'typing_off')
end