docs/intro/login.md
在上一章中你看到了基本的注册与登陆,而显然登陆使用的是itchat提供了auto_login方法,调用即可完成登录。
一般而言,我们都会在完成消息的注册后登陆。
当然这里需要特别强调的是三点,分别是短时间关闭重连、命令行二维码与自定义登陆内容。
这样即使程序关闭,一定时间内重新开启也可以不用重新扫码。
最简单的用法就是给auto_login方法传入值为真的hotReload。
该方法会生成一个静态文件itchat.pkl,用于存储登陆的状态。
import itchat
from itchat.content import TEXT
@itchat.msg_register(TEXT)
def simple_reply(msg):
print(msg.text)
itchat.auto_login(hotReload=True)
itchat.run()
通过设置statusStorageDir可以将静态文件指定为其他的值。
这一内置选项其实就相当于使用了以下两个函数的这一段程序:
import itchat
from itchat.content import TEXT
if itchat.load_login_status():
@itchat.msg_register(TEXT)
def simple_reply(msg):
print(msg['Text'])
itchat.run()
itchat.dump_login_status()
else:
itchat.auto_login()
itchat.dump_login_status()
print('Config stored, so exit.')
其中load_login_status与dump_login_status分别对应读取与导出设置。
通过设置传入的fileDir的值可以设定导入导出的文件。
通过以下命令可以在登陆的时候使用命令行显示二维码:
itchat.auto_login(enableCmdQR=True)
部分系统可能字幅宽度有出入,可以通过将enableCmdQR赋值为特定的倍数进行调整:
# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
itchat.auto_login(enableCmdQR=2)
默认控制台背景色为暗色(黑色),若背景色为浅色(白色),可以将enableCmdQR赋值为负值:
itchat.auto_login(enableCmdQR=-1)
如果需要控制登录的过程,可以阅读下面的内容。
同时itchat也提供了登陆所需的每一步的方法,登陆的过程按顺序为:
获取生成二维码所需的uuid,并返回。
get_QRuuid根据uuid获取二维码并打开,返回是否成功。
get_QR判断是否已经登陆成功,返回扫描的状态码。
check_login获取微信用户信息以及心跳所需要的数据。
web_init获取微信的所有好友信息并更新。
get_friends(曾用名:get_contact)在手机上显示登录状态。
show_mobile_login循环扫描是否有新的消息,开启心跳包。
start_receivingitchat自带的auto_login通过如下代码可以实现:
import itchat, time, sys
def output_info(msg):
print('[INFO] %s' % msg)
def open_QR():
for get_count in range(10):
output_info('Getting uuid')
uuid = itchat.get_QRuuid()
while uuid is None: uuid = itchat.get_QRuuid();time.sleep(1)
output_info('Getting QR Code')
if itchat.get_QR(uuid): break
elif get_count >= 9:
output_info('Failed to get QR Code, please restart the program')
sys.exit()
output_info('Please scan the QR Code')
return uuid
uuid = open_QR()
waitForConfirm = False
while 1:
status = itchat.check_login(uuid)
if status == '200':
break
elif status == '201':
if waitForConfirm:
output_info('Please press confirm')
waitForConfirm = True
elif status == '408':
output_info('Reloading QR Code')
uuid = open_QR()
waitForConfirm = False
userInfo = itchat.web_init()
itchat.show_mobile_login()
itchat.get_friends(True)
output_info('Login successfully as %s'%userInfo['NickName'])
itchat.start_receiving()
# Start auto-replying
@itchat.msg_register
def simple_reply(msg):
if msg['Type'] == 'Text':
return 'I received: %s' % msg['Content']
itchat.run()