Class: Fastlane::Actions::WechatAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wechat/actions/wechat_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



166
167
168
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 166

def self.authors
  ["xiongzenghui"]
end

.available_optionsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 170

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :webhook,
      description: "wechat access token",
      type: String,
      optional: true,
      conflicting_options: [:access_token_url, :secret]
    ),
    FastlaneCore::ConfigItem.new(
      key: :access_token_url,
      description: "request wechat access token url",
      type: String,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :agentid,
      description: "agentid param for request wechat access token",
      type: String,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :secret,
      description: "secret param for request wechat access token",
      type: String,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :recievers,
      description: "how many man to receive this message",
      type: Array,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :text,
      description: "wechat message text",
      type: String,
      optional: true,
    ),
    FastlaneCore::ConfigItem.new(
      key: :msgtype,
      description: "wechat message type, eg: markdown、text、image、news",
      type: String,
      optional: false
    ),
    FastlaneCore::ConfigItem.new(
      key: :send_message_url,
      description: "send message to wechat server api url",
      type: String,
      optional: false
    ),
    FastlaneCore::ConfigItem.new(
      key: :articles,
      description: "news articles",
      type: Array,
      optional: true,
      conflicting_options: [:text]
    )
  ]
end

.descriptionObject



158
159
160
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 158

def self.description
  "this is a wechat api wrapper"
end

.detailsObject



162
163
164
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 162

def self.details
  "send message via enterprice wechat server api"
end

.http_body(params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 77

def self.http_body(params = {})
  recievers = params[:recievers]
  msgtype = params[:msgtype]
  text = params[:text]
  articles = params[:articles]

  body = {}
  body['msgtype'] = msgtype

  unless params[:webhook]
    body['touser'] = recievers.join('|')
  end

  # 1、文本类型
  # {
  #   "msgtype": "text",
  #   "text": {
  #       "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
  #       "mentioned_list":["wangqing","@all"],
  #       "mentioned_mobile_list":["13800001111","@all"]
  #   }
  # }
  if msgtype == 'text'
    body['text'] = {
      'content' => text
    }
  end

  # 2、markdown类型
  # {
  #   "msgtype": "markdown",
  #   "markdown": {
  #     "content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
  #       >类型:<font color=\"comment\">用户反馈</font> \n
  #       >普通用户反馈:<font color=\"comment\">117例</font> \n
  #       >VIP用户反馈:<font color=\"comment\">15例</font>"
  #   }
  # }
  if msgtype == 'markdown'
    body['markdown'] = {
      "content" => text
    }
  end

  # 3、图片类型
  # {
  #   "msgtype": "image",
  #   "image": {
  #     "base64": "DATA",
  #     "md5": "MD5"
  #   }
  # }
  if msgtype == 'image'
    body['image'] = {
      'content' => text
    }
  end

  # 4、图文类型
  # {
  #   "msgtype": "news",
  #   "news": {
  #     "articles" : [
  #       {
  #       "title" : "中秋节礼品领取",
  #       "description" : "今年中秋节公司有豪礼相送",
  #       "url" : "URL",
  #       "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
  #       }
  #     ]
  #   }
  # }
  if msgtype == 'news'
    body['news'] = {
      'articles' => articles
    }
  end

  body.to_json
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 231

def self.is_supported?(platform)
  true
end

.request_access_token(url, agentid, secret) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 58

def self.request_access_token(url, agentid, secret)
  token_uri = URI(url)

  http = Net::HTTP.new(token_uri.host, token_uri.port)
  http.use_ssl = true if token_uri.scheme == 'https'

  request = Net::HTTP::Get.new(token_uri)
  request['agentid'] = agentid.to_s
  request['secret'] = secret.to_s

  # resp = Net::HTTP.start(token_uri.hostname, token_uri.port) do |http|
  #   http.request(request)
  # end
  # JSON.parse(resp.body)["access_token"]

  resp = http.request(request)
  JSON.parse(resp.body)["access_token"]
end

.run(params) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 11

def self.run(params)
  if params[:webhook]
    send_with_webhook(params)
  else
    send_with_token(params)
  end
end

.send_with_token(params = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 34

def self.send_with_token(params = {})
  access_token_url = params[:access_token_url]
  agentid = params[:agentid]
  secret = params[:secret]
  send_message_url = params[:send_message_url]
  
  access_token = request_access_token(access_token_url, agentid, secret)
  UI.important "[WechatAction] request access_token: #{access_token}"

  msg_uri = URI(send_message_url)
  http = Net::HTTP.new(msg_uri.host, msg_uri.port)
  http.use_ssl = true if msg_uri.scheme == 'https'

  headers = {
    'token' => access_token,
    'agentid' => agentid,
    'Content-Type' => 'application/json'
  }

  request = Net::HTTP::Post.new(msg_uri, headers)
  request.body = http_body(params)
  puts http.request(request).body
end

.send_with_webhook(params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/wechat/actions/wechat_action.rb', line 19

def self.send_with_webhook(params = {})
  msgtype = params[:msgtype]
  text = params[:text]

  url = URI(params[:webhook])
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if url.scheme == 'https'

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = 'application/json'
  request.body = http_body(params)
  
  puts http.request(request).body
end