Class: Kogno::Messenger::Message
Instance Attribute Summary
#event
Instance Method Summary
collapse
#deep_link_data, #deep_link_param, #empty?, #empty_thread_from_ad?, #location, #log_message_info, #nlp, #numbers_in_text, #overwrite_postback, #params, #payload, #payload_action, #postback_params, #postback_payload, #set_nlp, #type, #webhook_data
Constructor Details
#initialize(data) ⇒ Message
Returns a new instance of Message.
5
6
7
|
# File 'lib/core/lib/messenger/message.rb', line 5
def initialize(data)
@data = data
end
|
Instance Method Details
#attachments ⇒ Object
32
33
34
35
36
|
# File 'lib/core/lib/messenger/message.rb', line 32
def attachments
attachments = nil
attachments = @data[:message][:attachments] rescue nil
attachments
end
|
#digest_referral_params(params) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/core/lib/messenger/message.rb', line 67
def digest_referral_params(params)
dparams = {}
return dparams if params.nil?
dparams = (JSON.parse(params,{:symbolize_names => true}) rescue {})
if dparams.empty?
params.to_s.split(";").each do |param|
item = param.split(":")
if item.length > 1
dparams[item[0].to_sym] = item[1]
else
break
end
end
end
dparams = params if dparams.empty?
return dparams
end
|
#get_context(user, msg, notification) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/core/lib/messenger/message.rb', line 117
def get_context(user,msg,notification)
if !(context_from_postback = Context.get_from_payload(msg.payload)).nil?
context_route = context_from_postback
elsif !(context_in_link = Context.get(self.referral(:ref))).nil?
context_route = context_in_link[:context]
@data[:deep_link_param] = context_in_link[:param]
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/core/lib/messenger/message.rb', line 140
def handle_event(debug=false)
begin
user = User.find_or_create_by_psid(self.sender_id, :messenger, self.page_id)
user.get_session_vars
self.set_nlp(user.locale)
I18n.locale = user.locale unless user.locale.nil?
unless user.vars[:nlp_context_ref].nil?
self.nlp.set_context_reference(user.vars[:nlp_context_ref])
user.vars.delete(:nlp_context_ref) 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.store_log_in_database
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.store_log_in_database
response_log = user.log_response(notification)
response_log_id = response_log.id unless response_log.nil?
end
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.store_log_in_database
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)}") 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
|
#page_id ⇒ Object
17
18
19
|
# File 'lib/core/lib/messenger/message.rb', line 17
def page_id
return @data[:recipient][:id]
end
|
9
10
11
|
# File 'lib/core/lib/messenger/message.rb', line 9
def platform
:messenger
end
|
#raw_message ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/core/lib/messenger/message.rb', line 85
def raw_message
m = @data[:message]
return nil if m.nil?
if !m[:text].nil?
return({
:type => :text,
:value => m[:text]
})
elsif !m[:attachments].nil?
m[:attachments].each do |attachment|
case attachment[:type]
when "location"
return({
:type => :location,
:value => {
:lat => attachment[:payload][:coordinates][:lat],
:lon => attachment[:payload][:coordinates][:long]
}
})
end
end
end
end
|
#raw_payload ⇒ Object
21
22
23
24
25
26
|
# File 'lib/core/lib/messenger/message.rb', line 21
def raw_payload
payload = @data[:postback][:payload] rescue nil
payload = (@data[:message][:quick_reply][:payload] rescue nil) if payload.nil?
payload = @overwritten_payload if payload.nil?
return(payload)
end
|
#referral(field = nil) ⇒ Object
Field could be :context or :params)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/core/lib/messenger/message.rb', line 38
def referral(field=nil) referral = (@data[:postback][:referral] rescue nil)
referral = @data[:referral] if referral.nil?
if referral.nil? || referral == ""
return nil
elsif field.nil?
return referral
elsif field == :source
return referral[:source]
elsif field == :type
return referral[:type]
elsif field == :ref
return(referral[:ref] == "" ? nil : referral[:ref])
else
ref = (referral[:ref].split(Regexp.union(["/","-"]),2) rescue [])
if field == :context
return (ref[0] rescue nil)
elsif field == :params
return digest_referral_params(ref[1])
else
return nil
end
end
end
|
#sender_id ⇒ Object
13
14
15
|
# File 'lib/core/lib/messenger/message.rb', line 13
def sender_id
return @data[:sender][:id]
end
|
#stickers ⇒ Object
28
29
30
|
# File 'lib/core/lib/messenger/message.rb', line 28
def stickers
@data[:message][:attachments].map{|a| a[:payload][:sticker_id]}.compact rescue []
end
|
#text ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/core/lib/messenger/message.rb', line 109
def text
if (self.raw_message[:type] == :text rescue false)
return self.raw_message[:value].to_s
else
return ""
end
end
|