larksuite / node-sdk

larksuite open sdk for nodejs
MIT License
136 stars 27 forks source link

后端服务是用的nestjs #32

Closed SeongBrave closed 1 year ago

SeongBrave commented 1 year ago

后端服务是用的nestjs,支持吗接入吗?接入方式能给个简单的demo或者思路

mazhe-nerd commented 1 year ago

可以的哈,可以认为这个库是一个工具库,可以用在任意的框架下

SeongBrave commented 1 year ago

@mazhe-nerd 你好可以给一个简单的使用方式吗?

image

好像支持这几种,nest.js下如何引用呢,在module 中吗还是service中呢?

mazhe-nerd commented 1 year ago

目前只提供了express和koa的适配,如果需要适配其它框架,可以自定义适配器来实现:https://github.com/larksuite/node-sdk/blob/main/README.zh.md#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%80%82%E9%85%8D%E5%99%A8

UNICKCHENG commented 1 year ago

@SeongBrave 嗨喽,请问你解决了么,方便提供个 demo 嘛

下面是我的尝试,但是出现了错误

const client = new lark.Client({ appId: 'appId', appSecret: 'appSecret', appType: lark.AppType.SelfBuild });

const eventDispatcher = new lark.EventDispatcher({ verificationToken: 'verificationToken', encryptKey: 'encryptKey', }).register({ 'im.message.receive_v1': async (data) => { const chatId = data.message.chat_id;

    const res = await client.im.message.create({
        params: {
            receive_id_type: 'chat_id',
        },
        data: {
            receive_id: chatId,
            content: JSON.stringify({text: 'hello world'}),
            msg_type: 'text'
        },
    });
    return res;
}

});

export default (req: NextApiRequest, res: NextApiResponse) => { const server = http.createServer(); const data = server.getData(); const result = eventDispatcher.invoke(data); server.sendResult(result); };


- 错误

CC: @mazhe-nerd

mazhe-nerd commented 1 year ago

用http.createServer创建出的server本身还没有getData方法的。readme里的代码const data = server.getData();只是为了说明需要从server中获取数据,具体怎样获取,需要看场景,server不同,获取数据的方式也不同。

如果你是用http.createServer创建server的话,可以使用server.on('request')来获取,具体可见node的官方文档:https://nodejs.org/dist/latest-v18.x/docs/api/http.html#class-httpserver

UNICKCHENG commented 1 year ago

已解决

代码 pages/api/events.ts

import * as lark from '@larksuiteoapi/node-sdk';
import { NextApiRequest, NextApiResponse } from 'next';

const client = new lark.Client({
    appId: 'xxxxxxxxxxxxxxxxxxxxxxxxx',
    appSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    appType: lark.AppType.SelfBuild
});

const eventDispatcher = new lark.EventDispatcher({
    verificationToken: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    encryptKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
}).register({
    'im.message.receive_v1': async (data) => {
        const chatId = data.message.chat_id;

        const res = await client.im.message.create({
            params: {
                receive_id_type: 'chat_id',
            },
            data: {
                receive_id: chatId,
                content: JSON.stringify({text: 'hello world'}),
                msg_type: 'text'
            },
        });
        return res;
    }
});

export default (req: NextApiRequest, res: NextApiResponse) => {
    const data = Object.assign(
        Object.create({
            headers: req.headers,
        }),
        req.body
    );

    const { isChallenge, challenge } = lark.generateChallenge(data, {
        encryptKey: eventDispatcher.encryptKey,
    });

    if (isChallenge) {
        res.end(JSON.stringify(challenge));
        return;
    }

    const value = eventDispatcher.invoke(data);
    res.end('');
};

参考

关联 issues:https://github.com/larksuite/node-sdk/issues/46