Skip to content

Commit

Permalink
Merge pull request #1313 from lijiarui/06-13
Browse files Browse the repository at this point in the history
fix #1252
  • Loading branch information
huan committed Jun 13, 2018
2 parents 11d1528 + 4585fa5 commit e69f1eb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 42 deletions.
41 changes: 17 additions & 24 deletions src/puppet-padchat/padchat-rpc.ts
Expand Up @@ -60,6 +60,8 @@ import {
StandardType,
WXAddChatRoomMemberType,
WXLogoutType,
WXSearchContactType,
WXSearchContactTypeStatus,
} from './padchat-rpc.type'

import {
Expand Down Expand Up @@ -322,7 +324,7 @@ export class PadchatRpc extends EventEmitter {

private async rpcCall(
apiName : string,
...params : string[]
...params : (string | WXSearchContactTypeStatus)[]
): Promise<any> {
log.silly('PadchatRpc', 'rpcCall(%s, %s)', apiName, JSON.stringify(params).substr(0, 500))
return await this.jsonRpc.request(apiName, params)
Expand Down Expand Up @@ -848,24 +850,7 @@ export class PadchatRpc extends EventEmitter {
return result
}

// friendRequestSend
// type来源值:
// 2 -通过搜索邮箱
// 3 -通过微信号搜索
// 5 -通过朋友验证消息
// 7 -通过朋友验证消息(可回复)
// 12 -通过QQ好友添加
// 14 -通过群来源
// 15 -通过搜索手机号
// 16 -通过朋友验证消息
// 17 -通过名片分享
// 22 -通过摇一摇打招呼方式
// 25 -通过漂流瓶
// 30 -通过二维码方式
public async WXAddUser(strangerV1: string, strangerV2: string, type: string, verify: string): Promise<any> {
// TODO:
// type = '14'
// verify = 'hello'
public async WXAddUser(strangerV1: string, strangerV2: string, type: WXSearchContactTypeStatus, verify: string): Promise<any> {
const result = await this.rpcCall('WXAddUser', strangerV1, strangerV2, type, verify)
log.silly('PadchatRpc', 'WXAddUser result: %s', JSON.stringify(result))
return result
Expand Down Expand Up @@ -906,14 +891,22 @@ export class PadchatRpc extends EventEmitter {
return result
}

// TODO: check any
// For add new friends by id
public async WXSearchContact(): Promise<any> {
const result = await this.rpcCall('WXSearchContact')
// This function is used for add new friends by id
public async WXSearchContact(id: string): Promise<WXSearchContactType> {
const result = await this.rpcCall('WXSearchContact', id)
log.silly('PadchatRpc', 'WXSearchContact result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {

if ((!result) && (result.status !== WXSearchContactTypeStatus.Searchable) && (result.status !== WXSearchContactTypeStatus.UnSearchable)) {
throw Error('WXSearchContact error! canot get result from websocket server')
}

if (result.status === WXSearchContactTypeStatus.Searchable) {
log.info('PadchatRpc', 'WXSearchContact wxid: %s can be searched', id)
}

if (result.status === WXSearchContactTypeStatus.UnSearchable) {
log.info('PadchatRpc', 'WXSearchContact wxid: %s cannot be searched', id)
}
return result
}

Expand Down
57 changes: 57 additions & 0 deletions src/puppet-padchat/padchat-rpc.type.ts
Expand Up @@ -125,3 +125,60 @@ export interface WXHeartBeatType {
status : number, // 0
message : string, // ok
}

export enum WXSearchContactTypeStatus {
Searchable = 0,
UnSearchable = -24,
}

// Difference with Contact Payload
// see more in https://github.com/lijiarui/wechaty-puppet-padchat/issues/54
export interface WXSearchContactType {
big_head : string, // '',
city : string, // '',
country : string, // '',
// tslint:disable-next-line:max-line-length
message : string, // '\n�\u0002<e>\n<ShowType>1</ShowType>\n<Content><![CDATA[找不到相关帐号或内容]]></Content>\n<Url><![CDATA[]]></Url>\n<DispSec>30</DispSec>\n<Title><![CDATA[]]></Title>\n<Action>2</Action>\n<DelayConnSec>0</DelayConnSec>\n<Countdown>0</Countdown>\n<Ok><![CDATA[]]></Ok>\n<Cancel><![CDATA[]]></Cancel>\n</e>\n',
nick_name : string, // '',
provincia : string, // '',
py_initial : string, // '',
quan_pin : string, // '',
sex : number, // 0,
signature : string, // '',
small_head : string, // '',
// -24 represent raw wxid and cannot be searched, 0 represent can be search by wxid
status : number, // -24 | 0,
stranger : string, // '',
user_name : string, // ''
}

/**
* Raw type info:
* see more inhttps://ymiao.oss-cn-shanghai.aliyuncs.com/apifile.txt
* 2 - 通过搜索邮箱
* 3 - 通过微信号搜索
* 5 - 通过朋友验证消息
* 7 - 通过朋友验证消息(可回复)
* 12 - 通过QQ好友添加
* 14 - 通过群来源
* 15 - 通过搜索手机号
* 16 - 通过朋友验证消息
* 17 - 通过名片分享
* 22 - 通过摇一摇打招呼方式
* 25 - 通过漂流瓶
* 30 - 通过二维码方式
*/
export enum WXSearchContactTypeStatus {
EMAIL = 2, // search by email
WXID = 3, // search by wxid
VERIFY_NOREPLY = 5, // search by friend verify without reply(朋友验证消息)
VERIFY_REPLY = 7, // search by friend verify(朋友验证消息,可回复)
QQ = 12, // search by qq friend
ROOM = 14, // search by room
MOBILE = 15, // search by mobile number
VERIFY = 16, // search friend verify
CONTACT = 17, // search by contact card
SHAKE = 22, // search by shake and shack
FLOAT = 25, // search by float bottle
QRCODE = 30, // search by scanning qrcode
}
47 changes: 29 additions & 18 deletions src/puppet-padchat/puppet-padchat.ts
Expand Up @@ -98,6 +98,10 @@ import {
// PadchatPayloadType,
// PadchatRoomRawMember,
} from './padchat-schemas'
import {
WXSearchContactType,
WXSearchContactTypeStatus,
} from './padchat-rpc.type'

export type PuppetFoodType = 'scan' | 'ding'
export type ScanFoodType = 'scan' | 'login' | 'logout'
Expand Down Expand Up @@ -970,31 +974,38 @@ export class PuppetPadchat extends Puppet {
): Promise<void> {
log.verbose('PuppetPadchat', 'friendshipVerify(%s, %s)', contactId, hello)

const rawPayload = await this.contactRawPayload(contactId)
if (!this.bridge) {
throw new Error('no bridge')
}

// XXX
console.log('rawPayload.stranger: ', rawPayload)
const rawSearchPayload: WXSearchContactType = await this.bridge.WXSearchContact(contactId)

// let strangerV1
// let strangerV2
// if (isStrangerV1(rawPayload.stranger)) {
// strangerV1 = rawPayload.stranger
// } else if (isStrangerV2(rawPayload.stranger)) {
// strangerV2 = rawPayload.stranger
// } else {
// throw new Error('stranger neither v1 nor v2!')
// }
/**
* If the contact is not stranger, than ussing WXSearchContact can get user_name
*/
if (rawSearchPayload.user_name !== '' && !pfHelper.isStrangerV1(rawSearchPayload.user_name) && !pfHelper.isStrangerV2(rawSearchPayload.user_name)) {
log.warn('PuppetPadchat', 'friendRequestSend %s has been friend with bot, no need to send friend request!', contactId)
return
}

if (!this.bridge) {
throw new Error('no bridge')
let strangerV1
let strangerV2
if (pfHelper.isStrangerV1(rawSearchPayload.stranger)) {
strangerV1 = rawSearchPayload.stranger
strangerV2 = rawSearchPayload.user_name
} else if (pfHelper.isStrangerV2(rawSearchPayload.stranger)) {
strangerV2 = rawSearchPayload.stranger
strangerV1 = rawSearchPayload.user_name
} else {
throw new Error('stranger neither v1 nor v2!')
}

// Issue #1252 : what's wrong here?
// Issue #1252 : what's wrong here?, Trying to fix now...

await this.bridge.WXAddUser(
rawPayload.stranger || '',
rawPayload.ticket || '',
'14',
strangerV1 || '',
strangerV2 || '',
WXSearchContactTypeStatus.WXID, // default
hello,
)
}
Expand Down

0 comments on commit e69f1eb

Please sign in to comment.