wxhelper/nodejs_client_ts/example/ding-dong-bot.ts
LuChao 89a7fc8196 增加ts客户端sdk
- 增加ts客户端sdk
- 优化python代码
2024-03-21 18:58:09 +08:00

59 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as wxhelper from '../src/index'
import net from 'net'
import {
writeMsgStore,
readMsgStore
} from '../src/utils/messageStore'
// 启动一个TCP服务接收hook消息
const server = net.createServer((socket: any) => {
console.log('New client connected')
let messageStore = readMsgStore()
let data = Buffer.from('')
socket.on('data', (data: any, chunk: any) => {
console.log(`Received data: ${data}`)
const dataJson = JSON.parse(data)
messageStore = writeMsgStore(messageStore, dataJson)
// data = Buffer.concat([data, chunk])
// console.log(`Received data: ${data}`)
})
socket.on('end', () => {
const decodedData = data.toString('utf8')
console.log(`Received data: ${decodedData}`)
})
socket.on('close', () => {
console.log('Client disconnected')
})
})
const wsPort = 19099
const main = async () => {
// server.listen(wsPort, () => {
// console.log(`Server listening on port ${wsPort}`)
// })
const checkLoginRes = await wxhelper.checkLogin()
console.log('登录状态:', checkLoginRes.data)
if (checkLoginRes.data.msg !== 'success') {
console.log('请先登录')
return
} else {
const port = String(wsPort)
const ip = '127.0.0.1'
const url = ''
const timeout = '3000'
const enableHttp = '0'
const hookSyncMsgRes = await wxhelper.hookSyncMsg(port, ip, url, timeout, enableHttp)
console.log('开启hook', hookSyncMsgRes.data)
}
const userInfoRes = await wxhelper.userInfo()
console.log('当前账号信息:', userInfoRes.data)
const sendTextMsgRes = await wxhelper.sendTextMsg('filehelper', new Date().toLocaleString() + ':你好,我是机器人')
console.log('发送文本消息:', sendTextMsgRes.data)
}
void main()