module ActiveNotifier module NotifierAdapters class Dingtalk < ActiveAdapter::Implement VALID_TYPES = %i[text markdown].freeze # (see NotifierAdapters::Abstract#notify) # @param title [#to_s] # @param at_mobiles [#to_a] ([]) # @param is_at_all [Boolean] (false) 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 private def get_text_body(content, at_mobiles, is_at_all) { msgtype: "text", text: { content: content }, at: { atMobiles: at_mobiles, isAtAll: is_at_all } } end def get_markdown_body(title, text, at_mobiles, is_at_all) { msgtype: "markdown", markdown: { title: title, text: text }, at: { atMobiles: at_mobiles, isAtAll: is_at_all } } end end end end