Sha256: 8122bc99e07c944ea48d8dfed2a27bea870a0f4f7f6c2a16b6263a5ddda5d13e

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Dingtalk
  module Client
    module GroupRobotClient
      # @see https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-nameytovydamarkdown%E7%B1%BB%E5%9E%8B
      module NotifyMarkdown
        # @param code  [Symbol] set token_code and template_file
        # @param title [String]
        # @option options [String]  template_file (code)  without file suffix
        # @option options [Boolean] is_at_all     (false)
        # @option options [Array]   at_mobiles    ([])
        def notify_markdown(code, title, **options)
          options.with_defaults!(
            template_file: code,
            is_at_all: false,
            at_mobiles: []
          )
          token_code = code.to_sym
          token = fetch_token(token_code)
          title = String(title)
          template_file = String(options[:template_file])
          is_at_all = is_at_all != false
          at_mobiles = Array(at_mobiles)
          content = get_markdown_content(template_file)
          body = get_markdown_body(title, content, is_at_all, at_mobiles)
          notify(token, body)
        end

        private

        # @param template_file [String]
        def get_markdown_content(template_file)
          path = "#{Dingtalk::Client.config.template_dir}/#{template_file}.markdown.erb"
          ERB.new(File.read(path)).result(binding)
        rescue Errno::ENOENT
          raise ArgumentError, "Not found template: #{File.expand_path(path)}"
        end

        # @param title      [String]
        # @param content    [String]
        # @param is_at_all  [Boolean]
        # @param at_mobiles [Array]
        # @return [Hash]
        def get_markdown_body(title, content, is_at_all, at_mobiles)
          {
            msgtype: 'markdown',
            markdown: {
              title: title,
              text: content
            },
            at: {
              atMobiles: at_mobiles,
              isAtAll: is_at_all
            }
          }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dingtalk-client-0.2.0 lib/dingtalk/client/group_robot_client/notify_markdown.rb