Class: DingtalkHelper

Inherits:
Object show all
Includes:
FIR::Config
Defined in:
lib/fir/util/dingtalk_helper.rb

Overview

require ‘byebug’

Constant Summary

Constants included from FIR::Config

FIR::Config::API_YML_PATH, FIR::Config::APP_FILE_TYPE, FIR::Config::APP_INFO_PATH, FIR::Config::CONFIG_PATH, FIR::Config::XCODE_WRAPPER_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FIR::Config

#bughd_api, #config, #current_token, #fir_api, #reload_config, #write_app_info, #write_config

Constructor Details

#initialize(app_info, options, qrcode_path, download_url) ⇒ DingtalkHelper

Returns a new instance of DingtalkHelper.



10
11
12
13
14
15
16
17
# File 'lib/fir/util/dingtalk_helper.rb', line 10

def initialize(app_info, options, qrcode_path, download_url)
  @app_info = app_info
  @options = options
  @access_token = @options[:dingtalk_access_token]
  @secret = @options[:dingtalk_secret]
  @qrcode_path = qrcode_path
  @download_url = download_url
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def access_token
  @access_token
end

#app_infoObject

Returns the value of attribute app_info.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def app_info
  @app_info
end

#download_urlObject

Returns the value of attribute download_url.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def download_url
  @download_url
end

#image_keyObject

Returns the value of attribute image_key.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def image_key
  @image_key
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def options
  @options
end

#qrcode_pathObject

Returns the value of attribute qrcode_path.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def qrcode_path
  @qrcode_path
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/fir/util/dingtalk_helper.rb', line 7

def title
  @title
end

Instance Method Details

#build_dingtalk_at_info(payload) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fir/util/dingtalk_helper.rb', line 42

def build_dingtalk_at_info(payload)
  answer = {}
  answer[:isAtAll] = options[:dingtalk_at_all]

  unless options[:dingtalk_at_phones].blank?
    answer[:atMobiles] = options[:dingtalk_at_phones].split(',')
    payload[:markdown][:text] += options[:dingtalk_at_phones].split(',').map { |x| "@#{x}" }.join(',')
  end

  payload[:at] = answer
end

#secret_cal(secret) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fir/util/dingtalk_helper.rb', line 54

def secret_cal(secret)
  timestamp = Time.now.to_i * 1000
  secret_enc = secret.encode('utf-8')
  str = "#{timestamp}\n#{secret}"
  string_to_sign_enc = str.encode('utf-8')

  hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_enc, string_to_sign_enc)
  {
    timestamp: timestamp,
    sign: CGI.escape(Base64.encode64(hmac).strip)
  }

end

#send_msgObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fir/util/dingtalk_helper.rb', line 19

def send_msg
  return if options[:dingtalk_access_token].blank?

  api_domain = @app_info[:api_url]
  title = "#{@app_info[:name]}-#{@app_info[:version]}(Build #{@app_info[:build]})"
  payload = {
    "msgtype": 'markdown',
    "markdown": {
      "title": "#{title} uploaded",
      "text": "#### #{title}\n\n>uploaded at #{Time.now}\n\nurl: #{download_url}\n\n#{options[:dingtalk_custom_message]}\n\n ![](#{api_domain}/welcome/qrcode?url=#{download_url})"
    }
  }
  build_dingtalk_at_info(payload)
  url = "https://oapi.dingtalk.com/robot/send?access_token=#{options[:dingtalk_access_token]}"
  if options[:dingtalk_secret]
    info = secret_cal(options[:dingtalk_secret])
    url = "#{url}&timestamp=#{info[:timestamp]}&sign=#{info[:sign]}"
  end

  x = DefaultRest.post(url, payload)
rescue StandardError => e
end