Sha256: bf968942545d45ee96a2aa9629b00c6ca6a9d7abd55dd52195c298ac50eaecf1

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Dingtalk
  class Robot
    # Text type strategy for sending message
    class TextStrategy
      def initialize(webhook_url, message)
        @webhook_url = webhook_url
        @message = message
      end

      # @option options [Array<String>] :at_mobiles
      # @option options [Boolean]       :is_at_all
      def notify(**options)
        at_mobiles = options[:at_mobiles].to_a
        is_at_all  = options[:is_at_all] ? true : false
        body = generate_body(at_mobiles, is_at_all)
        headers = {
          'Content-Type': 'application/json',
          Accept: 'application/json'
        }
        Net::HTTP.post(URI(webhook_url), body.to_json, headers).body
      end

      private

      attr_reader :webhook_url, :message

      def generate_body(at_mobiles, is_at_all)
        {
          msgtype: :text,
          text: {
            content: message
          },
          at: {
            atMobiles: at_mobiles,
            isAtAll: is_at_all
          }
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dingtalk-robot-0.2.1 lib/dingtalk/robot/strategies/text_strategy.rb
dingtalk-robot-0.2.0 lib/dingtalk/robot/strategies/text_strategy.rb