-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.py
59 lines (46 loc) · 1.28 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# python3 virtual-environment
# sdk: https://github.com/justdoit0823/pywxclient
import sys
from pywxclient.core import Session, SyncClient, TextMessage
from pywxclient.core.exception import (
WaitScanQRCode, RequestError, APIResponseError, SessionExpiredError,
AuthorizeTimeout, UnsupportedMessage)
def sendTextTo(client, remarkName, textContext):
me=client.user['UserName']
contacts=client.get_contact();
to_user=''
for c in contacts:
if(c['RemarkName']==remarkName):
to_user=c['UserName']
break
if not to_user:
print('cannot find user for ', remarkName)
return
print('ready to send message to ', remarkName)
textMsg=TextMessage(me, to_user, textContext)
client.send_message(textMsg)
print('finish send text message')
def login():
session = Session();
client = SyncClient(session);
authorize_url = client.get_authorize_url()
print('Go to authorize: ', authorize_url)
while True:
try:
authorize_result = client.authorize()
except WaitScanQRCode:
continue
except AuthorizeTimeout:
print('Authorize timeout')
sys.exit(0)
if authorize_result:
break
print('start login')
client.login()
print('login success')
return client
def main():
client = login()
sendTextTo(client, 'xx', '测试文本')
if __name__ == '__main__':
main()