From b46445b72619592f1c7a13928c475202b970b897 Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 16 Jan 2020 00:28:22 +0800 Subject: [PATCH] Friend search (#1895) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 0.29.3 * modify: recorrect the mini-program payload type according to puppet mini-program. * temp * feat: support search new friend * modify: change parameter for search() * modify: optimize code, bump version * modify: bump wechaty and wechaty-puppet version * better search function * 0.29.23 * 0.29.24 * 0.29.25 * 0.29.26 * 0.29.27 * 0.29.28 * 0.29.29 * 0.29.30 * 0.29.31 Co-authored-by: Huan (李卓桓) --- package.json | 4 ++-- src/user/friendship.ts | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index fd08d855..3622e5d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechaty", - "version": "0.29.35", + "version": "0.29.40", "description": "Wechaty is a Bot SDK for Wechat Personal Account", "main": "dist/src/index.js", "typings": "dist/src/index.d.ts", @@ -100,7 +100,7 @@ "read-pkg-up": "^7.0.0", "state-switch": "^0.6.2", "watchdog": "^0.8.1", - "wechaty-puppet": "^0.17.5", + "wechaty-puppet": "^0.17.10", "ws": "^7.0.0" }, "devDependencies": { diff --git a/src/user/friendship.ts b/src/user/friendship.ts index b16d2f9d..83b52e07 100644 --- a/src/user/friendship.ts +++ b/src/user/friendship.ts @@ -35,7 +35,8 @@ import { import { FriendshipPayload, FriendshipType, -} from 'wechaty-puppet' + FriendshipSearchQueryFilter, +} from 'wechaty-puppet' import { Acceptable, @@ -70,6 +71,40 @@ export class Friendship extends Accessory implements Acceptable { return newFriendship } + /** + * Search a Friend by phone or weixin. + * + * The best practice is to search friend request once per minute. + * Remeber not to do this too frequently, or your account may be blocked. + * + * @param {FriendshipSearchCondition} condition - Search friend by phone or weixin. + * @returns {Promise} + * + * @example + * const friend_phone = await bot.Friendship.search({phone: '13112341234'}) + * const friend_weixin = await bot.Friendship.search({weixin: 'weixin_account'}) + * + * console.log(`This is the new friend info searched by phone : ${friend_phone}`) + * await bot.Friendship.add(friend_phone, 'hello') + * + */ + public static async search ( + queryFiter : FriendshipSearchQueryFilter, + ): Promise { + log.verbose('Friendship', 'static search("%s")', + JSON.stringify(queryFiter), + ) + const contactId = await this.puppet.friendshipSearch(queryFiter) + + if (!contactId) { + return null + } + + const contact = this.wechaty.Contact.load(contactId) + await contact.ready() + return contact + } + /** * @description * use {@link Friendship#add} instead -- GitLab