常見錯誤問題
圖片不展示
- 圖片必須是網絡路徑,本地路徑的圖片,base64好像是不行的
@指定的人不成功
以markdown格式舉例,常用的@指定人的方式,一種是手機號,一種是通過userId來指定的
手機號@指定的人
手機號@指定的人,在atMobiles中填入手機號后,一定要在text字段中拼接手機號信息
{
"msgtype": "markdown",
"markdown": {
"title": `打包發布通知`,
"text": `#### 應用發布成功 @150xxxxxxxx \n > ...`,
},
"at": {
"atMobiles": ['150xxxxxxxx'],
"atDingtalkIds": [],
"isAtAll": false
}
}
userId@指定的人
userId@指定的人,官方文檔好像沒及時更新,使用atUserIds字段一直不會成功通知,換為atDingtalkIds就可以了。同時,userId的信息也需要拼接再text字段中
{
"msgtype": "markdown",
"markdown": {
"title": `打包發布通知`,
"text": `#### 應用發布成功 @userId1,@userId2 \n > ...`,
},
"at": {
"atMobiles": [],
"atDingtalkIds": ['userId1', 'userId2'],
"isAtAll": false
}
}
text字段中文本換行錯誤
對於多行文本換行,應該通過\n\n來換行
const msgs = ['commit記錄1', 'commit記錄2', 'commit記錄3']
const desc = msgs.join('\n\n')
{
"msgtype": "markdown",
"markdown": {
"title": `打包發布通知`,
"text": `#### 應用發布成功 ${desc}`,
},
"at": {
"atMobiles": [],
"atDingtalkIds": [],
"isAtAll": false
}
}
使用方式
我這邊是在node中做的釘釘機器人發送通知
// robot.js
#!/usr/bin/env node
const https = require('https');
module.exports = function(token) {
return {
send: function(message, callback) {
const postData = JSON.stringify(message);
const options = {
hostname: 'api.dingtalk.com',
port: 443,
path: '/robot/send?access_token=' + token,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const request = https.request(options, function(response) {
const data = [];
let count = 0;
response.setEncoding('utf8');
response.on('data', function(chunk) {
data.push(chunk);
count += chunk.length;
});
response.on('end', function() {
let buffer;
const length = data.length;
if (length == 0) {
buffer = new Buffer(0);
} else if (length == 1) {
buffer = data[0];
} else {
buffer = new Buffer(count);
for (let index = 0, position = 0; index < length; index++) {
let chunk = data[index];
chunk.copy(buffer, position);
position += chunk.length;
}
}
const datastring = buffer.toString();
const result = JSON.parse(datastring);
if (result.errcode) {
return callback(new Error(result.errmsg));
}
return callback(null, result);
});
});
request.on('error', callback);
request.write(postData);
request.end();
}
};
};
// index.js
#!/usr/bin/env node
const robot = require('./robot')
const robotConfig = {
token: '',
// 需要@人員手機號
atMobiles: [],
// 被@人的用戶ID
atUserIds: [],
// 是否需要@所有人
isAtAll: false,
// 指定打包機器人
robot: 1,
// 打包編譯開啟進程數
threads: 8,
// 釘釘群通知關鍵字
keyword: ['發布'],
};
const dingdingMessage = () => {
const { token, atMobiles = [], atUserIds = [], isAtAll = false, keyword = [] } = robotConfig || {}
const msgs = ['commit記錄1', 'commit記錄2', 'commit記錄3'].join('\n\n')
if (!token) {
console.log('------釘釘機器人token不存在------')
return false
}
if (!keyword.length) {
console.log('------請填寫釘釘機器人關鍵字------')
return false
}
// 手機號列表
const notify = atMobiles.length ? atMobiles.map(item => `@${item}`) : ''
// userId列表
const userIdList = atUserIds.length ? atUserIds.map(item => `@${item}`) : ''
// 關鍵字列表
const keywordList = keyword.join('')
robot(token).send(
{
"msgtype": "markdown",
"markdown": {
"title": `打包發布通知${keywordList}`,
"text": `#### 應用${projectConfig.projectname}發布${type ? '成功' : '失敗'} ${notify} ${userIdList} \n > ${config.desc} \n\n 最近三條提交: \n\n ${msgs}`,
},
"at": {
"atMobiles": atMobiles,
"atDingtalkIds": atUserIds,
"isAtAll": isAtAll
}
}, (err, data) => {
if (err) {
if (err.toString().indexOf('keywords not in content') !== -1) {
console.log('------機器人關鍵詞不匹配,請修改------')
} else {
console.error(err);
}
return
}
console.log(data);
if (data.errcode === 0) {
process.exit()
}
})
}
