Class: Kogno::Telegram::Message
- Defined in:
- lib/core/lib/telegram/message.rb
Instance Attribute Summary
Attributes inherited from Message
Instance Method Summary collapse
- #attachments ⇒ Object
- #callback_query_id ⇒ Object
- #chat ⇒ Object
- #chat_id ⇒ Object
- #chat_title ⇒ Object
- #chat_type ⇒ Object
- #command(command_only = true) ⇒ Object
- #command_text ⇒ Object
- #deep_link_data ⇒ Object
- #deep_link_params ⇒ Object
- #empty_thread_from_ad? ⇒ Boolean
- #get_context(user, notification, chat) ⇒ Object
- #get_kgn_id ⇒ Object
- #get_long_payload(data) ⇒ Object
- #handle_event(debug = false) ⇒ Object
-
#initialize(data, type = nil) ⇒ Message
constructor
A new instance of Message.
- #inviter_user_id ⇒ Object
- #location ⇒ Object
- #overwrite_postback(payload) ⇒ Object
- #page_id ⇒ Object
- #params ⇒ Object
- #payload(action_only = false) ⇒ Object
- #payload_action ⇒ Object
- #platform ⇒ Object
- #raw_message ⇒ Object
- #raw_payload ⇒ Object
- #referral(field = nil) ⇒ Object
- #sender_first_name ⇒ Object
- #sender_id ⇒ Object
- #sender_language ⇒ Object
- #sender_last_name ⇒ Object
- #text ⇒ Object
- #type ⇒ Object
- #via_bot ⇒ Object
Methods inherited from Message
#deep_link_param, #empty?, #log_message_info, #nlp, #numbers_in_text, #postback_params, #postback_payload, #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/telegram/message.rb', line 7 def initialize(data, type=nil) @data = data @type = type end |
Instance Method Details
#attachments ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/core/lib/telegram/message.rb', line 52 def a = nil a = @data[:audio] a = @data[:document] if a.nil? a = @data[:photo] if a.nil? a = @data[:sticker] if a.nil? a = @data[:video] if a.nil? a = @data[:voice] if a.nil? return a end |
#callback_query_id ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/core/lib/telegram/message.rb', line 79 def callback_query_id if @type == :callback_query return @data[:id] else return nil end end |
#chat ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/core/lib/telegram/message.rb', line 20 def chat if @type == :callback_query @data[:message][:chat] else @data[:chat] end end |
#chat_id ⇒ Object
28 29 30 |
# File 'lib/core/lib/telegram/message.rb', line 28 def chat_id self.chat[:id] end |
#chat_title ⇒ Object
32 33 34 |
# File 'lib/core/lib/telegram/message.rb', line 32 def chat_title self.chat[:title] end |
#chat_type ⇒ Object
36 37 38 |
# File 'lib/core/lib/telegram/message.rb', line 36 def chat_type self.chat[:type].to_sym end |
#command(command_only = true) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/core/lib/telegram/message.rb', line 129 def command(command_only=true) bot_command_entity = @data[:entities].find{|e| e[:type] == "bot_command"} rescue nil if !bot_command_entity.nil? if command_only return @data[:text].split(" ").first.sub("/","").split("@")[0] else return @data[:text] end else return nil end end |
#command_text ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/core/lib/telegram/message.rb', line 155 def command_text command = self.command(false) unless command.nil? a = command.split(" ") a.shift return a.join(" ") else return "" end end |
#deep_link_data ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/core/lib/telegram/message.rb', line 142 def deep_link_data if self.command == "start" command_param = self.command_text.to_s if !command_param.empty? return command_param end end return nil end |
#deep_link_params ⇒ Object
152 153 |
# File 'lib/core/lib/telegram/message.rb', line 152 def deep_link_params end |
#empty_thread_from_ad? ⇒ Boolean
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/core/lib/telegram/message.rb', line 166 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, notification, chat) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/core/lib/telegram/message.rb', line 219 def get_context(user,notification,chat) if !(context_from_postback = Context.get_from_payload(self.payload)).nil? context_route = context_from_postback elsif !(context_in_link = Context.get(self.deep_link_data)).nil? context_route = context_in_link[:context] @data[:deep_link_param] = context_in_link[:param] logger.write "-----context_from_deep_link:#{context_route}-----", :pink elsif !(context_from_typed_postback = Context.get_from_typed_postback(self,user)).nil? context_route = context_from_typed_postback else context_route = user.context end context_class = Context.router(context_route)[:class] if context_class.nil? user.exit_context context_class = Context.router(Kogno::Application.config.routes.default)[:class] end context = context_class.new(user,self,notification,context_route, nil, chat) context end |
#get_kgn_id ⇒ Object
189 190 191 |
# File 'lib/core/lib/telegram/message.rb', line 189 def get_kgn_id self.params[:kgn_id] rescue nil end |
#get_long_payload(data) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/core/lib/telegram/message.rb', line 94 def get_long_payload(data) return @long_payload unless @long_payload.nil? long_data = data.split("kogno__",2) if long_data.length == 2 token = long_data[1] @long_payload = LongPayload.get(token) else @long_payload = data end return @long_payload end |
#handle_event(debug = false) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/core/lib/telegram/message.rb', line 243 def handle_event(debug=false) begin unless self.via_bot.nil? logger.debug "Shared message via inline_query detected. We don't do nothing here yet.", :pink return false end user = User.find_or_create_by_psid(self.sender_id, :telegram) user.get_session_vars user.first_name = self.sender_first_name user.last_name = self.sender_last_name user.locale = self.sender_language if user.locale.nil? user.save self.set_nlp(user.locale) I18n.locale = user.locale unless user.locale.nil? if [:group,:channel, :supergroup].include?(self.chat_type) chat = TelegramChatGroup.find_by_chat_id(self.chat_id) else chat = user end 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 if [:group,:channel, :supergroup].include?(self.chat_type) notification = Notification.new(chat,self) else notification = Notification.new(user,self) end self.(user) context = get_context(user,notification,chat) return({msg: self, user: user, notification: notification, context: context, chat: chat}) if debug unless empty_thread_from_ad? called_action = context.run if Kogno::Application.config.store_log_in_database = user.(self).id else = 0 end notification.answer_callback_query if self.type == :callback_query 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.set_last_usage user.save_session_vars context. else context.run_class_callbacks_only user.save_session_vars user.(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)}") # This helps to identify the error that arrives to Slack in order to search it in logs/http.log logger.write e., :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 |
#inviter_user_id ⇒ Object
44 45 46 |
# File 'lib/core/lib/telegram/message.rb', line 44 def inviter_user_id @data[:from][:id] end |
#location ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/core/lib/telegram/message.rb', line 211 def location if (self.[:type] == :location rescue false) return self.[:value] else return nil end end |
#overwrite_postback(payload) ⇒ Object
207 208 209 |
# File 'lib/core/lib/telegram/message.rb', line 207 def overwrite_postback(payload) @overwritten_payload = payload end |
#page_id ⇒ Object
48 49 50 |
# File 'lib/core/lib/telegram/message.rb', line 48 def page_id nil end |
#params ⇒ Object
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/core/lib/telegram/message.rb', line 178 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
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/core/lib/telegram/message.rb', line 108 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_action ⇒ Object
125 126 127 |
# File 'lib/core/lib/telegram/message.rb', line 125 def payload_action payload(true) end |
#platform ⇒ Object
16 17 18 |
# File 'lib/core/lib/telegram/message.rb', line 16 def platform :telegram end |
#raw_message ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/core/lib/telegram/message.rb', line 193 def t = @data[:text] rescue nil unless t.nil? return({ :type => :text, :value => t }) end end |
#raw_payload ⇒ Object
87 88 89 90 91 92 |
# File 'lib/core/lib/telegram/message.rb', line 87 def raw_payload payload = @data[:data] rescue nil payload = get_long_payload(payload) unless payload.nil? payload = @overwritten_payload if payload.nil? return(payload) end |
#referral(field = nil) ⇒ Object
121 122 123 |
# File 'lib/core/lib/telegram/message.rb', line 121 def referral(field=nil) nil end |
#sender_first_name ⇒ Object
67 68 69 |
# File 'lib/core/lib/telegram/message.rb', line 67 def sender_first_name return @data[:from][:first_name] rescue "" end |
#sender_id ⇒ Object
63 64 65 |
# File 'lib/core/lib/telegram/message.rb', line 63 def sender_id return @data[:from][:id] end |
#sender_language ⇒ Object
75 76 77 |
# File 'lib/core/lib/telegram/message.rb', line 75 def sender_language return @data[:from][:language_code] rescue "" end |
#sender_last_name ⇒ Object
71 72 73 |
# File 'lib/core/lib/telegram/message.rb', line 71 def sender_last_name return @data[:from][:last_name] rescue "" end |
#text ⇒ Object
203 204 205 |
# File 'lib/core/lib/telegram/message.rb', line 203 def text return self.[:value].to_s rescue "" end |
#type ⇒ Object
12 13 14 |
# File 'lib/core/lib/telegram/message.rb', line 12 def type @type end |
#via_bot ⇒ Object
40 41 42 |
# File 'lib/core/lib/telegram/message.rb', line 40 def via_bot @data[:via_bot] rescue nil end |