Class: ActiveNotifier::NotifierAdapters::Dingtalk

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

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

Parameters:

  • token (String)

    Channel webhook token

  • type (Symbol)

    Message type

  • message (String)

    Message body

  • options (Hash)

    Adapter message options, some adapters require some another options

  • title (#to_s)
  • at_mobiles (#to_a)

    ([])

  • is_at_all (Boolean)

    (false)

Raises:

  • (AdapterOptionsInvalidError)
  • (AdapterTypeInvalidError)
  • (MessageBlankError)

See Also:

  • for usage


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_notifier/notifier_adapters/dingtalk.rb', line 10

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
  at_mobiles = options[:at_mobiles].to_a
  is_at_all = !!options[:is_at_all]

  body = case type
         when :text
           get_text_body(message, at_mobiles, is_at_all)
         when :markdown
           title = options[:title].to_s
           raise ActiveNotifier::AdapterError, "Dingtalk adapter require other options: title" if title.empty?
           get_markdown_body(title, message, at_mobiles, is_at_all)
         end
  headers = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
  Net::HTTP.post(URI(webhook), body.to_json, headers)
end