Class: Kogno::Messenger::PostComment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, page_id) ⇒ PostComment

Returns a new instance of PostComment.



9
10
11
12
# File 'lib/core/lib/messenger/post_comment.rb', line 9

def initialize(data, page_id)
  @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/post_comment.rb', line 5

def event
  @event
end

Instance Method Details

#comment_idObject



46
47
48
# File 'lib/core/lib/messenger/post_comment.rb', line 46

def comment_id
  return(@data[:value][:comment_id] rescue nil)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  self.text.to_s.empty?
end

#get_context(user, notification) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/core/lib/messenger/post_comment.rb', line 50

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

#handle_event(debug = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/core/lib/messenger/post_comment.rb', line 62

def handle_event(debug=false)

  begin

    user = User.new(last_usage_at: Time.now, vars:{})
    self.set_nlp(user.locale)

    notification = Notification.new(user,self)

    logger.write "--------------------MESSAGE: #{self.text}--------------------------", :green

    context = get_context(user,notification)

    return({post_comment: self, user: user, notification: notification, context: context}) if debug

    context.run_for_text_only

    notification.send_private_reply(self.comment_id)

    recipient_psid = notification.get_psid_from_response_log

    user = User.find_or_create_by_psid(recipient_psid, :messenger, self.page_id, self.sender_id, false)

    if Kogno::Application.config.
      user.log_message(self, :post_comment_received) 
      user.log_response(notification)
    end

    logger.write "**********************#{user.context}***********************", :green
   
  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

#item_typeObject



38
39
40
# File 'lib/core/lib/messenger/post_comment.rb', line 38

def item_type
  return(@data[:value][:item].to_sym rescue nil)
end

#page_idObject



34
35
36
# File 'lib/core/lib/messenger/post_comment.rb', line 34

def page_id
  @page_id
end

#payload_actionObject



18
19
20
# File 'lib/core/lib/messenger/post_comment.rb', line 18

def payload_action
  nil
end

#post_idObject



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

def post_id
  return(@data[:value][:post_id] rescue nil)
end

#sender_idObject



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

def sender_id
  return(@data[:value][:from][:id] rescue nil)
end

#sender_nameObject



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

def sender_name
  return(@data[:value][:from][:name] rescue nil)
end

#textObject



42
43
44
# File 'lib/core/lib/messenger/post_comment.rb', line 42

def text
  return(@data[:value][:message] rescue "")
end

#typeObject



14
15
16
# File 'lib/core/lib/messenger/post_comment.rb', line 14

def type
  :post_comment
end