美文网首页
2019-04-26 钉钉微应用服务器端

2019-04-26 钉钉微应用服务器端

作者: 少儿创客 | 来源:发表于2019-04-26 10:55 被阅读0次

万丈高楼平地起,基础不牢的我只好东拼西凑,面向百(gu)度(ge)编程,把自己学习的过程记录下来,防止遗忘。

准备工作

虽然我有阿里云可以直接尝试,奈何vim用的实在不熟悉,内网穿透本地开发。

内网穿透

安装库

创建xjyxls目录,进入目录初始化项目,安装必要的包。

mkdir xjyxls
cd xjyxls
npm init
npm install express axios --save

代码

const express = require('express')
const dd = require('dingtalk-jsapi')
const axios = require('axios')

 const app = express()
 const port = 8080

axios.get('https://oapi.dingtalk.com/gettoken?appkey=YOUR_APP_KEY&appsecret=YOUR_APP_SECRET')
    .then(function(res) {
        console.log(res.data.access_token)
    })
    .catch(function(err){
        console.log(err)
    })

 app.get('/', (req, res) => {
     res.send('Hello World!')
   
 });

 app.listen(port, () => {
     console.log(`Server started on port:${port}`);
 });

运行代码


access_token

axios文档https://www.kancloud.cn/yunye/axios/234845,面向API编程,现学现用。

获取数据

拿到access_token后,获取部门列表

部门列表
相关文档:https://g.alicdn.com/dingding/opendoc/docs/_server/tab2.html?t=1467363848453

感想

  • 加强JavaScript和node基础的学习
  • 这段代码做成snippet

参考资料

发送消息

本来很简单的东东,也要搞好久

const express = require('express')
const dd = require('dingtalk-jsapi')
const axios = require('axios')

 const app = express()
 const port = 8080
// 52579475 信息中心部门id
axios.get('https://oapi.dingtalk.com/gettoken?appkey=ding7cyjkqff16kzh8xu&appsecret=6wSweJy99iblZs1v9zByJa51gY9GA9P5OnlsXKUAm-p4aqRprXXyeDs8NwPyB_8o')
    .then(function(res) {
        console.log(res.data.access_token)
        axios.post('https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=' + res.data.access_token,
        {
            agent_id: '259176923',
            dept_id_list: 52579475,
            msg: {
                'msgtype': 'text',
                'text': {
                    'content': '潍坊(上海)新纪元提醒您,明天必须着正装'
                }
            }
        },
        {
            headers: {
                "Content-Type": "application/json"
            }
        }).then(function(res){
            console.log(res.data)
        }).catch(function(err){
            console.log(err.errcode + err.errmsg + err.task_id)
        })
    })
    .catch(function(err){
        console.log(err)
    })

 app.get('/', (req, res) => {
     res.send('Hello World!')
   
 });

 app.listen(port, () => {
     console.log(`Server started on port:${port}`);
 });
命令行
效果图
手机端

参考

https://open-doc.dingtalk.com/microapp/serverapi2/pgoxpy
服务器端API列表

相关文章

网友评论

      本文标题:2019-04-26 钉钉微应用服务器端

      本文链接:https://www.haomeiwen.com/subject/dvqfnqtx.html