http://blog.csdn.net/mdpsdhr/article/details/61195817
#coding=utf-8 import itchat from itchat.content import * @itchat.msg_register([PICTURE,TEXT]) def simple_reply(msg): if msg[‘Type‘] == TEXT: ReplyContent = ‘I received message: ‘+msg[‘Content‘] if msg[‘Type‘] == PICTURE: ReplyContent = ‘I received picture: ‘+msg[‘FileName‘] itchat.send_msg(‘nice to meet you‘,msg[‘FromUserName‘]) itchat.auto_login() itchat.run()
- 这里注册了两个消息类型,文本和图片(表情),当微信接收到这两个消息时就会进入注册的函数simple_reply,msg是一个字典类型里面包含了消息数据包,有发送者、接收者、消息类型、消息内容等超多的信息
- itchat要注册消息类型,比如注册了TEXT(itchat.content.text),就会接收文本消息,其他消息不会触发函数。消息类型见库中的content.py文件
- 消息类型判断,msg[‘Type’]
- 消息发起者,msg[‘FromUserName’]
- 消息接收者,msg[‘ToUserName’]
- 文本消息,msg[‘Content’]
- 文件名字,msg[‘FileName’],注:如果是自带的表情就会显示表情
- 发送文件时,文件名字应该是unicode编码(这是python内部默认的编码风格)
- 如果想要指定向某人发送信息,
- 则
itchat.send_msg(‘nice to meet you‘,msg[‘FromUserName‘])里面的
msg[‘FromUserName‘]替换成要发送到人的UserName
-
#coding=utf-8 import itchat from itchat.content import * # @itchat.msg_register([PICTURE,TEXT]) # def simple_reply(msg): # if msg[‘Type‘] == TEXT: # ReplyContent = ‘I received message: ‘+msg[‘Content‘] # if msg[‘Type‘] == PICTURE: # ReplyContent = ‘I received picture: ‘+msg[‘FileName‘] # itchat.send_msg(‘nice to meet you‘,msg[‘FromUserName‘]) itchat.auto_login(hotReload=True) #itchat.run() friends_info = itchat.get_friends(update=True) for i in friends_info: print(i) #打印每个好友的信息 itchat.send_msg(‘nice to meet you‘,‘******‘)
上面的程序可打印出每个好友的信息