38 lines
936 B
TypeScript
38 lines
936 B
TypeScript
import { Worker } from "bullmq";
|
|
import { redis } from './redis.ts';
|
|
|
|
import { Wx } from "../../src/wx";
|
|
|
|
const worker = new Worker('wxmsg', async job => {
|
|
const wx = new Wx({
|
|
appId: process.env.WX_APPID || '',
|
|
appSecret: process.env.WX_APPSECRET || '',
|
|
redis: redis
|
|
});
|
|
if (job.name === 'analyzeUserMsg') {
|
|
const { touser, msg } = job.data;
|
|
const accessToken = await wx.getAccessToken();
|
|
const sendData = {
|
|
touser,
|
|
msgtype: 'text',
|
|
text: {
|
|
content: 'Hello World' + new Date().toISOString(),
|
|
},
|
|
};
|
|
await wx.sendUserMessage(sendData);
|
|
} else {
|
|
throw new Error(`Unknown job name: ${job.name}`);
|
|
}
|
|
}, {
|
|
connection: redis
|
|
});
|
|
|
|
worker.on('completed', (job) => {
|
|
console.log(`Job ${job.id} has completed!`);
|
|
});
|
|
|
|
worker.on('failed', (job, err) => {
|
|
console.log(`Job ${job?.id} has failed with error ${err.message}`);
|
|
});
|
|
|
|
console.log('Worker is running...'); |