From 447310e55cc2b5c86532c160deb6d84571e589ce Mon Sep 17 00:00:00 2001 From: Huan LI Date: Sat, 9 Jun 2018 22:22:49 +0800 Subject: [PATCH] feat: support contactSelf.qrcode() & say(voice.slk) (#1300 #1301) --- src/puppet-padchat/bridge.ts | 70 +--------------------------- src/puppet-padchat/padchat-rpc.ts | 8 ++-- src/puppet-padchat/puppet-padchat.ts | 29 +++++++++--- 3 files changed, 28 insertions(+), 79 deletions(-) diff --git a/src/puppet-padchat/bridge.ts b/src/puppet-padchat/bridge.ts index 734fa335..b92e0468 100644 --- a/src/puppet-padchat/bridge.ts +++ b/src/puppet-padchat/bridge.ts @@ -767,72 +767,6 @@ export class Bridge extends PadchatRpc { const result = await this.WXHeartBeat() return result.message } - - // public async WXSetUserRemark(contactId: string, alias: string): Promise { - // await this.padchatRpc.WXSetUserRemark(contactId, alias) - // } - - // public async WXSendMsg(to: string, content: string, at = ''): Promise { - // await this.padchatRpc.WXSendMsg(to, content, at) - // return - // } - - // public async WXSendImage(to: string, data: string): Promise { - // await this.padchatRpc.WXSendImage(to, data) - // } - - // public async WXGetChatRoomMember(id: string): Promise { - // log.verbose('PuppetPadchatBridge', 'WXGetChatRoomMember(%s)', id) - - // let lastResult: PadchatRoomMemberPayload - // const result = await Misc.retry(async (retry, attempt) => { - // log.silly('PuppetPadchatBridge', 'WXGetChatRoomMember(%s) retry() attempt=%d', id, attempt) - - // try { - // lastResult = await this.padchatRpc.WXGetChatRoomMember(id) - // if (lastResult.member.length <= 0) { - // throw new Error('no room member for room ' + id) - // } - // return lastResult - // } catch (e) { - // return retry(e) - // } - // }).catch(e => { - // log.silly('PuppetPadchatBridge', 'WXGetChatRoomMember(%s) retry() fail: %s', id, e) - // return lastResult - // }) - - // return result - // } - - // public async WXDeleteChatRoomMember(roomId: string, contactId: string): Promise { - // const result = await this.padchatRpc.WXDeleteChatRoomMember(roomId, contactId) - // return result - // } - - // public async WXAddChatRoomMember(roomId: string, contactId: string): Promise { - // const result = await this.padchatRpc.WXAddChatRoomMember(roomId, contactId) - // return result - // } - - // public async WXSetChatroomName(roomId: string, topic: string): Promise { - // await this.padchatRpc.WXSetChatroomName(roomId, topic) - // return - // } - - // public async WXQuitChatRoom(roomId: string): Promise { - // await this.padchatRpc.WXQuitChatRoom(roomId) - // } - - // public async WXAddUser(strangerV1: string, strangerV2: string, type: string, verify: string): Promise { - // await this.padchatRpc.WXAddUser(strangerV1, strangerV2, type, verify) - // } - - // public async WXSetChatroomAnnouncement(chatroom: string, content: string): Promise { - // await this.padchatRpc.WXSetChatroomAnnouncement(chatroom, content) - // } - // public async WXGetChatroomAnnouncement(chatroom: string): Promise { - // return await this.padchatRpc.WXGetChatroomAnnouncement(chatroom) - // } - } + +export default Bridge diff --git a/src/puppet-padchat/padchat-rpc.ts b/src/puppet-padchat/padchat-rpc.ts index 247d74b0..c7188bec 100644 --- a/src/puppet-padchat/padchat-rpc.ts +++ b/src/puppet-padchat/padchat-rpc.ts @@ -1284,8 +1284,8 @@ export class PadchatRpc extends EventEmitter { // 获取用户二维码(自己或者已加入的群) // user 用户名 // style 是否使用风格化二维码 - public async WXGetUserQRCode(user: string, style: string): Promise { - const result = await this.rpcCall('WXGetUserQRCode', user, style) + public async WXGetUserQRCode(user: string, style: number): Promise { + const result = await this.rpcCall('WXGetUserQRCode', user, String(style)) log.silly('PadchatRpc', 'WXGetUserQRCode , stranger,result: %s', JSON.stringify(result)) if (!result || result.status !== 0) { throw Error('WXGetUserQRCode , stranger,error! canot get result from websocket server') @@ -1311,8 +1311,8 @@ export class PadchatRpc extends EventEmitter { // voice_data 语音数据 // voice_size 语音大小 (应该不需要) // voice_time 语音时间(毫秒,最大60 * 1000) - public async WXSendVoice(user: string, data: string, time: string): Promise { - const result = await this.rpcCall('WXSendVoice', user, data, time) + public async WXSendVoice(user: string, data: string, time: number): Promise { + const result = await this.rpcCall('WXSendVoice', user, data, String(time)) log.silly('PadchatRpc', 'WXSendVoice , stranger,result: %s', JSON.stringify(result)) if (!result || result.status !== 0) { throw Error('WXSendVoice , stranger,error! canot get result from websocket server') diff --git a/src/puppet-padchat/puppet-padchat.ts b/src/puppet-padchat/puppet-padchat.ts index cb581063..6e69b566 100644 --- a/src/puppet-padchat/puppet-padchat.ts +++ b/src/puppet-padchat/puppet-padchat.ts @@ -17,7 +17,7 @@ * */ -// import path from 'path' +import path from 'path' // import fs from 'fs' // import cuid from 'cuid' @@ -350,8 +350,9 @@ export class PuppetPadchat extends Puppet { throw new Error('can not set avatar for others') } - throw new Error('not supported') - // return await this.bridge.WXqr + const base64 = await this.bridge.WXGetUserQRCode(contactId, 0) + const qrcode = await pfHelper.imageBase64ToQrCode(base64) + return qrcode } public async contactRawPayload(contactId: string): Promise { @@ -443,10 +444,24 @@ export class PuppetPadchat extends Puppet { throw new Error('no id!') } - await this.bridge.WXSendImage( - id, - await file.toBase64(), - ) + const type = file.mimeType || path.extname(file.name) + switch (type) { + case '.slk': + // 发送语音消息(微信silk格式语音) + await this.bridge.WXSendVoice( + id, + await file.toBase64(), + 60, + ) + break + + default: + await this.bridge.WXSendImage( + id, + await file.toBase64(), + ) + break + } } public async messageForward( -- GitLab