Class: SpreeCmCommissioner::TelegramNotifier

Inherits:
Object
  • Object
show all
Defined in:
app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TelegramNotifier

Returns a new instance of TelegramNotifier.



5
6
7
8
9
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 5

def initialize(options)
  @base_options = options
  @channel_id = options.delete(:channel_id)
  @telegram_client = ::Telegram.bots[:exception_notifier]
end

Instance Attribute Details

#base_optionsObject (readonly)

Returns the value of attribute base_options.



3
4
5
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 3

def base_options
  @base_options
end

#channel_idObject (readonly)

Returns the value of attribute channel_id.



3
4
5
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 3

def channel_id
  @channel_id
end

#telegram_clientObject (readonly)

Returns the value of attribute telegram_client.



3
4
5
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 3

def telegram_client
  @telegram_client
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 3

def token
  @token
end

Instance Method Details

#backtrace_message(exception) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 68

def backtrace_message(exception)
  backtrace = exception.backtrace
  return if backtrace.blank?

  text = []

  text << '```'
  backtrace.first(5).each { |line| text << "* #{line}" }
  text << '```'

  text.join("\n")
end

#body(exception, formatter, data, telegram_exception) ⇒ Object



30
31
32
33
34
35
36
37
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
66
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 30

def body(exception, formatter, data, telegram_exception)
  text = []

  text << "<b>Application #{formatter.app_name}</b>"
  text << "\n"

  text << formatter.title
  text << formatter.subtitle
  text << "\n"

  text << '<b>Exception:</b>'
  text << exception.message
  text << "\n"

  if data.present?
    text << '<b>Set by controller:</b>'
    text << "<code>#{data}</code>"
    text << "\n"
  end

  if formatter.request_message.present? && formatter.request_message != "```\n```"
    text << '<b>Request:</b>'
    text << "<code>#{formatter.request_message}</code>"
    text << "\n"
  end

  backtrace = backtrace_message(exception)
  if backtrace.present?
    text << '<b>Backtrace:</b>'
    text << "<code>#{backtrace}</code>"
    text << "\n"
  end

  text << telegram_exception.to_s if telegram_exception.present?

  text.compact.join("\n")
end

#call(exception, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/exception_notifier/spree_cm_commissioner/telegram_notifier.rb', line 11

def call(exception, opts = {})
  options = base_options.merge(opts)
  formatter = ::ExceptionNotifier::Formatter.new(exception, options)
  data = options[:env]['exception_notifier.exception_data']

  telegram_client.send_message(
    chat_id: channel_id,
    parse_mode: 'HTML',
    text: body(exception, formatter, data, nil)
  )
rescue ::Telegram::Bot::Error => e
  # when telegram bot error, we try to send again without parse (raw text),
  # so we still can get exceptions and know what cause telegram error.
  telegram_client.send_message(
    chat_id: channel_id,
    text: body(exception, formatter, data, e)
  )
end