Class: Lhj::Dingtalk

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/dingtalk_helper.rb

Overview

pgyer upload

Class Method Summary collapse

Class Method Details

.action_card_body_message(title, message, btns, orientation = :vertical) ⇒ Object

btns: [‘actionURL’=>‘www.baidu.com’]



108
109
110
111
112
113
114
115
116
117
# File 'lib/lhj/helper/dingtalk_helper.rb', line 108

def self.action_card_body_message(title, message, btns, orientation = :vertical)
  btn_orientation = orientation == :vertical ? '1' : '0'
  {
    'msgtype' => 'actionCard',
    'actionCard' => { 'title' => title,
                      'text' => message,
                      'btnOrientation' => btn_orientation,
                      'btns' => btns }
  }.to_json
end

.feed_card_body_message(links) ⇒ Object

links: [is …’, ‘messageURL’=>‘www.dingtalk.com’, ‘picURL’=>‘’]



131
132
133
134
135
136
# File 'lib/lhj/helper/dingtalk_helper.rb', line 131

def self.feed_card_body_message(links)
  {
    'msgtype' => 'feedCard',
    'feedCard' => links
  }.to_json
end

.get_token(app_key, app_secret) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/lhj/helper/dingtalk_helper.rb', line 138

def self.get_token(app_key, app_secret)
  token_url = "https://oapi.dingtalk.com/gettoken?appkey=#{app_key}&appsecret=#{app_secret}"
  url = URI(token_url)
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Get.new(url)
  response = https.request(request)
  res_body = JSON.parse(response.body)
  res_body['access_token'] if res_body['errcode'].to_i.zero?
end

.group_message_body(robot_code, conversation_id, user_ids, msg_key, msg_param) ⇒ Object

sampleText,sampleMarkdown,sampleImageMsg,sampleLink,sampleActionCard,sampleActionCard6,sampleAudio,sampleFile,sampleVideo



186
187
188
189
190
191
192
193
194
# File 'lib/lhj/helper/dingtalk_helper.rb', line 186

def self.group_message_body(robot_code, conversation_id, user_ids, msg_key, msg_param)
  {
    'robotCode' => robot_code,
    'openConversationId' => conversation_id,
    'userIds' => user_ids,
    'msgKey' => msg_key,
    'msgParam' => msg_param
  }.to_json
end

.http_body_message(title, message) ⇒ Object

deprecation warnings used: markdown_body_message method



73
74
75
# File 'lib/lhj/helper/dingtalk_helper.rb', line 73

def self.http_body_message(title, message)
  markdown_body_message(title, message)
end

.http_post(robot_url, http_body) ⇒ Object



68
69
70
# File 'lib/lhj/helper/dingtalk_helper.rb', line 68

def self.http_post(robot_url, http_body)
  Net::HTTP.post(URI(robot_url), http_body, 'Content-Type' => 'application/json')
end

.markdown_body_message(title, message) ⇒ Object



92
93
94
95
96
97
# File 'lib/lhj/helper/dingtalk_helper.rb', line 92

def self.markdown_body_message(title, message)
  {
    'msgtype' => 'markdown',
    'markdown' => { 'title' => title, 'text' => message }
  }.to_json
end

.markdown_body_message_users(title, message, users) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/lhj/helper/dingtalk_helper.rb', line 99

def self.markdown_body_message_users(title, message, users)
  {
    'at' => { 'atUserIds' => users, 'isAtAll' => false },
    'msgtype' => 'markdown',
    'markdown' => { 'title' => title, 'text' => message }
  }.to_json
end

.post_card_message(title, message, btns) ⇒ Object



28
29
30
31
# File 'lib/lhj/helper/dingtalk_helper.rb', line 28

def self.post_card_message(title, message, btns)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_card_message_robot(robot_url, title, message, btns)
end

.post_card_message_robot(robot_url, title, message, btns) ⇒ Object



58
59
60
61
# File 'lib/lhj/helper/dingtalk_helper.rb', line 58

def self.post_card_message_robot(robot_url, title, message, btns)
  http_body = action_card_body_message(title, message, btns, :vertical)
  http_post(robot_url, http_body)
end

.post_message(title, message) ⇒ Object



8
9
10
11
# File 'lib/lhj/helper/dingtalk_helper.rb', line 8

def self.post_message(title, message)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_message_robot(robot_url, title, message)
end

