Class: Kogno::WhatsApp::Notification

Inherits:
Notification show all
Defined in:
lib/core/lib/whatsapp/notification.rb

Instance Method Summary collapse

Methods inherited from Notification

#answer_callback_query, #answer_inline_query, #carousel, #confirm, #delete_messages, #destroy_message_log, #export_messages, #get_psid_from_response_log, #html, #html_template, #import_messages, #initialize, #loading_typing_off, #loading_typing_on, #location_request, #markdown, #match_next_message, #message_log, #messenger_generic_template, #payload, #push_inline_query_result, #push_message, rand_text, #raw, #recurring_notification_request, #render_html_template, #replace_place_holders, #response_log, #scheduled, #send_multiple, #send_private_reply, #send_using_token, #set_context, #set_typed_postbacks, #set_vars, #template, #typing, #typing_off

Constructor Details

This class inherits a constructor from Kogno::Notification

Instance Method Details

#button(text, replies, extra_settings = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/core/lib/whatsapp/notification.rb', line 42

def button(text, replies, extra_settings={})
  replies = [replies] if replies.class == Hash
  settings = {typed_postbacks: Kogno::Application.config.typed_postbacks}.merge(extra_settings) # defaults
  replies = replies.replace_keys({payload: :id})
  buttons = replies.map do |reply|
    {
      type: :reply,
      reply: reply
    }
  end

  set_typed_postbacks(replies.map{|button|
    [button[:title].to_payload, button[:id]] unless button[:title].nil?
  }.compact.to_h) if settings[:typed_postbacks]

  self.push_message(Api::interactive_buttons(text, buttons), :message)
end

#contact(params) ⇒ Object



198
199
200
# File 'lib/core/lib/whatsapp/notification.rb', line 198

def contact(params)
  self.push_message(Api::contacts(params), :message)
end

#document(params) ⇒ Object



194
195
196
# File 'lib/core/lib/whatsapp/notification.rb', line 194

def document(params)
  self.push_message(Api::media(:document, params), :message)
end

#image(params) ⇒ Object



94
95
96
97
98
99
100
101
102
103
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
134
135
136
137
138
139
140
141
142
# File 'lib/core/lib/whatsapp/notification.rb', line 94

def image(params)
  # params = params.replace_keys({url: :link})
  if params[:buttons].nil?

    self.raw(
      {  
        type: :image,
        image: {
          link: params[:url],
          caption: params[:caption]
        }
      }
    )

  else

    buttons = params[:buttons]
    buttons = [buttons] if buttons.class == Hash
    buttons = buttons.replace_keys({payload: :id})
    replies = buttons.map do |reply|
      {
        type: :reply,
        reply: reply
      }
    end
    self.raw(
      {  
        type: :interactive,
        interactive: {
          type: :button,
          header: {
            type: :image,
            image: {
              link: params[:url]
            }
          },
          body:{
            text: params[:caption]
          },
          action:{
            buttons: replies
          }
        }
      }
    )

  end

end

#list(params, header = {}, footer = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/core/lib/whatsapp/notification.rb', line 64

def list(params,header={},footer={})
  params[:sections] = params[:sections].map{|s| 
    {
      title: s[:title],
      rows: s[:rows].replace_keys({payload: :id})
    }
  }

  params[:header] = header unless header.empty?
  params[:footer] = footer unless footer.empty?

  self.push_message(Api::interactive_list(params), :message)
end

#location(params) ⇒ Object



90
91
92
# File 'lib/core/lib/whatsapp/notification.rb', line 90

def location(params)
  self.push_message(Api::location(params), :message)
end

#quick_reply(text, replies, extra_settings = {}) ⇒ Object



60
61
62
# File 'lib/core/lib/whatsapp/notification.rb', line 60

def quick_reply(text, replies, extra_settings={})
  self.button(text, replies, extra_settings)
end

#send(recipient_id = nil, delete = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/core/lib/whatsapp/notification.rb', line 5

def send(recipient_id=nil,delete=true)    
  recipient_id = @recipient.psid if recipient_id.nil?
  page_id = @recipient.page_id if page_id.nil?
  messages = @before_messages+@messages+@after_messages
  @message_log = messages
  messages.each do |message|
    if message[:type].to_sym == :action
      sleep(message[:value][:duration]) if message[:value][:action].to_sym == :typing_on
    else
      message = self.replace_place_holders(message[:value])
      @response_log << Api::send(recipient_id,message)
    end
  end    
  self.delete_messages() if delete
  @recipient.mark_last_message_as_unread unless @recipient.nil?
end

#text(text, extra_params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/core/lib/whatsapp/notification.rb', line 22

def text(text, extra_params={})
  extra_params = {preview_url: false}.merge(extra_params)  
  params = {
    type: :text,
    text: {
      body: text
    }.merge(extra_params)
  }  
  self.push_message(params, :message)
end

#typing_on(duration) ⇒ Object



34
35
36
# File 'lib/core/lib/whatsapp/notification.rb', line 34

def typing_on(duration)
  self.push_message({:action => :typing_on, :duration => duration}, :action)
end

#url(params) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core/lib/whatsapp/notification.rb', line 78

def url(params)
  self.raw(
      {  
        type: :image,
        image: {          
          link: params[:image],
          caption: "#{params[:title]}\n#{params[:sub_title]}\n#{params[:url]}"
        }
      }
    )
end

#video(params) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/core/lib/whatsapp/notification.rb', line 144

def video(params)

  if params[:buttons].nil?

    self.raw(
      {  
        type: :video,
        video: {
          link: params[:url],
          caption: params[:caption]
        }
      }
    )

  else

    buttons = params[:buttons]
    buttons = [buttons] if buttons.class == Hash
    buttons = buttons.replace_keys({payload: :id})
    replies = buttons.map do |reply|
      {
        type: :reply,
        reply: reply
      }
    end
    self.raw(
      {  
        type: :interactive,
        interactive: {
          type: :button,
          header: {
            type: :video,
            video: {
              link: params[:url]
            }
          },
          body:{
            text: params[:caption]
          },
          action:{
            buttons: replies
          }
        }
      }
    )

  end

end

#whatsapp_template(name, components = [], lang = "en_US") ⇒ Object



38
39
40
# File 'lib/core/lib/whatsapp/notification.rb', line 38

def whatsapp_template(name, components=[], lang="en_US")
  self.push_message(Api::template_message(name, components, lang), :message)
end