123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- const fetch = require('node-fetch')
- const FormData = require('form-data')
- const { createReadStream } = require('fs')
- const ROBOT_ID = 'cli_a13ad2afa438d00b'
- const ROBOT_ST = '4tK9LY9VbiQlY5umhE42dclBFo6t4p5O'
- const ROBOT_URL = 'https://open.feishu.cn/open-apis/bot/v2/hook/4a877590-334e-4212-9198-25e7768c64f9'
- const IMG_UPLOAD_URL = 'https://open.feishu.cn/open-apis/im/v1/images'
- const GET_ACCESS_TOKEN_URL = 'https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal'
- async function getAccessToken() {
- const body = {
- app_id: ROBOT_ID,
- app_secret: ROBOT_ST
- }
- const response = await fetch(GET_ACCESS_TOKEN_URL, {
- method: 'post',
- body: JSON.stringify(body),
- headers: {'Content-Type': 'application/json'}
- })
- return await response.json()
- }
- async function uploadQr(access_token, imgPath) {
- const formData = new FormData()
- const file = createReadStream(imgPath)
- formData.append('image', file)
- formData.append('image_type', 'message')
- const response = await fetch(IMG_UPLOAD_URL, {
- method: 'post',
- headers: {
- authorization: `Bearer ${access_token}`
- },
- body: formData
- })
- return await response.json()
- }
- async function sendQrToFeishu(image_key, desc) {
- const schema = {
- msg_type: 'post',
- content: {
- post: {
- zh_cn: {
- title: '小程序预览二维码',
- content: [
- [
- {
- tag: 'img',
- image_key: image_key
- }
- ],
- [
- {
- tag: 'text',
- text: desc
- }
- ]
- ]
- }
- }
- }
- }
- const response = await fetch(ROBOT_URL, {
- method: 'post',
- headers: {'Content-Type': 'application/json'},
- body: JSON.stringify(schema),
- })
- return await response.json()
- }
- function PostPreViewQr(ctx) {
- ctx.register({
- name: 'onPreviewComplete',
- fn: ({ success, data, error }) => {
- // console.log('接收预览后数据', success, data, error)
- if (success) {
- const { qrCodeLocalPath } = data
- getAccessToken().then((res: any) => {
- const { code, tenant_access_token, msg } = res
-
- if (code !== 0)
- return Promise.reject({ code, msg })
-
- return tenant_access_token
- }).then(res => uploadQr(res, qrCodeLocalPath))
- .then((res: any) => {
- const { code , data, msg } = res
-
- if (code !== 0)
- return Promise.reject({ code , msg })
-
- const { image_key } = data
-
- return sendQrToFeishu(image_key, '票圈乐活')
-
- }).catch(err => {
- console.error(err)
- })
- }
- }
- })
- }
- export default PostPreViewQr
|