.post_message_at_users(title, message, users) ⇒ Object



13
14
15
16
# File 'lib/lhj/helper/dingtalk_helper.rb', line 13

def self.post_message_at_users(title, message, users)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_message_robot_at_users(robot_url, title, message, users)
end

.post_message_robot(robot_url, title, message) ⇒ Object



38
39
40
41
# File 'lib/lhj/helper/dingtalk_helper.rb', line 38

def self.post_message_robot(robot_url, title, message)
  http_body = markdown_body_message(title, message)
  http_post(robot_url, http_body)
end

.post_message_robot_at_users(robot_url, title, message, users) ⇒ Object



43
44
45
46
# File 'lib/lhj/helper/dingtalk_helper.rb', line 43

def self.post_message_robot_at_users(robot_url, title, message, users)
  http_body = markdown_body_message_users(title, message, users)
  http_post(robot_url, http_body)
end

.post_single_card_message(title, message, single_title, single_url) ⇒ Object



33
34
35
36
# File 'lib/lhj/helper/dingtalk_helper.rb', line 33

def self.post_single_card_message(title, message, single_title, single_url)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_single_card_message_robot(robot_url, title, message, single_title, single_url)
end

.post_single_card_message_robot(robot_url, title, message, single_title, single_url) ⇒ Object



63
64
65
66
# File 'lib/lhj/helper/dingtalk_helper.rb', line 63

def self.post_single_card_message_robot(robot_url, title, message, single_title, single_url)
  http_body = single_action_card_body_message(title, message, single_title, single_url)
  http_post(robot_url, http_body)
end

.post_text_message(message) ⇒ Object



18
19
20
21
# File 'lib/lhj/helper/dingtalk_helper.rb', line 18

def self.post_text_message(message)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_text_message_robot(robot_url, message)
end

.post_text_message_at_users(message, users) ⇒ Object



23
24
25
26
# File 'lib/lhj/helper/dingtalk_helper.rb', line 23

def self.post_text_message_at_users(message, users)
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
  post_text_message_robot_at_users(robot_url, message, users)
end

.post_text_message_robot(robot_url, message) ⇒ Object



48
49
50
51
# File 'lib/lhj/helper/dingtalk_helper.rb', line 48

def self.post_text_message_robot(robot_url, message)
  http_body = text_body_message(message)
  http_post(robot_url, http_body)
end

.post_text_message_robot_at_users(robot_url, message, users) ⇒ Object



53
54
55
56
# File 'lib/lhj/helper/dingtalk_helper.rb', line 53

def self.post_text_message_robot_at_users(robot_url, message, users)
  http_body = text_body_message_users(message, users)
  http_post(robot_url, http_body)
end

.post_token(app_key, app_secret) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/lhj/helper/dingtalk_helper.rb', line 149

def self.post_token(app_key, app_secret)
  url = URI('https://api.dingtalk.com/v1.0/oauth2/accessToken')
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = { 'appKey' => app_key, 'appSecret' => app_secret }.to_json
  response = https.request(request)
  JSON.parse(response.body)['accessToken']
end

.sample_file_param(media_id, file_name, file_type) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/lhj/helper/dingtalk_helper.rb', line 215

def self.sample_file_param(media_id, file_name, file_type)
  {
    'mediaId' => media_id,
    'fileName' => file_name,
    'fileType' => file_type
  }.to_json
end

.sample_image_msg_param(photo_url) ⇒ Object



209
210
211
212
213
# File 'lib/lhj/helper/dingtalk_helper.rb', line 209

def self.sample_image_msg_param(photo_url)
  {
    'photoURL' => photo_url
  }.to_json
end

.sample_markdown_param(title, text) ⇒ Object



202
203
204
205
206
207
# File 'lib/lhj/helper/dingtalk_helper.rb', line 202

def self.sample_markdown_param(title, text)
  {
    'title' => title,
    'text' => text
  }.to_json
end

.sample_text_param(content) ⇒ Object



196
197
198
199
200
# File 'lib/lhj/helper/dingtalk_helper.rb', line 196

def self.sample_text_param(content)
  {
    'content' => content
  }.to_json
end

.sample_video_param(duration, video_media_id, video_type, pic_media_id) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/lhj/helper/dingtalk_helper.rb', line 223

def self.sample_video_param(duration, video_media_id, video_type, pic_media_id)
  {
    'duration' => duration,
    'videoMediaId' => video_media_id,
    'videoType' => video_type,
    'picMediaId' => pic_media_id
  }.to_json
