Skip to content

Commit 23d80d4

Browse files
committed
Merge branch 'master' of https://github.com/caobaibing/wxBot into caobaibing-master
2 parents 776cae2 + b065095 commit 23d80d4

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Web微信协议参考资料:
3939

4040
## 1 环境与依赖
4141

42-
目前只能运行于Python 2环境
42+
目前能运行于Python 3.4环境
4343

4444
**wxBot** 用到了Python **requests** , **pypng** , 以及 **pyqrcode** 库。
4545

bot.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# coding: utf-8
33

44
from wxbot import *
5-
import ConfigParser
5+
import configparser
66
import json
77

88

@@ -14,20 +14,23 @@ def __init__(self):
1414
self.robot_switch = True
1515

1616
try:
17-
cf = ConfigParser.ConfigParser()
17+
cf = configparser.ConfigParser()
1818
cf.read('conf.ini')
1919
self.tuling_key = cf.get('main', 'key')
2020
except Exception:
2121
pass
22-
print 'tuling_key:', self.tuling_key
22+
print ('tuling_key:', self.tuling_key)
2323

2424
def tuling_auto_reply(self, uid, msg):
2525
if self.tuling_key:
2626
url = "http://www.tuling123.com/openapi/api"
2727
user_id = uid.replace('@', '')[:30]
28-
body = {'key': self.tuling_key, 'info': msg.encode('utf8'), 'userid': user_id}
29-
r = requests.post(url, data=body)
30-
respond = json.loads(r.text)
28+
body = {'key': self.tuling_key, 'info': msg.encode('utf-8'), 'userid': user_id}
29+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
30+
r = requests.post(url, data=body,headers=headers)
31+
print(r.text)
32+
c=str(r.text)
33+
respond = json.loads(c)
3134
result = ''
3235
if respond['code'] == 100000:
3336
result = respond['text'].replace('<br>', ' ')

wxbot.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ def get_contact(self):
4949
r = self.session.post(url, data='{}')
5050
r.encoding = 'utf-8'
5151
if self.DEBUG:
52-
with open('contacts.json', 'w') as f:
52+
with open('contacts.json', 'wb') as f:
5353
f.write(r.text.encode('utf-8'))
54+
# if self.DEBUG:
55+
# with open('contacts.json', 'wb') as f:
56+
# f.write(r.text)
5457
dic = json.loads(r.text)
5558
self.member_list = dic['MemberList']
5659

@@ -282,24 +285,24 @@ def extract_msg_content(self, msg_type_id, msg):
282285
msg_content['data'] = pos
283286
msg_content['detail'] = data
284287
if self.DEBUG:
285-
print ' %s[Location] %s ' % (msg_prefix, pos)
288+
print (' %s[Location] %s ' % (msg_prefix, pos))
286289
else:
287290
msg_content['type'] = 0
288291
msg_content['data'] = content.replace(u'\u2005', '')
289292
if self.DEBUG:
290-
print ' %s[Text] %s' % (msg_prefix, msg_content['data'])
293+
print( ' %s[Text] %s' % (msg_prefix, msg_content['data']))
291294
elif mtype == 3:
292295
msg_content['type'] = 3
293296
msg_content['data'] = self.get_msg_img_url(msg_id)
294297
if self.DEBUG:
295298
image = self.get_msg_img(msg_id)
296-
print ' %s[Image] %s' % (msg_prefix, image)
299+
print (' %s[Image] %s' % (msg_prefix, image))
297300
elif mtype == 34:
298301
msg_content['type'] = 4
299302
msg_content['data'] = self.get_voice_url(msg_id)
300303
if self.DEBUG:
301304
voice = self.get_voice(msg_id)
302-
print ' %s[Voice] %s' % (msg_prefix, voice)
305+
print (' %s[Voice] %s' % (msg_prefix, voice))
303306
elif mtype == 42:
304307
msg_content['type'] = 5
305308
info = msg['RecommendInfo']
@@ -309,18 +312,18 @@ def extract_msg_content(self, msg_type_id, msg):
309312
'city': info['City'],
310313
'gender': ['unknown', 'male', 'female'][info['Sex']]}
311314
if self.DEBUG:
312-
print ' %s[Recommend]' % msg_prefix
313-
print ' -----------------------------'
314-
print ' | NickName: %s' % info['NickName']
315-
print ' | Alias: %s' % info['Alias']
316-
print ' | Local: %s %s' % (info['Province'], info['City'])
317-
print ' | Gender: %s' % ['unknown', 'male', 'female'][info['Sex']]
318-
print ' -----------------------------'
315+
print (' %s[Recommend]' % msg_prefix)
316+
print (' -----------------------------')
317+
print (' | NickName: %s' % info['NickName'])
318+
print (' | Alias: %s' % info['Alias'])
319+
print (' | Local: %s %s' % (info['Province'], info['City']))
320+
print (' | Gender: %s' % ['unknown', 'male', 'female'][info['Sex']])
321+
print (' -----------------------------')
319322
elif mtype == 47:
320323
msg_content['type'] = 6
321324
msg_content['data'] = self.search_content('cdnurl', content)
322325
if self.DEBUG:
323-
print ' %s[Animation] %s' % (msg_prefix, msg_content['data'])
326+
print (' %s[Animation] %s' % (msg_prefix, msg_content['data']))
324327
elif mtype == 49:
325328
msg_content['type'] = 7
326329
app_msg_type = ''
@@ -338,39 +341,39 @@ def extract_msg_content(self, msg_type_id, msg):
338341
'url': msg['Url'],
339342
'from': self.search_content('appname', content, 'xml')}
340343
if self.DEBUG:
341-
print ' %s[Share] %s' % (msg_prefix, app_msg_type)
342-
print ' --------------------------'
343-
print ' | title: %s' % msg['FileName']
344-
print ' | desc: %s' % self.search_content('des', content, 'xml')
345-
print ' | link: %s' % msg['Url']
346-
print ' | from: %s' % self.search_content('appname', content, 'xml')
347-
print ' --------------------------'
344+
print (' %s[Share] %s' % (msg_prefix, app_msg_type))
345+
print (' --------------------------')
346+
print (' | title: %s' % msg['FileName'])
347+
print (' | desc: %s' % self.search_content('des', content, 'xml'))
348+
print (' | link: %s' % msg['Url'])
349+
print (' | from: %s' % self.search_content('appname', content, 'xml'))
350+
print (' --------------------------')
348351

