Class: ActiveNotifier::NotifierAdapters::Dingtalk

Inherits:
ActiveAdapter::Implement
  • Object
show all
Defined in:
lib/active_notifier/notifier_adapters/dingtalk.rb

Overview

Dingtalk implement for ActiveNotifier::Notifier adapter

Constant Summary collapse

VALID_TYPES =
%i[text markdown].freeze

Instance Method Summary collapse

Instance Method Details

#notify(token, type, message, **options) ⇒ Object

This method is abstract.

Implement through setting a real adapter, like :dingtalk

Notify message

Options Hash (**options):

  • :title (#to_s)
  • :at_mobiles (#to_a)
  • :is_at_all (Boolean)

Raises:

See Also:

  • for usage

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_notifier/notifier_adapters/dingtalk.rb', line 11

def notify(token, type, message, **options)
  webhook = "#{ActiveNotifier.config.adapters_with_base_url.fetch(:dingtalk)}#{token}"
  unless VALID_TYPES.include?(type)
    error_message = "The Dingtalk adapter only support types: #{VALID_TYPES.join(', ')}"
    raise ActiveNotifier::AdapterError, error_message
  end
  if message.empty?
    error_message = "Message of the Dingtalk adapter can't be blank, please check template file"
    raise ActiveNotifier::AdapterError, error_message
  end
  body = get_body(type, message, **options)
  headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json"
  }
  Net::HTTP.post(URI(webhook), body.to_json, headers)
end