end

.send_group_message(token, body) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/lhj/helper/dingtalk_helper.rb', line 173

def self.send_group_message(token, body)
  url = URI('https://api.dingtalk.com/v1.0/robot/groupMessages/send')
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  request['x-acs-dingtalk-access-token'] = token
  request['Content-Type'] = 'application/json'
  request.body = body
  response = https.request(request)
  JSON.parse(response.body)
end

.send_markdown_message_with_api(conversation_id, title, markdown, user_ids) ⇒ Object



250
251
252
253
254
255
256
257
258
# File 'lib/lhj/helper/dingtalk_helper.rb', line 250

def self.send_markdown_message_with_api(conversation_id, title, markdown, user_ids)
  key = Lhj::IOSRobotConfig.app_key
  secret = Lhj::IOSRobotConfig.app_secret
  token = get_token(key, secret)
  msg_key = 'sampleMarkdown'
  msg_param = sample_markdown_param(title, markdown)
  msg_body = group_message_body(key, conversation_id, user_ids, msg_key, msg_param)
  send_group_message(token, msg_body)
end

.send_text_message_with_api(conversation_id, content, user_ids) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/lhj/helper/dingtalk_helper.rb', line 260

def self.send_text_message_with_api(conversation_id, content, user_ids)
  key = Lhj::IOSRobotConfig.app_key
  secret = Lhj::IOSRobotConfig.app_secret
  token = get_token(key, secret)
  msg_key = 'sampleText'
  msg_param = sample_text_param(content)
  msg_body = group_message_body(key, conversation_id, user_ids, msg_key, msg_param)
  send_group_message(token, msg_body)
end

.single_action_card_body_message(title, message, single_title, single_url) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/lhj/helper/dingtalk_helper.rb', line 119

def self.single_action_card_body_message(title, message, single_title, single_url)
  {
    'msgtype' => 'actionCard',
    'actionCard' => { 'title' => title,
                      'text' => message,
                      'btnOrientation' => '0',
                      'singleTitle' => single_title,
                      'singleURL' => single_url }
  }.to_json
end

.text_body_message(message) ⇒ Object



77
78
79
80
81
82
# File 'lib/lhj/helper/dingtalk_helper.rb', line 77

def self.text_body_message(message)
  {
    'msgtype' => 'text',
    'text' => { 'content' => message }
  }.to_json
end

.text_body_message_users(message, users) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/lhj/helper/dingtalk_helper.rb', line 84

def self.text_body_message_users(message, users)
  {
    'at' => { 'atUserIds' => users, 'isAtAll' => false },
    'msgtype' => 'text',
    'text' => { 'content' => message }
  }.to_json
end

.update_file_with_api(conversation_id, upload_file, user_ids) ⇒ Object



244
245
246
247
248
# File 'lib/lhj/helper/dingtalk_helper.rb', line 244

def self.update_file_with_api(conversation_id, upload_file, user_ids)
  key = Lhj::IOSRobotConfig.app_key
  secret = Lhj::IOSRobotConfig.app_secret
  upload_file_and_notify(key, secret, upload_file, conversation_id, user_ids)
end

.upload(token, file) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/lhj/helper/dingtalk_helper.rb', line 160

def self.upload(token, file)
  upload_url = "https://oapi.dingtalk.com/media/upload?access_token=#{token}"
  url = URI(upload_url)
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  form_data = [['media', file], ['type', 'file']]
  request.set_form(form_data, 'multipart/form-data')
  response = https.request(request)
  res_body = JSON.parse(response.body)
  res_body['media_id'] if res_body['errcode'].to_i.zero?
end

.upload_file_and_notify(key, secret, upload_file, conversation_id, user_ids) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/lhj/helper/dingtalk_helper.rb', line 232

def self.upload_file_and_notify(key, secret, upload_file, conversation_id, user_ids)
  base_name = File.basename(upload_file)
  file_type = File.extname(upload_file)[1..-1]
  token = get_token(key, secret)
  content = File.open(upload_file)
  media_id = upload(token, content)
  msg_key = 'sampleFile'
  msg_param = sample_file_param(media_id, base_name, file_type) if media_id
  msg_body = group_message_body(key, conversation_id, user_ids, msg_key, msg_param) if msg_param
  send_group_message(token, msg_body) if msg_body
end