Class: Kogno::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/core/lib/notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient = nil, msg = nil) ⇒ Notification

attr_accessor :template attr_accessor :user, :msg, :context attr_accessor :nlp



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

def initialize(recipient=nil,msg=nil)
  @messages = []
  @inline_query_results = []
  @before_messages = []
  @after_messages = []
  @vars = {}
  @recipient = recipient
  @user = @recipient if @recipient.type == :user
  @message = msg
  @response_log = []
  @message_log = []
  @context_obj_for_tilt_template = nil
end

Class Method Details

.rand_text(text) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/core/lib/notification.rb', line 164

def self.rand_text(text)    
  if text.class == Array
    return(text[rand(text.count)])
  else
    return(text)
  end
end

Instance Method Details

#answer_callback_query(params = {}, callback_query_id = nil) ⇒ Object



276
277
278
# File 'lib/core/lib/notification.rb', line 276

def answer_callback_query(params={},callback_query_id=nil)
  logger.write "---- answer_callback_query not implemented yet for this platform ----", :red  
end

#answer_inline_query(inline_query_id, delete = true) ⇒ Object



272
273
274
# File 'lib/core/lib/notification.rb', line 272

def answer_inline_query(inline_query_id, delete=true)
  logger.write "---- answer_inline_query not implemented yet for this platform ----", :red  
end

#button(text, buttons, typed_postbacks = true) ⇒ Object



136
137
138
# File 'lib/core/lib/notification.rb', line 136

def button(text,buttons,typed_postbacks=true)
  logger.write "---- button not implemented yet for this platform ----", :red  
end


152
153
154
# File 'lib/core/lib/notification.rb', line 152

def carousel(elements,last_element={},image_aspect_ratio=:horizontal , quick_replies=[])
  logger.write "---- carousel not implemented yet for this platform ----", :red  
end

#confirm(question, params, type = :quick_reply) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/core/lib/notification.rb', line 300

def confirm(question,params,type=:quick_reply)
  # params example:
  # {
  #   yes: {title: "Yes", payload: true}},
  #   no: {title: "No", :payload: false}
  # }
  if type == :quick_reply
    self.quick_reply(
      question,
      [
        {
          :content_type => :text,
          :title => params[:yes][:title],
          :payload => params[:yes][:payload]
        },
        {
          :content_type => :text,
          :title => params[:no][:title],
          :payload => params[:no][:payload],
        }
      ],
      {typed_postbacks: true}
    )
  elsif type == :button
    self.button(
      question,
      [
        {
          :type => :postback,
          :title => params[:accept_title],
          :payload => params[:accept_payload]
        },
        {
          :type => :postback,
          :title => params[:reject_title],
          :payload => params[:reject_payload]
        }
      ]
    )
  end
  self.set_typed_postbacks({"YES"=>params[:accept_payload], "NO"=> params[:reject_payload], "CANCEL" => params[:reject_payload]})
end

#contact(params) ⇒ Object



268
269
270
# File 'lib/core/lib/notification.rb', line 268

def contact(params)
  logger.write "---- contact not implemented yet for this platform ----", :red  
end

#delete_messagesObject



98
99
100
101
102
# File 'lib/core/lib/notification.rb', line 98

def delete_messages()
  @before_messages = []
  @messages = []
  @after_messages = []
end

#destroy_message_logObject



62
63
64
# File 'lib/core/lib/notification.rb', line 62

def destroy_message_log
  @message_log = []
end

#document(params) ⇒ Object



264
265
266
# File 'lib/core/lib/notification.rb', line 264

def document(params)
  logger.write "---- document not implemented yet for this platform ----", :red  
end

#export_messagesObject



90
91
92
93
94
95
96
# File 'lib/core/lib/notification.rb', line 90

def export_messages

  messages = @messages.to_json
  self.delete_messages()
  return messages

end

#get_psid_from_response_logObject



66
67
68
# File 'lib/core/lib/notification.rb', line 66

def get_psid_from_response_log
  return(@response_log.first[:recipient_id] rescue nil)
end

#html(code, reply_markup = {}, extra_params = {}) ⇒ Object



284
285
286
# File 'lib/core/lib/notification.rb', line 284

def html(code, reply_markup = {} ,extra_params={})
  logger.write "---- html not implemented yet for this platform ----", :red  
end

#html_template(action_group, action, params, reply_markup = {}, extra_params = {}) ⇒ Object



288
289
290
# File 'lib/core/lib/notification.rb', line 288

