Class: Xip::Services::Facebook::ReplyHandler

Inherits:
BaseReplyHandler
  • Object
show all
Defined in:
lib/xip/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.



11
12
13
14
# File 'lib/xip/services/facebook/reply_handler.rb', line 11

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.



9
10
11
# File 'lib/xip/services/facebook/reply_handler.rb', line 9

def recipient_id
  @recipient_id
end

#replyObject (readonly)

Returns the value of attribute reply.



9
10
11
# File 'lib/xip/services/facebook/reply_handler.rb', line 9

def reply
  @reply
end

Instance Method Details

#audioObject



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

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



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

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



174
175
176
# File 'lib/xip/services/facebook/reply_handler.rb', line 174

def delay
  enable_typing_indicator
end

#disable_typing_indicatorObject



170
171
172
# File 'lib/xip/services/facebook/reply_handler.rb', line 170

def disable_typing_indicator
  sender_action_template(action: 'typing_off')
end

#enable_typing_indicatorObject



166
167
168
# File 'lib/xip/services/facebook/reply_handler.rb', line 166

def enable_typing_indicator
  sender_action_template(action: 'typing_on')
end

#fileObject



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

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



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

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



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

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



162
163
164
# File 'lib/xip/services/facebook/reply_handler.rb', line 162

def mark_seen
  sender_action_template(action: 'mark_seen')
end

#messenger_profileObject

generates property/value pairs required to set the profile



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

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

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

  profile
end

#textObject



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

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



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

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