larksuite / node-sdk

larksuite open sdk for nodejs
MIT License
131 stars 26 forks source link

feat: add custom adapter #48

Open UNICKCHENG opened 1 year ago

UNICKCHENG commented 1 year ago

add a common custom adapter and a NextJS adapter.

// pages/api/webhook.ts
import * as lark from '@larksuiteoapi/node-sdk';

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

const eventDispatcher = new lark.EventDispatcher({
    verificationToken: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
    encryptKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
}).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 async (req: NextApiRequest, res: NextApiResponse) => {
    await lark.adaptNextjs(eventDispatcher, {
        autoChallenge: true,
    })(req, res);   
};

// 方式 2: 使用 adaptCustom
// export default async (req: NextApiRequest, res: NextApiResponse) => {
//    const result = await lark.adaptCustom(eventDispatcher, {
//        autoChallenge: true,
//    })(req.headers, req.body);
//    res.end(result);
//};

Related issues: https://github.com/larksuite/node-sdk/issues/46, https://github.com/larksuite/node-sdk/issues/32

mazhe-nerd commented 1 year ago

感谢同学提供pr。有没有可运行的demo嘞?我想本地跑跑看。

UNICKCHENG commented 1 year ago

感谢同学提供pr。有没有可运行的demo嘞?我想本地跑跑看。

@mazhe-nerd 试一试?

https://github.com/UNICKCHENG/lark-node-sdk-template

UNICKCHENG commented 1 year ago

@mazhe-nerd 嗨,这个 PR 是还有啥问题么?

mazhe-nerd commented 1 year ago

我拉下来刷了下next的文档,还在review哈,最近在处理其它高优的事情。