def html_template(action_group, action, params, reply_markup = {} ,extra_params={})
  logger.write "---- html_template not implemented yet for this platform ----", :red  
end

#image(params) ⇒ Object



256
257
258
# File 'lib/core/lib/notification.rb', line 256

def image(params)
  logger.write "---- image not implemented yet for this platform ----", :red  
end

#import_messages(messages, position = "") ⇒ Object



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

def import_messages(messages,position="")
  messages = messages.class == Array ? messages : JSON.parse(messages,{:symbolize_names => true})
  case position.to_sym
    when :before
      @before_messages = messages
    when :after
      @after_messages = messages
    else
      @messages = messages
  end
end

#list(elements, buttons = []) ⇒ Object



140
141
142
# File 'lib/core/lib/notification.rb', line 140

def list(elements,buttons=[])
  logger.write "---- list not implemented yet for this platform ----", :red  
end

#loading_typing_off(recipient_id) ⇒ Object



116
117
118
# File 'lib/core/lib/notification.rb', line 116

def loading_typing_off(recipient_id)
  logger.write "---- loading_typing_off not implemented yet for this platform ----", :red  
end

#loading_typing_on(recipient_id) ⇒ Object



112
113
114
# File 'lib/core/lib/notification.rb', line 112

def loading_typing_on(recipient_id)
  logger.write "---- loading_typing_on not implemented yet for this platform ----", :red  
end

#location(params) ⇒ Object



252
253
254
# File 'lib/core/lib/notification.rb', line 252

def location(params)
  logger.write "---- location not implemented yet for this platform ----", :red  
end

#location_request(text) ⇒ Object

def opt_out(text, button_label)

self.quick_reply(
  text,
  [
    {
      :content_type => :text,
      :title => button_label,
      :payload => PAYLOADS[:opt_out]
    }
  ]
)

end



244
245
246
# File 'lib/core/lib/notification.rb', line 244

def location_request(text) # Deprecated by Facebook
  logger.write "---- location_request not implemented yet for this platform ----", :red  
end

#markdown(params) ⇒ Object



296
297
298
# File 'lib/core/lib/notification.rb', line 296

def markdown(params)
  logger.write "---- markdown not implemented yet for this platform ----", :red  
end

#match_next_message(users, messaging_type) ⇒ Object



74
75
76
# File 'lib/core/lib/notification.rb', line 74

def match_next_message(users,messaging_type)
  logger.write "---- match_next_message isn't working for this platform ----", :red  
end

#message_logObject



58
59
60
# File 'lib/core/lib/notification.rb', line 58

def message_log
  @message_log
end

#messenger_generic_template(elements, image_aspect_ratio = :horizontal, quick_replies = []) ⇒ Object



144
145
146
# File 'lib/core/lib/notification.rb', line 144

def messenger_generic_template(elements,image_aspect_ratio=:horizontal, quick_replies=[])
  logger.write "---- messenger_generic_template not implemented yet for this platform ----", :red  
end

#payload(payload, params = {}) ⇒ Object



188
189
190
# File 'lib/core/lib/notification.rb', line 188

def payload(payload,params={})
  "#{payload}:#{params.to_json}"
end

#push_inline_query_result(result) ⇒ Object



280
281
282
# File 'lib/core/lib/notification.rb', line 280

def push_inline_query_result(result)
  logger.write "---- push_inline_query_result not implemented yet for this platform ----", :red  
end

#push_message(message, type = :message) ⇒ Object

:message o :action



182
183
184
185
186
# File 'lib/core/lib/notification.rb', line 182

def push_message(message,type=:message) # :message o :action
  new_message = {:type => type, :value => message}
  logger.debug_json new_message, :blue
  @messages << new_message
end

#quick_reply(text, replies, typed_postbacks = true) ⇒ Object



132
133
134
# File 'lib/core/lib/notification.rb', line 132

def quick_reply(text,replies,typed_postbacks=true)
  logger.write "---- quick_reply not implemented yet for this platform ----", :red  
end

#raw(params, type = :message) ⇒ Object



104
105
106
# File 'lib/core/lib/notification.rb', line 104

def raw(params, type=:message)
  self.push_message(params, type)
end

#recurring_notification_request(args) ⇒ Object



156
157
158
# File 'lib/core/lib/notification.rb', line 156

def recurring_notification_request(args)
  logger.write "---- recurring_notification_request not implemented yet for this platform ----", :red 
end

#render_html_template(action_group, action, params) ⇒ Object



292
293
294
# File 'lib/core/lib/notification.rb', line 292

def render_html_template(action_group, action, params)
  logger.write "---- render_html_template not implemented yet for this platform ----", :red  
