Class: DingtalkHelper
- 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
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#app_info ⇒ Object
Returns the value of attribute app_info.
-
#download_url ⇒ Object
Returns the value of attribute download_url.
-
#image_key ⇒ Object
Returns the value of attribute image_key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#qrcode_path ⇒ Object
Returns the value of attribute qrcode_path.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #build_dingtalk_at_info(payload) ⇒ Object
-
#initialize(app_info, options, qrcode_path, download_url) ⇒ DingtalkHelper
constructor
A new instance of DingtalkHelper.
- #secret_cal(secret) ⇒ Object
- #send_msg ⇒ Object
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, , qrcode_path, download_url) @app_info = app_info @options = @access_token = @options[:dingtalk_access_token] @secret = @options[:dingtalk_secret] @qrcode_path = qrcode_path @download_url = download_url end |
Instance Attribute Details
#access_token ⇒ Object
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_info ⇒ Object
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_url ⇒ Object
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_key ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/fir/util/dingtalk_helper.rb', line 7 def @options end |
#qrcode_path ⇒ Object
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 |
#title ⇒ Object
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] = [:dingtalk_at_all] unless [:dingtalk_at_phones].blank? answer[:atMobiles] = [:dingtalk_at_phones].split(',') payload[:markdown][:text] += [: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) = Time.now.to_i * 1000 secret_enc = secret.encode('utf-8') str = "#{}\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: , sign: CGI.escape(Base64.encode64(hmac).strip) } end |
#send_msg ⇒ Object
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 [:dingtalk_access_token].blank? api_domain = @app_info[:api_url] title = "#{@app_info[:name]}-#{@app_info[:version]}(Build #{@app_info[:build]}) #{@app_info[:type]}" payload = { "msgtype": 'markdown', "markdown": { "title": "#{title} uploaded", "text": "#### #{title}\n\n>uploaded at #{Time.now}\n\nurl: #{download_url}\n\n#{[: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=#{[:dingtalk_access_token]}" if [:dingtalk_secret] info = secret_cal([:dingtalk_secret]) url = "#{url}×tamp=#{info[:timestamp]}&sign=#{info[:sign]}" end x = DefaultRest.post(url, payload) rescue StandardError => e end |