Class: Fastlane::Actions::WorkWechatAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



135
136
137
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 135

def self.authors
  ["DevZhang"]
end

.available_optionsObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 148

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :webhook_URL,
                         description: "机器人的webhook地址",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :text_content,
                         description: "文本内容,最长不超过2048个字节,必须是utf8编码",
                            optional: true,
                                type: String,
                 conflicting_options:[:markdown_content,:image_base64,:image_md5,:news_title,:news_description,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :text_mentioned_list,
                         description: "userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list",
                            optional: true,
                                type: Array,
                 conflicting_options:[:markdown_content,:image_base64,:image_md5,:news_title,:news_description,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :text_mentioned_mobile_list,
                         description: "手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人",
                            optional: true,
                                type: Array,
                 conflicting_options:[:markdown_content,:image_base64,:image_md5,:news_title,:news_description,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :markdown_content,
                         description: "markdown内容,最长不超过4096个字节,必须是utf8编码",
                            optional: true,
                                type: String,
                 conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:image_base64,:image_md5,:news_title,:news_description,:news_url,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :image_base64,
                         description: "图片内容的base64编码",
                            optional: true,
                                type: String,
                 conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:news_title,:news_description,:news_url,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :image_md5,
                         description: "图片内容(base64编码前)的md5值",
                            optional: true,
                                type: String,
                 conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:news_title,:news_description,:news_url,:news_url,:news_picurl]),
    FastlaneCore::ConfigItem.new(key: :news_title,
                         description: "标题,不超过128个字节,超过会自动截断",
                            optional: true,
                                type: String,
                 conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:image_base64,:image_md5]),
    FastlaneCore::ConfigItem.new(key: :news_description,
                         description: "描述,不超过512个字节,超过会自动截断",
                            optional: true,
                                type: String,
                 conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:image_base64,:image_md5]),
    FastlaneCore::ConfigItem.new(key: :news_url,
                         description: "点击后跳转的链接。",
                            optional: true,
                                type: String,conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:image_base64,:image_md5]),
    FastlaneCore::ConfigItem.new(key: :news_picurl,
                         description: "图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。",
                            optional: true,
                                type: String,conflicting_options:[:text_content,:text_mentioned_list,:text_mentioned_mobile_list,:markdown_content,:image_base64,:image_md5])
  ]
end

.descriptionObject



131
132
133
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 131

def self.description
  "work wecaht webhook"
end

.detailsObject



143
144
145
146
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 143

def self.details
  # Optional:
  "send message via work wechat server api"
end

.image_http_body(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 79

def self.image_http_body(params)
  image_base64 = params[:image_base64]
  image_md5 = params[:image_md5]

  body = {}
  body['msgtype'] = "image"
  
#   {
#     "msgtype": "image",
#     "image": {
#         "base64": "DATA",
#         "md5": "MD5"
#     }
# }

  image = { 'base64' => image_base64,'md5' => image_md5 }
  body['image'] = image
  body.to_json
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 205

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.markdown_http_body(params) ⇒ Object



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

def self.markdown_http_body(params)
  markdown_content = params[:markdown_content]
  body = {}
  body['msgtype'] = "markdown"
  
#   {
#     "msgtype": "markdown",
#     "markdown": {
#         "content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
#          >类型:<font color=\"comment\">用户反馈</font>
#          >普通用户反馈:<font color=\"comment\">117例</font>
#          >VIP用户反馈:<font color=\"comment\">15例</font>"
#     }
# }

  markdown = { 'content' => markdown_content }
  body['markdown'] = markdown
  body.to_json
end

.news_http_body(params) ⇒ Object



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
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 99

def self.news_http_body(params)
  news_title = params[:news_title]
  news_description = params[:news_description]
  news_url = params[:news_url]
  news_picurl = params[:news_picurl]



  body = {}
  body['msgtype'] = "news"
  
#   {
#     "msgtype": "news",
#     "news": {
#        "articles" : [
#            {
#                "title" : "中秋节礼品领取",
#                "description" : "今年中秋节公司有豪礼相送",
#                "url" : "www.qq.com",
#                "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
#            }
#         ]
#     }
# }
  article = { 'title'=>news_title, 'url'=>news_url}
  article['description'] = news_description if news_description
  article['picurl'] = news_picurl if news_picurl
  articles = [article]
  body['news'] = {'articles'=>articles}
  body.to_json
end

.return_valueObject



139
140
141
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 139

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 11

def self.run(params)
  UI.message("The work_wechat plugin is working!")
  webhook = params[:webhook_URL]
  puts "webhook = #{webhook}"
  url = URI(webhook)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if url.scheme == 'https'

  headers = { 'Content-Type' => 'application/json' }
  request = Net::HTTP::Post.new(url, headers)

  if params[:markdown_content]
    request.body = markdown_http_body(params)
  elsif params[:image_base64]
    request.body = image_http_body(params)
  elsif params[:news_title]
    request.body = news_http_body(params)
  elsif params[:text_content]
    request.body = text_http_body(params)
  end
  puts http.request(request).body
end

.text_http_body(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
57
# File 'lib/fastlane/plugin/work_wechat/actions/work_wechat_action.rb', line 34

def self.text_http_body(params)
  content = params[:text_content]
  mentioned_list = params[:mentioned_list]
  mentioned_mobile_list = params[:mentioned_mobile_list]

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

  text = { 'content' => content }
  text['mentioned_list'] = mentioned_list if mentioned_list
  text['mentioned_mobile_list'] = mentioned_mobile_list if mentioned_mobile_list
  body['text'] = text
  body.to_json
end