end

#replace_place_holders(message) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/core/lib/notification.rb', line 173

def replace_place_holders(message)
  message_in_json = message.to_json
  @vars.each do |place_holder,replacement_value|
   message_in_json = message_in_json.gsub(":::#{place_holder}:::",replacement_value.to_s)
  end
  JSON.parse(message_in_json)
end

#response_logObject



54
55
56
# File 'lib/core/lib/notification.rb', line 54

def response_log
  @response_log
end

#scheduled(send_at, tag = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/core/lib/notification.rb', line 34

def scheduled(send_at, tag=nil)
  return false unless @recipient.type == :user
  unless @recipient.nil?
    unless @messages.empty?
      @recipient.scheduled_messages.create({
        messages: @messages.to_json,
        tag: tag,
        send_at: send_at
      })
      self.delete_messages()
    else
      logger.write "Error: No messages to send", :red
    end
  end
end

#send(messaging_type = "RESPONSE", recipient_id = nil, page_id = nil, delete = true) ⇒ Object



26
27
28
# File 'lib/core/lib/notification.rb', line 26

def send(messaging_type="RESPONSE",recipient_id=nil,page_id=nil,delete=true)
  logger.write "---- send() not implemented yet for this platform ----", :red  
end

#send_multiple(users, messaging_type) ⇒ Object



70
71
72
# File 'lib/core/lib/notification.rb', line 70

def send_multiple(users,messaging_type)
  logger.write "---- send_multiple not implemented yet for this platform ----", :red  
end

#send_private_reply(recipient_id = nil, page_id = nil, delete = true) ⇒ Object



50
51
52
# File 'lib/core/lib/notification.rb', line 50

def send_private_reply(recipient_id=nil,page_id=nil,delete=true)
  logger.write "---- send_private_reply not implemented yet for this platform ----", :red  
end

#send_using_tokenObject



30
31
32
# File 'lib/core/lib/notification.rb', line 30

def send_using_token()
  logger.write "---- send_with_token() not implemented yet for this platform ----", :red  
end

#set_context(context) ⇒ Object



22
23
24
# File 'lib/core/lib/notification.rb', line 22

def set_context(context)
  @context = context
end

#set_typed_postbacks(typed_postbacks) ⇒ Object

def partial_block(action_group,action,params={},&block)

@reply = self
eval(template(action_group,"_#{action}",params,true))

end



223
224
225
226
227
# File 'lib/core/lib/notification.rb', line 223

def set_typed_postbacks(typed_postbacks)
  return false unless @recipient.type == :user
  @recipient.vars[:typed_postbacks] = {} if @recipient.vars[:typed_postbacks].nil?
  @recipient.vars[:typed_postbacks] = @recipient.vars[:typed_postbacks].merge(typed_postbacks)
end

#set_vars(vars) ⇒ Object



160
161
162
# File 'lib/core/lib/notification.rb', line 160

def set_vars(vars)
  @vars = @vars.merge(vars)
end

#template(route, params = {}) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/core/lib/notification.rb', line 192

def template(route,params={})
  map = digest_template_route(route)
  if map == false
    return false
  else
    action_group = map[:action_group]
    action = map[:action]
  end

  if @context.nil?
    @reply = self
    Context.base_template(action_group,action,params,false, self)
  else
    @context.template(action_group,action,params)
  end
end

#text(text) ⇒ Object



108
109
110
# File 'lib/core/lib/notification.rb', line 108

def text(text)
  logger.write "---- text not implemented yet for this platform ----", :red  
end

#typing(seconds) ⇒ Object



124
125
126
# File 'lib/core/lib/notification.rb', line 124

def typing(seconds)
  typing_on(seconds.to_i)
end

#typing_offObject



128
129
130
# File 'lib/core/lib/notification.rb', line 128

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

#typing_on(duration) ⇒ Object



120
121
122
# File 'lib/core/lib/notification.rb', line 120

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

#url(params) ⇒ Object



248
249
250
# File 'lib/core/lib/notification.rb', line 248

def url(params)
  logger.write "---- url not implemented yet for this platform ----", :red  
end

#video(params) ⇒ Object



260
261
262
# File 'lib/core/lib/notification.rb', line 260

def video(params)
  logger.write "---- video not implemented yet for this platform ----", :red  
end

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



148
149
150
# File 'lib/core/lib/notification.rb', line 148

def whatsapp_template(name, components=[], lang="en_US")
  logger.write "---- whatsapp_template not implemented yet for this platform ----", :red  
end