Class: Stealth::Services::Facebook::ReplyHandler

Inherits:
BaseReplyHandler
  • Object
show all
Defined in:
lib/stealth/services/facebook/reply_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient_id: nil, reply: nil) ⇒ ReplyHandler

Returns a new instance of ReplyHandler.



12
13
14
15
# File 'lib/stealth/services/facebook/reply_handler.rb', line 12

def initialize(recipient_id: nil, reply: nil)
  @recipient_id = recipient_id
  @reply = reply
end

Instance Attribute Details

#recipient_idObject (readonly)

Returns the value of attribute recipient_id.



10
11
12
# File 'lib/stealth/services/facebook/reply_handler.rb', line 10

def recipient_id
  @recipient_id
end

#replyObject (readonly)

Returns the value of attribute reply.



10
11
12
# File 'lib/stealth/services/facebook/reply_handler.rb', line 10

def reply
  @reply
end

Instance Method Details

#audioObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stealth/services/facebook/reply_handler.rb', line 64

def audio
  check_if_arguments_are_valid!(
    suggestions: reply['suggestions'],
    buttons: reply['buttons']
  )

  template = unstructured_template
  attachment = attachment_template(
    attachment_type: 'audio',
    attachment_url: reply['audio_url']
  )
  template['message']['attachment'] = attachment

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  template
end

#cardsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stealth/services/facebook/reply_handler.rb', line 127

def cards
  template = card_template(
    sharable: reply["sharable"],
    aspect_ratio: reply["aspect_ratio"]
  )

  fb_elements = generate_card_elements(elements: reply["elements"])
  template["message"]["attachment"]["payload"]["elements"] = fb_elements

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  template
end

#delayObject



175
176
177
# File 'lib/stealth/services/facebook/reply_handler.rb', line 175

def delay
  enable_typing_indicator
end

#disable_typing_indicatorObject



171
172
173
# File 'lib/stealth/services/facebook/reply_handler.rb', line 171

def disable_typing_indicator
  sender_action_template(action: 'typing_off')
end

#enable_typing_indicatorObject



167
168
169
# File 'lib/stealth/services/facebook/reply_handler.rb', line 167

def enable_typing_indicator
  sender_action_template(action: 'typing_on')
end

#fileObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/stealth/services/facebook/reply_handler.rb', line 106

def file
  check_if_arguments_are_valid!(
    suggestions: reply['suggestions'],
    buttons: reply['buttons']
  )

  template = unstructured_template
  attachment = attachment_template(
    attachment_type: 'file',
    attachment_url: reply['file_url']
  )
  template['message']['attachment'] = attachment

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  template
end

#imageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/stealth/services/facebook/reply_handler.rb', line 43

def image
  check_if_arguments_are_valid!(
    suggestions: reply['suggestions'],
    buttons: reply['buttons']
  )

  template = unstructured_template
  attachment = attachment_template(
    attachment_type: 'image',
    attachment_url: reply['image_url']
  )
  template['message']['attachment'] = attachment

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  template
end

#listObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/stealth/services/facebook/reply_handler.rb', line 144

def list
  template = list_template(
    top_element_style: reply["top_element_style"]
  )

  fb_elements = generate_list_elements(elements: reply["elements"])
  template["message"]["attachment"]["payload"]["elements"] = fb_elements

  if reply["buttons"].present?
    if reply["buttons"].size > 1
      raise(ArgumentError, "Facebook lists support a single button attached to the list itsef.")
    end

    template["message"]["attachment"]["payload"]["buttons"] = generate_buttons(buttons: reply["buttons"])
  end

  template
end

#mark_seenObject



163
164
165
# File 'lib/stealth/services/facebook/reply_handler.rb', line 163

def mark_seen
  sender_action_template(action: 'mark_seen')
end

#messenger_profileObject

generates property/value pairs required to set the profile



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/stealth/services/facebook/reply_handler.rb', line 180

def messenger_profile
  unless Stealth.config.facebook.setup.present?
    raise Stealth::Errors::ConfigurationError, "Setup for Facebook is not specified in services.yml."
  end

  profile = {}
  Stealth.config.facebook.setup.each do |profile_option, _|
    profile[profile_option] = self.send(profile_option)
  end

  profile
end

#textObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stealth/services/facebook/reply_handler.rb', line 17

def text
  check_if_arguments_are_valid!(
    suggestions: reply['suggestions'],
    buttons: reply['buttons']
  )

  template = unstructured_template
  template['message']['text'] = reply['text']

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  # If buttons are present, we need to convert this to a button template
  if reply['buttons'].present?
    template['message'].delete('text')

    fb_buttons = generate_buttons(buttons: reply['buttons'])
    attachment = button_attachment_template(text: reply['text'], buttons: fb_buttons)
    template['message']['attachment'] = attachment
  end

  template
end

#videoObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stealth/services/facebook/reply_handler.rb', line 85

def video
  check_if_arguments_are_valid!(
    suggestions: reply['suggestions'],
    buttons: reply['buttons']
  )

  template = unstructured_template
  attachment = attachment_template(
    attachment_type: 'video',
    attachment_url: reply['video_url']
  )
  template['message']['attachment'] = attachment

  if reply['suggestions'].present?
    fb_suggestions = generate_suggestions(suggestions: reply['suggestions'])
    template["message"]["quick_replies"] = fb_suggestions
  end

  template
end