之前写过一篇python-requests获取好友列表的文章,简直花费了好多的时间和精力,又抓包,又找参数,又分析的,简直麻烦透顶,今天突然知道了另外一种捷径,几行代码就可以完成....
itchat
- itchat是一个开源的微信个人号接口,里面有好多功能,百度搜索下一搜一大堆...
- http://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/
- https://www.cnblogs.com/ouyangping/p/8453920.html
- http://itchat.readthedocs.io/zh/latest/
- 可以参考下
- 这篇文章就来简单的实现下 消息自动回复
- 自动回复消息的内容,就通过图灵机器人API来实现
- 具体的方法可以参考上面的链接
- 可以简单浏览下,用到哪个 可以去搜索下该怎么实现
from . import content
from .core import Core
from .config import VERSION
from .log import set_logging
__version__ = VERSION
instanceList = []
def new_instance():
newInstance = Core()
instanceList.append(newInstance)
return newInstance
originInstance = new_instance()
# I really want to use sys.modules[__name__] = originInstance
# but it makes auto-fill a real mess, so forgive me for my following **
# actually it toke me less than 30 seconds, god bless Uganda
# components.login
login = originInstance.login
get_QRuuid = originInstance.get_QRuuid
get_QR = originInstance.get_QR
check_login = originInstance.check_login
web_init = originInstance.web_init
show_mobile_login = originInstance.show_mobile_login
start_receiving = originInstance.start_receiving
get_msg = originInstance.get_msg
logout = originInstance.logout
# components.contact
update_chatroom = originInstance.update_chatroom
update_friend = originInstance.update_friend
get_contact = originInstance.get_contact
get_friends = originInstance.get_friends
get_chatrooms = originInstance.get_chatrooms
get_mps = originInstance.get_mps
set_alias = originInstance.set_alias
set_pinned = originInstance.set_pinned
add_friend = originInstance.add_friend
get_head_img = originInstance.get_head_img
create_chatroom = originInstance.create_chatroom
set_chatroom_name = originInstance.set_chatroom_name
delete_member_from_chatroom = originInstance.delete_member_from_chatroom
add_member_into_chatroom = originInstance.add_member_into_chatroom
# components.messages
send_raw_msg = originInstance.send_raw_msg
send_msg = originInstance.send_msg
upload_file = originInstance.upload_file
send_file = originInstance.send_file
send_image = originInstance.send_image
send_video = originInstance.send_video
send = originInstance.send
revoke = originInstance.revoke
# components.hotreload
dump_login_status = originInstance.dump_login_status
load_login_status = originInstance.load_login_status
# components.register
auto_login = originInstance.auto_login
configured_reply = originInstance.configured_reply
msg_register = originInstance.msg_register
run = originInstance.run
# other functions
search_friends = originInstance.search_friends
search_chatrooms = originInstance.search_chatrooms
search_mps = originInstance.search_mps
set_logging = set_logging
图灵机器人
-
注册个账号 然后创建个自己的机器人(忽略我已经创建过了的)
image.png
image.png
image.png
-
创建成功后 会生成一个apikey
-
帮助文档
image.png
-
传送3个参数
1.key : apikey
2.info :发送的消息
3.userid : 'robot' -
具体我也不知道怎么解释哈哈哈 ,也是参考别人的文章
-
上代码
# -*- coding: utf-8 -*-
import itchat
import requests
#微信登录
#第一次登录要扫码,之后不需要,但是有时效
itchat.auto_login(hotReload=True)
#图灵机器人API
apiurl = 'http://www.tuling123.com/openapi/api'
def get_info(msg):
'''
给图灵机器人发消息,获取回复并返回
:param msg:
:return:
'''
data = {
'key':'bd542e4b6f654555af7bd29d78dad898',
'info': msg,
'userid': 'robot',
}
reply_msg = requests.post(apiurl, data=data).json()
print('我的回复:%s'%reply_msg)
return reply_msg['text']
@itchat.msg_register(itchat.content.TEXT)
def auto_reply(msg):
#搜索微信好友
realFriend = itchat.search_friends(name='老李')
realFriendsName = realFriend[0]['UserName']
print(realFriend)
print(realFriendsName)
#打印好友回复的信息
print('收到消息:%s'%msg['Text'])
#调用图灵接口
reply = get_info(msg['Text'])
#设置指定人物
if msg['FromUserName'] == realFriendsName:
itchat.send(reply, toUserName=realFriendsName)
itchat.run()

总结:
1.长知识了啊哈哈哈哈哈
2.还有待研究
网友评论