In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo('W-ZsWqcl1_c')
Out[1]:
by: Sam.Gu@KudosData.com
April 2017 ========== Scan the QR code to become trainer's friend in WeChat ========>>
In [ ]:
# from __future__ import unicode_literals, division
import time, datetime, requests, itchat
from itchat.content import *
In [ ]:
itchat.auto_login(hotReload=True) # hotReload=True: 退出程序后暂存登陆状态。即使程序关闭,一定时间内重新开启也可以不用重新扫码。
# itchat.auto_login(enableCmdQR=-2) # enableCmdQR=-2: 命令行显示QR图片
In [ ]:
# 获取自己的用户信息,返回自己的属性字典
friend = itchat.search_friends()
print(friend)
In [ ]:
print('NickName : %s' % friend['NickName'])
print('Alias A-ID: %s' % friend['Alias'])
print('RemarkName: %s' % friend['RemarkName'])
print('UserName : %s' % friend['UserName'])
In [ ]:
# 获取任何一项等于name键值的用户。
# 'NickName' 昵称, set by that friend, changeable
# 'Alias' ID微信号 = wechatAccount, one time set by that friend, cannot change
# 'RemarkName' 备注名, set by current login account owner, changeable by login account owner
# 注意:返回可能包含多个朋友。为什么呢?
friend = itchat.search_friends(name=u'Sam Gu')
# friend = itchat.search_friends(name=u'Mr. R')
# friend = itchat.search_friends(name=u'Ms. S')
In [ ]:
for i in range(0, len(friend)):
print('NickName : %s' % friend[i]['NickName'])
print('Alias A-ID: %s' % friend[i]['Alias'])
print('RemarkName: %s' % friend[i]['RemarkName'])
print('UserName : %s' % friend[i]['UserName'])
In [ ]:
# 获取分别对应相应键值的用户。
# friend = itchat.search_friends(nickName=u'Sam Gu')
# friend = itchat.search_friends(wechatAccount=u'Sam Gu')
friend = itchat.search_friends(remarkName=u'Sam Gu')
# friend = itchat.search_friends(userName=u'Sam Gu')
In [ ]:
for i in range(0, len(friend)):
print('NickName : %s' % friend[i]['NickName'])
print('Alias A-ID: %s' % friend[i]['Alias'])
print('RemarkName: %s' % friend[i]['RemarkName'])
print('UserName : %s' % friend[i]['UserName'])
In [ ]:
# 查找群组
# group = itchat.search_chatrooms(name=u'Data Science')
group = itchat.search_chatrooms(name=u'陪聊妹UAT')
In [ ]:
for i in range(0, len(group)):
print('NickName : %s' % group[i]['NickName'])
print('Alias A-ID: %s' % group[i]['Alias'])
print('RemarkName: %s' % group[i]['RemarkName'])
print('UserName : %s' % group[i]['UserName'])
print('Is Owner? : %s ( 0 for No | 1 for Yes )' % group[0]['IsOwner'])
print('Is Admin? : %s' % group[i]['IsAdmin'])
print('')
In [ ]:
# 文字
reply = itchat.send(u'别来无恙啊!\n发送时间:\n{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()), friend[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
In [ ]:
# 图片
reply = itchat.send_image('./reference/WeChat_SamGu_QR.png', friend[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
In [ ]:
# 文件
reply = itchat.send_file('./reference/logo.pdf', friend[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
In [ ]:
# 音频(语音可以先转成MP3)
reply = itchat.send_file('./reference/audio.mp3', friend[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
In [ ]:
# 视频
reply = itchat.send_video('./reference/video.mp4', friend[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
In [ ]:
# 发送信息去群组: group[0]['UserName']
# 文字
reply = itchat.send(u'别来无恙啊!\n发送时间:\n{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()), group[0]['UserName'])
print(reply['BaseResponse']['ErrMsg'])
显示发给自己的文本消息:
In [ ]:
# itchat.auto_login(hotReload=True) # hotReload=True: 退出程序后暂存登陆状态。即使程序关闭,一定时间内重新开启也可以不用重新扫码。
In [ ]:
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
print(msg['Text'])
In [ ]:
# 长期有效地运行(术语叫做:开始监听)
itchat.run()
回复发给自己的文本消息:
In [ ]:
# interupt, then re-login
itchat.auto_login(hotReload=True)
In [ ]:
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
print(msg['Text'])
return u'谢谢亲[嘴唇]我收到 I received:\n' + msg['Text']
In [ ]:
itchat.run()
In [ ]:
# interupt, then re-login
itchat.auto_login(hotReload=True)
In [ ]:
# 如果收到[TEXT, MAP, CARD, NOTE, SHARING]类的信息,会自动回复:
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) # 文字、位置、名片、通知、分享
def text_reply(msg):
itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
# 如果收到[PICTURE, RECORDING, ATTACHMENT, VIDEO]类的信息,会自动保存:
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) # 图片、语音、文件、视频
def download_files(msg):
msg['Text'](msg['FileName'])
return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
# 如果收到新朋友的请求,会自动通过验证添加加好友,并主动打个招呼:幸会幸会!Nice to meet you!
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
itchat.send_msg(u'幸会幸会!Nice to meet you!', msg['RecommendInfo']['UserName'])
# 在群里,如果收到@自己的文字信息,会自动回复:
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName'])
In [ ]:
itchat.run()
In [ ]:
# interupt, then logout
itchat.logout() # 安全退出