Skip to content

Commit edd7fdb

Browse files
authored
📝Write doc for friendship module (#352)
1 parent a853bc6 commit edd7fdb

File tree

2 files changed

+89
-128
lines changed

2 files changed

+89
-128
lines changed

docs/references/friendship.md

Lines changed: 26 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,23 @@
22
title: Friendship
33
---
44

5-
发送、接收好友请求和好友确认事件。
5+
# class Friendship()
6+
> 发送、接收好友请求和好友确认事件。
7+
> [示例/Friend-Bot](https://github.com/wechaty/python-wechaty-getting-started/blob/master/examples/advanced/friendship-bot.py)
68
7-
## Friendship
89

9-
发送、接收好友请求和好友确认事件。
10+
::: wechaty.user.friendship.Friendship.search
1011

11-
1. 发送请求
12-
2. 接收请求\(in friend event\)
13-
3. 接受请求\(friend event\)
14-
15-
[示例/Friend-Bot](https://github.com/wechaty/python-wechaty-getting-started/blob/master/examples/advanced/friendship-bot.py)
16-
17-
**类型**: 全局类
18-
19-
* [Friendship](friendship.md#Friendship)
20-
* _实例方法_
21-
* [.accept\(\)](friendship.md#Friendship+accept)`None`
22-
* [.hello\(\)](friendship.md#Friendship+hello)`str`
23-
* [.contact\(\)](friendship.md#Friendship+contact)`Contact`
24-
* [.type\(\)](friendship.md#Friendship+type)`FriendshipType`
25-
* _静态方法_
26-
* [~~.send\(\)~~](friendship.md#Friendship.send)
27-
* [.add\(contact, hello\)](friendship.md#Friendship.add)`None`
28-
29-
### friendship.accept\(\)`None`
30-
31-
接受朋友请求
32-
33-
**类型**: [`Friendship`](friendship.md#Friendship)的实例方法
34-
35-
#### 示例
12+
::: wechaty.user.friendship.Friendship.add
13+
### 示例代码
14+
```python
15+
memberList = await room.memberList()
16+
for member in memberList:
17+
await bot.Friendship.add(member, 'Nice to meet you! I am wechaty bot!')
18+
```
3619

20+
::: wechaty.user.friendship.Friendship.contact
21+
### 示例代码
3722
```python
3823
import asyncio
3924
from wechaty import Wechaty, Friendship
@@ -44,97 +29,45 @@ class MyBot(Wechaty):
4429
async on_friendship(self, friendship: Friendship) -> None:
4530
contact = friendship.contact()
4631
await contact.ready()
47-
48-
if friendship.type() == FriendshipType.FRIENDSHIP_TYPE_RECEIVE:
49-
log_msg = 'accepted automatically'
50-
await friendship.accept()
51-
# if want to send msg, you need to delay sometimes
52-
53-
print('waiting to send message ...')
54-
await asyncio.sleep(3)
55-
await contact.say('hello from wechaty ...')
56-
print('after accept ...')
57-
elif friendship.type() == FriendshipType.FRIENDSHIP_TYPE_CONFIRM:
58-
log_msg = 'friend ship confirmed with ' + contact.name
59-
32+
log_msg = f'receive "friendship" message from {contact.name}'
6033
print(log_msg)
6134

35+
6236
asyncio.run(MyBot().start())
6337
```
6438

65-
### friendship.hello\(\)`str`
66-
67-
Get verify message from
68-
69-
**类型**: [`Friendship`](friendship.md#Friendship)的实例方法
70-
71-
**示例**
72-
73-
_\(If request content is \`ding\`, then accept the friendship\)_
74-
39+
::: wechaty.user.friendship.Friendship.accept
40+
### 示例代码
7541
```python
7642
import asyncio
7743
from wechaty import Wechaty, Friendship
7844

79-
8045
class MyBot(Wechaty):
8146

8247
async on_friendship(self, friendship: Friendship) -> None:
8348
contact = friendship.contact()
8449
await contact.ready()
8550

86-
if friendship.type() == FriendshipType.FRIENDSHIP_TYPE_RECEIVE and friendship.hello() == 'ding':
87-
log_msg = 'accepted automatically because verify messsage is "ding"'
51+
if friendship.type() == FriendshipType.FRIENDSHIP_TYPE_RECEIVE:
52+
log_msg = 'accepted automatically'
8853
await friendship.accept()
8954
# if want to send msg, you need to delay sometimes
9055

9156
print('waiting to send message ...')
9257
await asyncio.sleep(3)
9358
await contact.say('hello from wechaty ...')
9459
print('after accept ...')
60+
elif friendship.type() == FriendshipType.FRIENDSHIP_TYPE_CONFIRM:
61+
log_msg = 'friend ship confirmed with ' + contact.name
9562

96-
asyncio.run(MyBot().start())
97-
```
98-
99-
### friendship.contact\(\)`Contact`
100-
101-
获取邀请的联系人对象
102-
103-
**类型**: [`Friendship`](friendship.md#Friendship)的实例方法
104-
105-
#### 示例
106-
107-
```python
108-
import asyncio
109-
from wechaty import Wechaty, Friendship
110-
111-
112-
class MyBot(Wechaty):
113-
114-
async on_friendship(self, friendship: Friendship) -> None:
115-
contact = friendship.contact()
116-
await contact.ready()
117-
log_msg = f'receive "friendship" message from {contact.name}'
11863
print(log_msg)
11964

120-
12165
asyncio.run(MyBot().start())
12266
```
12367

124-
### friendship.type\(\)`FriendshipType`
125-
126-
返回Friendship请求的类型
127-
128-
> 提示: FriendshipType在这里是枚举类型. </br>
129-
>
130-
> * FriendshipType.FriendshipTypeFRIENDSHIP_TYPE_UNSPECIFIED
131-
> * FriendshipType.FRIENDSHIP_TYPE_CONFIRM
132-
> * FriendshipType.FRIENDSHIP_TYPE_RECEIVE
133-
> * FriendshipType.FRIENDSHIP_TYPE_VERIFY
134-
135-
**类型**: [`Friendship`](friendship.md#Friendship)的实例方法
136-
137-
**示例** _\(If request content is \`ding\`, then accept the friendship\)_
68+
::: wechaty.user.friendship.Friendship.hello
69+
### 示例代码
70+
> 自动接受好友请求中包含消息为 `ding` 的好友请求
13871
13972
```python
14073
import asyncio
@@ -160,32 +93,6 @@ class MyBot(Wechaty):
16093
asyncio.run(MyBot().start())
16194
```
16295

163-
### ~~Friendship.send\(\)~~
164-
165-
_**已弃用**_
166-
167-
请使用[Friendship\#add](friendship.md#friendship-add-contact-hello-promise)
168-
169-
**类型**: [`Friendship`](friendship.md#Friendship)的静态方法
170-
171-
### Friendship.add\(contact, hello\)`Promise <void>`
172-
173-
Send a Friend Request to a `contact` with message `hello`.
174-
175-
The best practice is to send friend request once per minute. Remeber not to do this too frequently, or your account may be blocked.
96+
::: wechaty.user.friendship.Friendship.type
17697

177-
**类型**: [`Friendship`](friendship.md#Friendship)的静态方法
178-
179-
| 参数 | 类型 | 描述 |
180-
| :--- | :--- | :--- |
181-
| contact | `Contact` | Send friend request to contact |
182-
| hello | `string` | The friend request content |
183-
184-
#### Example
185-
186-
```python
187-
memberList = await room.memberList()
188-
for member in memberList:
189-
await bot.Friendship.add(member, 'Nice to meet you! I am wechaty bot!')
190-
191-
```
98+
::: wechaty.user.friendship.Friendship.from_json

src/wechaty/user/friendship.py

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def __init__(self, friendship_id: str):
6969
def load(cls, friendship_id: str) -> Friendship:
7070
"""
7171
load friendship without payload, which loads in a lazy way
72-
:param friendship_id:
73-
:return: initialized friendship
72+
Args:
73+
friendship_id: the id of the friendship
74+
Returns:
75+
Friendship: initialized friendship
7476
"""
7577
return cls(friendship_id)
7678

@@ -79,10 +81,18 @@ async def search(cls, weixin: Optional[str] = None,
7981
phone: Optional[str] = None) -> Optional[Contact]:
8082
"""
8183
* Search a Friend by phone or weixin.
82-
*
83-
* The best practice is to search friend request once per minute.
84-
* Remeber not to do this too frequently, or your account
85-
* may be blocked.
84+
* The best practice is to search friend request once per minute.\
85+
Remeber not to do this too frequently, or your account \
86+
will be blocked.
87+
88+
Args:
89+
weixin: the weixin id of the contact
90+
phone: the phone of the contact
91+
Examples:
92+
>>> friendship = await Friendship.search('phone')
93+
>>> friendship.contact()
94+
Returns:
95+
Contact: the contact found
8696
"""
8797
log.info('search() <%s, %s, %s>', cls, weixin, phone)
8898
friend_id = await cls.get_puppet().friendship_search(weixin=weixin,
@@ -97,6 +107,15 @@ async def search(cls, weixin: Optional[str] = None,
97107
async def add(cls, contact: Contact, hello: str) -> None:
98108
"""
99109
add friendship
110+
111+
Args:
112+
contact: the contact to be added
113+
hello: the hello message
114+
Examples:
115+
>>> contact = Contact.load('contact_id')
116+
>>> await Friendship.add(contact, 'hello')
117+
Returns:
118+
None
100119
"""
101120
log.info('add() <%s, %s>', contact.contact_id, hello)
102121
await cls.get_puppet().friendship_add(
@@ -135,14 +154,25 @@ async def ready(self, force_sync: bool = False) -> None:
135154
def contact(self) -> Contact:
136155
"""
137156
get the contact of the friendship
157+
Args:
158+
None
159+
Examples:
160+
>>> contact = friendship.contact()
161+
Returns:
162+
Contact: the contact of the friendship
138163
"""
139-
140164
contact = self.wechaty.Contact.load(self.payload.contact_id)
141165
return contact
142166

143167
async def accept(self) -> None:
144168
"""
145169
accept friendship
170+
Args:
171+
None
172+
Examples:
173+
>>> await friendship.accept()
174+
Returns:
175+
None
146176
"""
147177
log.info('accept friendship, friendship_id: <%s>', self.friendship_id)
148178
if self.type() != FriendshipType.FRIENDSHIP_TYPE_RECEIVE:
@@ -167,8 +197,13 @@ async def accept(self) -> None:
167197

168198
def hello(self) -> str:
169199
"""
170-
TODO ->
171-
Get verify message from
200+
Get verify message from received friendship request
201+
Args:
202+
None
203+
Examples:
204+
>>> hello_msg = friendship.hello()
205+
Returns:
206+
str: the hello message
172207
"""
173208
if self.payload.hello is None:
174209
hello_msg = ''
@@ -182,6 +217,19 @@ def hello(self) -> str:
182217
def type(self) -> FriendshipType:
183218
"""
184219
Return the Friendship Type
220+
Note:
221+
`FriendshipType` is an enumeration type
222+
223+
* FriendshipType.FriendshipTypeFRIENDSHIP_TYPE_UNSPECIFIED
224+
* FriendshipType.FRIENDSHIP_TYPE_CONFIRM
225+
* FriendshipType.FRIENDSHIP_TYPE_RECEIVE
226+
* FriendshipType.FRIENDSHIP_TYPE_VERIFY
227+
Args:
228+
None
229+
Examples:
230+
>>> friendship.type()
231+
Returns:
232+
FriendshipType: the type of the friendship
185233
"""
186234
if self.payload is None:
187235
return FriendshipType.FRIENDSHIP_TYPE_UNSPECIFIED
@@ -207,6 +255,12 @@ async def from_json(
207255
) -> Friendship:
208256
"""
209257
create friendShip by friendshipJson
258+
Args:
259+
json_data: the json data of the friendship
260+
Examples:
261+
>>> friendship = Friendship.from_json(friendshipJson)
262+
Returns:
263+
Friendship: the friendship created
210264
"""
211265
log.info('from_json() <%s>', json_data)
212266
if isinstance(json_data, str):

0 commit comments

Comments
 (0)