创建飞书机器人用于推送

2023/2/24 bot

# 添加消息推送机器人

  1. 登录飞书帐号
  2. 创建群组
  3. 群设置中设置群机器人 获取webhook地址

# 使用

# 调用

获得的webhook地址使用postman等调用,传入规定参数,即可由机器人转发至飞书内发送;

let baseUrl ='https://open.feishu.cn/open-apis/bot/v2/hook/XXX'
let data ={    msg_type: 'text',
    content: {
      text:'测试消息123',
    },}
   request({
     method: 'post',
     url: baseUrl,
     data,
   }).then((res) => {
     console.log(`发送结果:${res.StatusMessage}`)
     // console.log(res)
   })
1
2
3
4
5
6
7
8
9
10
11
12
13

# 参数

//纯文本
let textType ={    msg_type: 'text',
    content: {
      text:'测试',
    },} 
//富文本
let postType = {
      msg_type: 'post',
      content: {
        post: {
          'zh-CN': {
            title,
            content: [
              [
                {
                  tag: 'text',
                  text: text,
                },
                {
                  tag: 'a',
                  text: '点击查看',
                  href: url,
                },
              ],
            ],
          },
        },
    },
}
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

# 添加可以转发消息的机器人

# 创建企业自建应用

  1. 进入飞书开发者后台
  2. 创建企业自建应用(个人使用创建测试企业即可不用进行发版)
  3. 添加应用能力==>机器人
  4. 飞书切换至测试企业,创建群加入自定义机器人 或者 私聊机器人

#

# 事件订阅(node.js)

  1. 创建Encrypt Key
  2. 请求地址配置
  • 后台部分
const express = require('express')
const router = express.Router()
const crypto = require('crypto')
router.post('/message', (req, res) => {
var cipher = new AESCipher('上面生成的Encrypt Key')
  console.log(cipher.decrypt(req.body.encrypt))
  res.send(cipher.decrypt(req.body.encrypt))
})

// 官方的解密方法
class AESCipher {
constructor(key) {
  const hash = crypto.createHash('sha256')
  hash.update(key)
  this.key = hash.digest()
}
decrypt(encrypt) {
  const encryptBuffer = Buffer.from(encrypt, 'base64')
  const decipher = crypto.createDecipheriv(
    'aes-256-cbc',
    this.key,
    encryptBuffer.slice(0, 16)
  )
  let decrypted = decipher.update(
    encryptBuffer.slice(16).toString('hex'),
    'hex',
    'utf8'
  )
  decrypted += decipher.final('utf8')
  return decrypted
}
}
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
  1. 添加接受消息事件 添加事件接受消息v2.0 并开通权限

之后在给此机器人发消息 或者群内@ 都会调用上面的接口;

# 应用机器人回复消息

# 发送消息

给指定用户或者会话发送消息,支持文本、富文本、可交互的消息卡片、群名片、个人名片、图片、视频、音频、文件、表情包

const config = {
  url: `https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=${receive_id_type}`,
  method: 'post',
  headers: {
    Authorization: `Bearer ${tenant_access_token}`,
    'Content-Type': 'application/json; charset=utf-8',
  },
  data: {
    receive_id,
    content,
    msg_type,
    uuid,
  },
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#

最后更新时间: 2023/7/16 15:32:35