class FeishuHelper

require ‘byebug’

Attributes

app_info[RW]
download_url[RW]
feishu_access_token[RW]
image_key[RW]
options[RW]
qrcode_path[RW]
title[RW]

Public Class Methods

new(app_info, options, qrcode_path, download_url) click to toggle source
# File lib/fir/util/feishu_helper.rb, line 7
def initialize(app_info, options, qrcode_path, download_url)
  @app_info = app_info
  @options = options
  @feishu_access_token = @options[:feishu_access_token]
  @qrcode_path = qrcode_path
  @download_url = download_url

  @title = "#{@app_info[:name]}-#{@app_info[:version]}(Build #{@app_info[:build]}) #{@app_info[:type]}"
end

Public Instance Methods

send_msg() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 17
def send_msg
  return if feishu_access_token.blank?

  answer = v2_request
  v1_request if answer.dig('ok') == 'false'
end
v1_request() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 30
def v1_request
  url = "https://open.feishu.cn/open-apis/bot/hook/#{feishu_access_token}"
  payload = {
    "title": "#{title} uploaded",
    "text": "#{title} uploaded at #{Time.now}\nurl: #{download_url}\n#{options[:feishu_custom_message]}\n"
  }
  DefaultRest.post(url, payload, {timeout: ENV['FEISHU_TIMEOUT'] ? ENV['FEISHU_TIMEOUT'].to_i : 30 })
end
v2_request() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 24
def v2_request
  url = "https://open.feishu.cn/open-apis/bot/v2/hook/#{feishu_access_token}"
  x = build_v2_info
  DefaultRest.post(url, x, {timeout: ENV['FEISHU_TIMEOUT'] ? ENV['FEISHU_TIMEOUT'].to_i : 30 })
end

Private Instance Methods

build_image_tag() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 70
def build_image_tag
  {
    "tag": 'img',
    "image_key": upload_qr_code,
    "width": 300,
    "height": 300
  }
end
build_info_tags() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 55
def build_info_tags
  text = "#{title} uploaded at #{Time.now}\n#{options[:feishu_custom_message]}\n"
  [
    {
      "tag": 'text',
      "text": text
    },
    {
      "tag": 'a',
      "text": download_url,
      "href": download_url
    }
  ]
end
build_v2_info() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 41
def build_v2_info
  {
    msg_type: 'post',
    content: {
      post: {
        zh_cn: {
          title: options[:feishu_custom_title] ? options[:feishu_custom_title] : title,
          content: [build_info_tags, [build_image_tag]]
        }
      }
    }
  }
end
fetch_image_access_token() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 79
def fetch_image_access_token
  answer = DefaultRest.post(fir_api[:user_feishu_access_token] || 'http://api.appmeta.cn/user/fetch_feishu_v3_token', {})
  answer[:feishu_v3_token]
end
upload_qr_code() click to toggle source
# File lib/fir/util/feishu_helper.rb, line 84
def upload_qr_code
  answer = RestClient.post('https://open.feishu.cn/open-apis/image/v4/put/', {
                             image_type: 'message',
                             image: File.new(qrcode_path, 'rb')
                           }, {
                             'Authorization' => "Bearer #{fetch_image_access_token}"
                           })

  json = JSON.parse(answer)
  json.dig('data', 'image_key')
end