• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

钉钉机器人消息推送 Nodejs SDK。

Dingtalk Robot

download codecov download

钉钉机器人Node.js SDK,基于官方提供的文档稍微抽象封装了一层,方便调用。

官网链接:https://open-doc.dingtalk.com/microapp/serverapi3/iydd5h

1. 用法

1.0 安装模块

npm i dingtalk-robot-sender --save

1.1 创建机器人实例

const ChatBot = require('dingtalk-robot-sender');
// 直接使用 webhook
const robot = new ChatBot({
  webhook: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx'
});

// 组合 baseUrl 和 accessToken 如果采用加签方式的安全设置,同时填写secret
const robot = new ChatBot({
  baseUrl: 'https://oapi.dingtalk.com/robot/send',
  accessToken: 'xxxxxxxxx',
  secret: 'xxxxxxxx',
});

// 自定义 httpclient
const robot = new ChatBot({
  baseUrl: 'https://oapi.dingtalk.com/robot/send',
  accessToken: 'xxxxxxxxx',
  secret: 'xxxxxxxx',
  httpclient: require('urllib')
});

1.2 发送钉钉消息

// 发送钉钉消息
let textContent = {
  "msgtype": "text", 
  "text": {
    "content": "我就是我, 是不一样的烟火"
  }, 
  "at": {
    "atMobiles": [
      "156xxxx8827", 
      "189xxxx8325"
    ], 
    "isAtAll": false
  }
}
robot.send(textContent)
  .then((res) => {
    // TODO
  });

1.3 快速发送各种格式的消息(强烈建议阅读源码中的注释部分)

1.3.1 发送普通文本

let content = '我就是我, 是不一样的烟火';
let at = {
   "atMobiles": [
    "156xxxx8827", 
    "189xxxx8325"
  ], 
  "isAtAll": false
};
// 快速发送文本消息
robot.text(content, at);

1.3.2 发送链接

let link = {
  "text": "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?", 
  "title": "时代的火车向前开", 
  "picUrl": "", 
  "messageUrl": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
};
robot.link(link);

1.3.3 发送Markdown

let title = '杭州天气';
let text = "#### 杭州天气 @156xxxx8827\n" +
          "> 9度,西北风1级,空气良89,相对温度73%\n\n" +
          "> ![screenshot](http://image.jpg)\n"  +
          "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n";
let at2 = {
   "atMobiles": [
    "156xxxx8827", 
    "189xxxx8325"
  ], 
  "isAtAll": false
};
robot.markdown(title, text, at2);

1.3.4 发送actionCard

let card = {
  "title": "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
  "text": `![screenshot](@lADOpwk3K80C0M0FoA) 
                ### 乔布斯 20 年前想打造的苹果咖啡厅 
                Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划`,
  "hideAvatar": "0",
  "btnOrientation": "0",
  "btns": [
    {
      "title": "内容不错",
      "actionURL": "https://www.dingtalk.com/"
    },
    {
      "title": "不感兴趣",
      "actionURL": "https://www.dingtalk.com/"
    }
  ]
};
robot.actionCard(card);

1.3.5 发送feedCard

let links = [
  {
    "title": "时代的火车向前开",
    "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
    "picURL": "https://www.dingtalk.com/"
  },
  {
    "title": "时代的火车向前开2",
    "messageURL": "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
    "picURL": "https://www.dingtalk.com/"
  }
]
robot.feedCard(links);

Examples

https://github.com/x-cold/dingtalk-robot/tree/master/examples

Environment

  • Node.js > 6

Test

Units Test

npm test

Coverage

npm run ci

More Repositories

1

yuque-hexo

同步语雀的文章到你的 Hexo 项目吧!
JavaScript
712
star
2

N3-admin

vue / vuex / N3 / vue-router / axios v2
Vue
193
star
3

yuque-blog

基于语雀作为后台实现的博客系统
SCSS
143
star
4

awesome-npm

Collections for node.js and fe
128
star
5

face-detection-nodejs

face detection by node.js
JavaScript
14
star
6

task-queue-sample

Node.js 生产者/消费者 模式简单实践 - 多进程爬虫任务队列
JavaScript
13
star
7

face-detection-browser

A demo for face detection in browser.
HTML
12
star
8

TipsAboutBatch

批处理写的一些实用脚本
HTML
11
star
9

angular-weui

将weui整合到angular项目,面向服务进行封装
JavaScript
9
star
10

blog

blog power by yuque and hexo.
JavaScript
7
star
11

event-invoke

The invoker based on event model provides an elegant way to call your methods in another container via promisify functions.
JavaScript
7
star
12

typescript-tutorial

A brief tutorial for TypeScript and React developers.
TypeScript
6
star
13

quill2-image-drop-and-paste

A quill editor module for drop and paste image, with a callback hook before insert image into the editor
TypeScript
6
star
14

love-timer

Timer for me ❤️ wanwan
JavaScript
4
star
15

oldBlogPage

Hexo Blog
HTML
4
star
16

rollup-ts-library-boilerplate

A quick starter for library development and includes TS / Jest / Rollup / standard-version / typedocs etc...
JavaScript
3
star
17

aliyun-function

阿里云函数计算 examples
JavaScript
3
star
18

cookies-parser

Convert cookies string to cookies array.
JavaScript
2
star
19

electron-ts-react-boilerplate

A brief starter for developing electron applications.
JavaScript
2
star
20

ablecloud-sdk

Officious iot ablecloud sdk.
JavaScript
2
star
21

stitching_gui

An image stiching application built by OpenCV3 and QT5
C++
2
star
22

fragments

碎片化知识片段
1
star
23

riddle

A riddle game
JavaScript
1
star
24

egg-ablecloud

egg plugin for ablecloud
JavaScript
1
star
25

service-worker-proxy

1
star
26

function-compute-demos

Aliyun function compute demos.
JavaScript
1
star
27

TipsAboutPowerShell

There are quatantities of powershell scripts written by xcold
PowerShell
1
star
28

gallery

Gallery powered by node.js json.db
JavaScript
1
star
29

page-collector

Yet anoter page-collector to collecting structured web pages.
1
star