node-sdk icon indicating copy to clipboard operation
node-sdk copied to clipboard

feat: add custom adapter

Open UNICKCHENG opened this issue 1 year ago • 4 comments

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

UNICKCHENG avatar May 08 '23 03:05 UNICKCHENG

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

mazhe-nerd avatar May 15 '23 08:05 mazhe-nerd

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

@mazhe-nerd 试一试?

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

UNICKCHENG avatar May 15 '23 10:05 UNICKCHENG

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

UNICKCHENG avatar May 29 '23 00:05 UNICKCHENG

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

mazhe-nerd avatar May 30 '23 04:05 mazhe-nerd