Python学习-Itchat微信API

    xiaoxiao2021-03-25  108

    Itchat-微信API接口for python

    说明

    Itchat是用python对微信API的封装 支持发送消息、图片、视频、地图、名片、文件等支持热登陆,不用每次登陆都要扫描二维码支持上传中文文件

    简单示例1:自动回复 code

    #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’)发送图片,ToUser不指定时发给自己 itchat.send_image(ImageName.decode(‘utf-8’),ToUser) # 发送图片发送视频 itchat.send_video(VideoName.decode(‘utf-8’),ToUser) # 发送图片发送文件 itchat.send_file(path.decode(‘utf-8’)) # 图片(文件)

    中文支持

    Github下载或下载将下载的文件fields.py放在requests 包的 packages/urllib3 下即可,替代原有的文件
    转载请注明原文地址: https://ju.6miu.com/read-20213.html

    最新回复(0)