349352
elif mtype == 62:
350353
msg_content['type'] = 8
351354
msg_content['data'] = content
352355
if self.DEBUG:
353-
print ' %s[Video] Please check on mobiles' % msg_prefix
356+
print (' %s[Video] Please check on mobiles' % msg_prefix)
354357
elif mtype == 53:
355358
msg_content['type'] = 9
356359
msg_content['data'] = content
357360
if self.DEBUG:
358-
print ' %s[Video Call]' % msg_prefix
361+
print (' %s[Video Call]' % msg_prefix)
359362
elif mtype == 10002:
360363
msg_content['type'] = 10
361364
msg_content['data'] = content
362365
if self.DEBUG:
363-
print ' %s[Redraw]' % msg_prefix
366+
print (' %s[Redraw]' % msg_prefix)
364367
elif mtype == 10000:
365368
msg_content['type'] = 12
366369
msg_content['data'] = msg['Content']
367370
if self.DEBUG:
368-
print ' [Red Packet]'
371+
print (' [Red Packet]')
369372
else:
370373
msg_content['type'] = 99
371374
msg_content['data'] = content
372375
if self.DEBUG:
373-
print ' %s[Unknown]' % msg_prefix
376+
print (' %s[Unknown]' % msg_prefix)
374377
return msg_content
375378

376379
def handle_msg(self, r):
@@ -417,7 +420,7 @@ def handle_msg(self, r):
417420
user['name'] = 'unknown'
418421

419422
if self.DEBUG and msg_type_id != 0:
420-
print '[MSG] %s:' % user['name']
423+
print ('[MSG] %s:' % user['name'])
421424
content = self.extract_msg_content(msg_type_id, msg)
422425
message = {'msg_type_id': msg_type_id,
423426
'msg_id': msg['MsgId'],
@@ -505,7 +508,7 @@ def send_msg(self, name, word, isfile=False):
505508
result = True
506509
for line in f.readlines():
507510
line = line.replace('\n', '')
508-
print '-> ' + name + ': ' + line
511+
print ('-> ' + name + ': ' + line)
509512
if self.send_msg_by_uid(line, uid):
510513
pass
511514
else:
@@ -519,7 +522,7 @@ def send_msg(self, name, word, isfile=False):
519522
return False
520523
else:
521524
if self.DEBUG:
522-
print '[ERROR] This user does not exist .'
525+
print ('[ERROR] This user does not exist .')
523526
return True
524527

525528
@staticmethod
@@ -537,24 +540,24 @@ def search_content(key, content, fmat='attr'):
537540
def run(self):
538541
self.get_uuid()
539542
self.gen_qr_code('qr.png')
540-
print '[INFO] Please use WeCaht to scan the QR code .'
543+
print ('[INFO] Please use WeCaht to scan the QR code .')
541544
self.wait4login(1)
542-
print '[INFO] Please confirm to login .'
545+
print ('[INFO] Please confirm to login .')
543546
self.wait4login(0)
544547
if self.login():
545-
print '[INFO] Web WeChat login succeed .'
548+
print ('[INFO] Web WeChat login succeed .')
546549
else:
547-
print '[ERROR] Web WeChat login failed .'
550+
print ('[ERROR] Web WeChat login failed .')
548551
return
549552
if self.init():
550-
print '[INFO] Web WeChat init succeed .'
553+
print ('[INFO] Web WeChat init succeed .')
551554
else:
552-
print '[INFO] Web WeChat init failed'
555+
print ('[INFO] Web WeChat init failed')
553556
return
554557
self.status_notify()
555558
self.get_contact()
556-
print '[INFO] Get %d contacts' % len(self.contact_list)
557-
print '[INFO] Start to process messages .'
559+
print ('[INFO] Get %d contacts' % len(self.contact_list))
560+
print ('[INFO] Start to process messages .')
558561
self.proc_msg()
559562

560563
def get_uuid(self):
@@ -610,9 +613,9 @@ def wait4login(self, tip):
610613
self.base_uri = redirect_uri[:redirect_uri.rfind('/')]
611614
return True
612615
elif code == '408':
613-
print '[ERROR] WeChat login timeout .'
616+
print ('[ERROR] WeChat login timeout .')
614617
else:
615-
print '[ERROR] WeChat login exception .'
618+
print ('[ERROR] WeChat login exception .')
616619
return False
617620

618621
def login(self):
@@ -693,7 +696,7 @@ def sync_check(self):
693696
'synckey': self.sync_key_str,
694697
'_': int(time.time()),
695698
}
696-
url = 'https://' + self.sync_host + '.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?' + urllib.urlencode(params)
699+
url = 'https://' + self.sync_host + '.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?' + urllib.parse.urlencode(params)
697700
try:
698701
r = self.session.get(url)
699702
except (ConnectionError, ReadTimeout):

截图.png

28.2 KB
Loading

0 commit comments

Comments
 (0)