Class: Kogno::Messenger::RecurringNotification

Inherits:
Kogno::Message show all
Defined in:
lib/core/lib/messenger/recurring_notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Kogno::Message

#attachments, #deep_link_data, #deep_link_param, #empty?, #empty_thread_from_ad?, #location, #log_message_info, #numbers_in_text, #overwrite_postback, #params, #payload, #payload_action, #postback_params, #postback_payload, #raw_message, #referral, #set_nlp, #stickers, #text, #webhook_data

Constructor Details

#initialize(data) ⇒ RecurringNotification

Returns a new instance of RecurringNotification.



10
11
12
13
# File 'lib/core/lib/messenger/recurring_notification.rb', line 10

def initialize(data)
  @data = data
  @page_id = page_id
end

Instance Attribute Details

#eventObject

Returns the value of attribute event.



5
6
7
# File 'lib/core/lib/messenger/recurring_notification.rb', line 5

def event
  @event
end

#nlpObject

Returns the value of attribute nlp.



6
7
8
# File 'lib/core/lib/messenger/recurring_notification.rb', line 6

def nlp
  @nlp
end

Instance Method Details

#get_context(user, notification) ⇒ Object



40
41
42
43
44
45
# File 'lib/core/lib/messenger/recurring_notification.rb', line 40

def get_context(user,notification)
  # Here you can use different contexts by self.post_id
  context_class = Context::router(Kogno::Application.config.routes.recurring_notification)[:class]
  context = context_class.new(user,self,notification,nil)
  return(context)
end

#handle_event(debug = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/core/lib/messenger/recurring_notification.rb', line 47

def handle_event(debug=false)

  begin

    user = User.find_or_create_by_psid(self.sender_id, :messenger, self.page_id)
    user.update_messenger_recurring_notification(@data[:optin])
    user.get_session_vars
    self.set_nlp(user.locale)
    I18n.locale = user.locale unless user.locale.nil?

    notification = Notification.new(user,self)

    context = get_context(user,notification)
    context.run_for_recurring_notification_only

    notification.send

    if Kogno::Application.config.
      user.log_message(self, :recurring_notifications) 
      user.log_response(notification)
    end
   
  rescue StandardError => e
    error_token = Digest::MD5.hexdigest("#{Time.now}#{rand(1000)}") # This helps to identify the error that arrives to Slack in order to search it in logs/http.log      
    logger.write e.message, :red
    logger.write "Error Token: #{error_token}", :red
    logger.write "Backtrace:\n\t#{e.backtrace.join("\n\t")}", :red
    ErrorHandler.notify_by_slack(Kogno::Application.config.app_name,e, error_token) if Kogno::Application.config.error_notifier.slack[:enable] rescue false
  end

end

#notification_messages_statusObject



32
33
34
35
36
37
38
# File 'lib/core/lib/messenger/recurring_notification.rb', line 32

def notification_messages_status
  if @data[:optin][:notification_messages_status] == "STOP_NOTIFICATIONS"
    :stopped
  else
    :active
  end
end

#page_idObject



23
24
25
# File 'lib/core/lib/messenger/recurring_notification.rb', line 23

def page_id
  return @data[:recipient][:id]
end

#raw_payloadObject



27
28
29
30
# File 'lib/core/lib/messenger/recurring_notification.rb', line 27

def raw_payload
  payload = @data[:optin][:payload] rescue nil
  return(payload)
end

#sender_idObject



19
20
21
# File 'lib/core/lib/messenger/recurring_notification.rb', line 19

def sender_id
  return @data[:sender][:id]
end

#typeObject



15
16
17
# File 'lib/core/lib/messenger/recurring_notification.rb', line 15

def type
  :recurring_notification
end