企业微信机器人 api 使用

key 来自:
在这里插入图片描述

一、发送消息

function sendText(key = '', text = '', mentioned = []) {
	try {
		axios({
	        method: 'post',
	        url: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='+ key,
	        data: {
	          msgtype: 'text',
	          text: {
	            content: text,
	            mentioned_list: mentioned
	          }
	        }
	      })
	} catch (error) {
      console.log(error)
    }
}

二、发送文件
要先上传到企业微信的服务器里,获得 id 后,再发送 id

上传文件

import FormData from 'form-data'
import fs from 'fs'
import axios from 'axios'

uploadFile = (key = '', filePath = '') => {
  try {
    const readStream = fs.createReadStream(filePath)
    const formData = new FormData()
    formData.append('media', readStream)

    return axios
      .post('https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?type=file&&key=' + key, formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then(({ data }) => {
        return data.media_id || ''
      })
  } catch (error) {
    console.log(error)
  }
}

通过得到的 id,发送文件到企业微信:

sendFile = (key ='', id = '') => {
  try {
    axios({
      method: 'post',
      url: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + key,
      data: {
        msgtype: 'file',
        file: {
          media_id: id // 从 uploadFile 处获得
        }
      }
    })
  } catch (error) {
    console.log(error)
  }
}

和发送文本不同的是,里面的 data 不同

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值