Class: Kogno::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eventObject

Returns the value of attribute event.



4
5
6
# File 'lib/core/lib/message.rb', line 4

def event
  @event
end

Instance Method Details

#attachmentsObject



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

def attachments
  []
end


32
33
34
# File 'lib/core/lib/message.rb', line 32

def deep_link_data
  nil
end


44
45
46
# File 'lib/core/lib/message.rb', line 44

def deep_link_param
  @data[:deep_link_param] rescue nil
end

#empty?Boolean

def event

JSON.parse(self.body,{:symbolize_names => true})

end

Returns:

  • (Boolean)


85
86
87
# File 'lib/core/lib/message.rb', line 85

def empty?
  self.raw_payload.nil? && self.raw_message.nil? && self.attachments.nil?
end

#empty_thread_from_ad?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/core/lib/message.rb', line 89

def empty_thread_from_ad?
  if self.empty?
    unless self.referral.nil?
      if self.referral(:source) == "ADS" and self.referral(:type) == "OPEN_THREAD"
        return true
      end
    end
  end    

  return false  
end

#get_context(user, msg, notification) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/core/lib/message.rb', line 140

def get_context(user,msg,notification)

  # Context::call_typed_postbacks(msg,user) if msg.payload.nil? && !user.vars[:typed_postbacks].nil?
  # user.vars.delete(:typed_postbacks)
  
  if !(context_from_postback = Context.get_from_payload(msg.payload)).nil?
    context_route = context_from_postback
  elsif !(context_from_typed_postback = Context.get_from_typed_postback(msg,user)).nil?    
    context_route = context_from_typed_postback
  else
    context_route = user.context      
  end

  context_class = Context.router(context_route, msg.type)[:class]
  if context_class.nil?
    user.exit_context
    context_class = Context.router(Kogno::Application.config.routes.default)[:class]
  end
  context = context_class.new(user,msg,notification,context_route)
  context
end

#handle_event(debug = false) ⇒ Object



163
164
165
# File 'lib/core/lib/message.rb', line 163

def handle_event(debug=false)
  # In order to implement in another platform, this function must exist  
end

#locationObject



132
133
134
135
136
137
138
# File 'lib/core/lib/message.rb', line 132

def location
  if (self.raw_message[:type] == :location rescue false)
    return self.raw_message[:value]
  else
    return nil
  end
end

#log_message_info(user) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/core/lib/message.rb', line 167

def log_message_info(user)
  context = user.context
  logger.write "\n-------- INCOMING MESSAGE -------", :yellow
  logger.write "Text    : \"#{self.text}\"", :yellow
  unless self.params.empty?
    logger.write "Payload : #{self.payload} => params: #{self.params}", :yellow
  else
    logger.write "Payload : #{self.payload}", :yellow
  end
  logger.write "User ID : #{user.psid}", :yellow
  logger.write "Context : #{(context.nil? || context.empty?) ? Kogno::Application.config.routes.default : context}", :yellow    
  logger.write "---------------------------------\n", :yellow
end

#nlpObject



40
41
42
# File 'lib/core/lib/message.rb', line 40

def nlp
  @nlp
end

#numbers_in_textObject



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

def numbers_in_text
  # self.text.scan(/\d+/).map{|n| n.to_i}
  self.text.scan(/(\d+(?:\.\d+)?)/).map{|n| n.first}
end

#overwrite_postback(payload) ⇒ Object



52
53
54
# File 'lib/core/lib/message.rb', line 52

def overwrite_postback(payload)
  @overwritten_payload = payload
end

#page_idObject



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

def page_id
  nil
end

#paramsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/core/lib/message.rb', line 101

def params
  raw_params = self.raw_payload.to_s.split(":",2)[1].to_s
  if raw_params.empty?
    return({})
  else  
    p = (JSON.parse(raw_params, {:symbolize_names => true}) rescue nil)
    p = raw_params if p.nil?
    return(p)
  end      
end

#payload(action_only = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/core/lib/message.rb', line 56

def payload(action_only=false)
  if action_only
    pl = self.raw_payload.split(":",2)[0] rescue nil
    if pl.nil?
      return pl
    else
      return pl.split(Regexp.union(["/","-"])).last
    end
  else
    self.raw_payload.split(":",2)[0] rescue nil
  end
end

#payload_actionObject



69
70
71
# File 'lib/core/lib/message.rb', line 69

def payload_action
  self.payload(true)
end

#postback_paramsObject



77
78
79
# File 'lib/core/lib/message.rb', line 77

def postback_params
  self.params
end

#postback_payloadObject



73
74
75
# File 'lib/core/lib/message.rb', line 73

def postback_payload
  self.payload(true)
end

#raw_messageObject



117
118
119
120
# File 'lib/core/lib/message.rb', line 117

def raw_message
  # In order to implement in another platform, this function must exist
  {}
end

#raw_payloadObject



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

def raw_payload
  # In order to implement in another platform, this function must exist
  nil
end

#referral(field = nil) ⇒ Object



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

def referral(field=nil)
  nil
end

#sender_idObject



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

def sender_id
  0
end

#set_nlp(lang) ⇒ Object



36
37
38
# File 'lib/core/lib/message.rb', line 36

def set_nlp(lang)
  @nlp = Nlp.new(self.text,lang)
end

#stickersObject



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

def stickers
  []
end

#textObject



122
123
124
125
# File 'lib/core/lib/message.rb', line 122

def text
  # In order to implement in another platform, this function must exist
  ""
end

#typeObject



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

def type
  :message
end

#webhook_dataObject



48
49
50
# File 'lib/core/lib/message.rb', line 48

def webhook_data
  @data
end