这里给出 python
和 node
的使用方法。
这里只给出加签的使用方法。
python
pip install dingtalkchatbot
传输 markdown
1 2 3 4 5 6 7 8
| def dingding_pro(data): try: url = "web_hook_url" xiaoding = DingtalkChatbot(url, secret="dingding_secret") xiaoding.send_markdown(title="量化 BOT", text=data) except Exception as e: logger.error("钉钉发送失败") logger.error(traceback.format_exc())
|
很简单,只需要把上面的 web_hook_url 还有 dingding_secret 替换掉就可以了
node
新建一个 bot.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| const request = require('request'); const crypto = require('crypto'); const headers = { "Content-Type": "application/json;charset=utf-8" };
const defaultOptions = { msgtype: "text", text: { content: 'hello~' } }
class Bot { base_url = 'https://oapi.dingtalk.com/robot/send' access_token = '' secret = ''
constructor(access_token,secret) { this.access_token = access_token; this.secret = secret;
}
signFn = (secret, content) => { const str = crypto.createHmac('sha256', secret).update(content) .digest('base64'); return encodeURIComponent(str); }
send(json = defaultOptions) { try { const timestamp = new Date().getTime() const sign = this.signFn(this.secret, `${timestamp}\n${this.secret}`) let _webhookUrl = `${this.base_url}?access_token=${this.access_token}×tamp=${timestamp}&sign=${sign}` let options = { headers, json }; request.post(_webhookUrl, options, function (_error, _response, body) { console.log(`send msg, response: ${JSON.stringify(body)}`); }); } catch (err) { console.error(err); return false; } } }
module.exports = Bot
|
然后在其他地方引用。
1 2 3 4 5 6 7 8 9
| const bot = new Bot('web_hook', 'secret')
bot.send({ msgtype: "text", text: { content: 'hello~' } })
|
这样就好了。
java
java
的 markdown
发送。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.fate.backstage.service.services.common;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.response.OapiRobotSendResponse; import com.fate.backstage.entity.monitor.MonitorEntity; import com.fate.backstage.service.ServiceInter; import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.util.codec.binary.Base64; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException;
@Service @Slf4j(topic = "service.MonitorService") public class MonitorService implements ServiceInter {
@Value("${dingding_secret}") private String dingding_secret;
public String getSign(Long timestamp, String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { String stringToSign = timestamp + "\n" + secret; Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)); return URLEncoder.encode(new String(Base64.encodeBase64(signData)), StandardCharsets.UTF_8); }
public void sendMessage(MonitorEntity monitorEntity) throws ApiException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { Long timestamp = System.currentTimeMillis(); String sign = getSign(timestamp, dingding_secret); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=72fbd038c659fa5253f2470bb15907f56fc611f7170c5f63cd4270a737afa8fd" + "×tamp=" + timestamp + "&sign=" + sign); OapiRobotSendRequest request = new OapiRobotSendRequest(); request.setMsgtype("markdown"); OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); markdown.setTitle(monitorEntity.getTask()); markdown.setText( "- " + monitorEntity.getTask() + " \n" + "- ip: " + monitorEntity.getIp() + " \n" ); request.setMarkdown(markdown); OapiRobotSendResponse response = client.execute(request); } }
|