Closed SeongBrave closed 1 year ago
可以的哈,可以认为这个库是一个工具库,可以用在任意的框架下
@mazhe-nerd 你好可以给一个简单的使用方式吗?
好像支持这几种,nest.js下如何引用呢,在module 中吗还是service中呢?
目前只提供了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
@SeongBrave 嗨喽,请问你解决了么,方便提供个 demo 嘛
下面是我的尝试,但是出现了错误
import * as lark from '@larksuiteoapi/node-sdk';
import http from 'http';
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); };
- 错误
error pages/api/webhook.ts (33:24) @ getData
error Error [TypeError]: server.getData is not a function at WEBPACK_DEFAULT_EXPORT (webpack-internal:///(api)/./pages/api/webhook.ts:39:25) at /workstation/lark/node_modules/next/dist/server/api-utils/node.js:463:16 at /workstation/lark/node_modules/next/dist/server/lib/trace/tracer.js:113:36 at NoopContextManager.with (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7057) at ContextAPI.with (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:516) at NoopTracer.startActiveSpan (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18086) at ProxyTracer.startActiveSpan (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18847) at /workstation/lark/node_modules/next/dist/server/lib/trace/tracer.js:102:107 at NoopContextManager.with (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7057) at ContextAPI.with (/workstation/lark/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:516) at NextTracerImpl.trace (/workstation/lark/node_modules/next/dist/server/lib/trace/tracer.js:102:32) at apiResolver (/workstation/lark/node_modules/next/dist/server/api-utils/node.js:461:63) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async DevServer.runApi (/workstation/lark/node_modules/next/dist/server/next-server.js:658:9) at async Object.fn (/workstation/lark/node_modules/next/dist/server/next-server.js:1124:35) at async Router.execute (/workstation/lark/node_modules/next/dist/server/router.js:315:32) at async DevServer.runImpl (/workstation/lark/node_modules/next/dist/server/base-server.js:601:29) at async DevServer.run (/workstation/lark/node_modules/next/dist/server/dev/next-dev-server.js:922:20) at async DevServer.handleRequestImpl (/workstation/lark/node_modules/next/dist/server/base-server.js:533:20) { digest: undefined } 31 | export default (req: NextApiRequest, res: NextApiResponse) => { 32 | const server = http.createServer();
33 | const data = server.getData(); | ^ 34 | const result = eventDispatcher.invoke(data); 35 | server.sendResult(result); 36 | };
package.json
中依赖信息
"dependencies": {
"@larksuiteoapi/node-sdk": "^1.16.0",
"express": "^4.18.2",
"next": "^13.3.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.16.3",
"@types/react": "18.2.3",
"typescript": "5.0.4"
}
CC: @mazhe-nerd
用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
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('');
};
后端服务是用的nestjs,支持吗接入吗?接入方式能给个简单的demo或者思路