Class: Kogno::WhatsApp::Message

Inherits:
Message
  • Object
show all
Defined in:
lib/core/lib/whatsapp/message.rb

Instance Attribute Summary

Attributes inherited from Message

#event

Instance Method Summary collapse

Methods inherited from Message

#deep_link_data, #deep_link_param, #empty?, #empty_thread_from_ad?, #get_context, #location, #log_message_info, #nlp, #numbers_in_text, #overwrite_postback, #page_id, #params, #payload, #payload_action, #postback_params, #postback_payload, #referral, #set_nlp, #stickers, #webhook_data

Constructor Details

#initialize(data, type = nil) ⇒ Message

Returns a new instance of Message.



7
8
9
10
# File 'lib/core/lib/whatsapp/message.rb', line 7

def initialize(data, type=nil)
  @data = data
  @type = type
end

Instance Method Details

#attachmentsObject



32
33
34
35
36
37
38
39
40
# File 'lib/core/lib/whatsapp/message.rb', line 32

def attachments
  message = @data[:messages][0]
  a = nil
  a = message[:audio]
  a = message[:document] if a.nil?
  a = message[:image] if a.nil?
  a = message[:video] if a.nil?
  return a
end

#handle_event(debug = false) ⇒ Object



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
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
# File 'lib/core/lib/whatsapp/message.rb', line 66

def handle_event(debug=false)

  begin

    user = User.find_or_create_by_psid(self.sender_id, :whatsapp)
    user.get_session_vars

    self.set_nlp(user.locale)

    I18n.locale = user.locale unless user.locale.nil?

    self.set_nlp(I18n.locale)

    unless user.vars[:nlp_context_ref].nil?
      self.nlp.set_context_reference(user.vars[:nlp_context_ref])
      user.vars.delete(:nlp_context_ref) # context references will only be used once
    end

    notification = Notification.new(user,self)

    self.log_message_info(user)

    context = get_context(user,self,notification)

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

    unless empty_thread_from_ad?

      called_action = context.run      
      if Kogno::Application.config.   
        message_log_id = user.log_message(self).id
      else
        message_chat_log_id = 0
      end     
           
      notification.send

      response_log_id = 0
      if Kogno::Application.config.
        response_log = user.log_response(notification)          
        response_log_id = response_log.id unless response_log.nil?        
      end  

      # user.set_last_usage
      user.save_session_vars
      context.handle_message_from_memory

    else
      context.run_class_callbacks_only
      user.save_session_vars
      user.log_message(self) if Kogno::Application.config.         
    end
    logger.write "- Current user context: #{user.context}", :blue unless user.context.nil?
   
  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

#metadataObject



20
21
22
# File 'lib/core/lib/whatsapp/message.rb', line 20

def 
  @data[:metadata]
end

#platformObject



16
17
18
# File 'lib/core/lib/whatsapp/message.rb', line 16

def platform
  :whatsapp
end

#raw_messageObject



43
44
45
46
47
48
49
50
51
# File 'lib/core/lib/whatsapp/message.rb', line 43

def raw_message
  t = @data[:messages][0][:text][:body] rescue nil
  unless t.nil?
    return({
      :type => :text,
      :value => t
    })
  end
end

#raw_payloadObject



53
54
55
56
57
58
59
# File 'lib/core/lib/whatsapp/message.rb', line 53

def raw_payload
  payload = @data[:messages][0][:button][:payload] rescue nil
  payload = (@data[:messages][0][:interactive][:button_reply][:id] rescue nil) if payload.nil?
  payload = (@data[:messages][0][:interactive][:list_reply][:id] rescue nil) if payload.nil?
  payload = @overwritten_payload if payload.nil?
  return(payload)
end

#sender_idObject



24
25
26
# File 'lib/core/lib/whatsapp/message.rb', line 24

def sender_id
  return @data[:contacts][0][:wa_id]
end

#sender_nameObject



28
29
30
# File 'lib/core/lib/whatsapp/message.rb', line 28

def sender_name
  return @data[:contacts][0][:profile][:name] rescue ""
end

#textObject



61
62
63
# File 'lib/core/lib/whatsapp/message.rb', line 61

def text
  return self.raw_message[:value].to_s rescue ""
end

#typeObject



12
13
14
# File 'lib/core/lib/whatsapp/message.rb', line 12

def type
  @type
end