Class: Boppers::Notifier::Telegram

Inherits:
Object
  • Object
show all
Defined in:
lib/boppers/notifier/telegram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token:, channel_id:, subscribe: nil) ⇒ Telegram

Returns a new instance of Telegram.



8
9
10
11
12
# File 'lib/boppers/notifier/telegram.rb', line 8

def initialize(api_token:, channel_id:, subscribe: nil)
  @api_token = api_token
  @channel_id = channel_id
  @subscribe = subscribe
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



6
7
8
# File 'lib/boppers/notifier/telegram.rb', line 6

def api_token
  @api_token
end

#channel_idObject (readonly)

Returns the value of attribute channel_id.



6
7
8
# File 'lib/boppers/notifier/telegram.rb', line 6

def channel_id
  @channel_id
end

#subscribeObject (readonly)

Returns the value of attribute subscribe.



6
7
8
# File 'lib/boppers/notifier/telegram.rb', line 6

def subscribe
  @subscribe
end

Instance Method Details

#call(title, message, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/boppers/notifier/telegram.rb', line 14

def call(title, message, options)
  context = self
  telegram_options = options.fetch(:telegram, {})
  title = telegram_options.delete(:title) { title }
  message = telegram_options.delete(:message) { message }

  telegram_options[:text] = "#{title}\n\n#{message}"
  telegram_options[:chat_id] = channel_id

  HttpClient.post do
    url "https://api.telegram.org/bot#{context.api_token}/sendMessage"
    params telegram_options
    options expect: 200
  end
end