Class: ActiveNotifier::DingtalkAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_notifier/adapters/dingtalk_adapter.rb

Constant Summary collapse

VALID_TYPES =
%i[markdown].freeze

Instance Method Summary collapse

Instance Method Details

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_notifier/adapters/dingtalk_adapter.rb', line 5

def notify(token, type, message, **options)
  webhook = "#{ActiveNotifier.config.adapters_with_base_url.fetch(:dingtalk)}#{token}"
  unless VALID_TYPES.include?(type)
    error_message = "适配器 type 暂时只支持:#{VALID_TYPES.join(', ')}"
    raise ActiveNotifier::AdapterTypeInvalidError, error_message
  end
  raise ActiveNotifier::MessageBlankError, "message can't be blank, please check template file" if message.empty?
  title = options[:title].to_s
  raise ActiveNotifier::AdapterOptionsInvalidError, "Dingtalk adapter require other options: title" if title.empty?

  body = {
    'msgtype' => type,
    type => {
      'title': title,
      'text': message
    }
  }
  headers = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
  Net::HTTP.post(URI(webhook), body.to_json, headers)
end