Skip to content

WeChat4 接口(4.x 版本)

支持版本:4.1.5.16 / 4.1.6.14 / 4.1.6.46 / 4.1.7.30 / 4.1.8.27 / 4.1.8.28 / 4.1.8.67

初始化

python
from pywechat_libencode import WeChat4

bot = WeChat4(version="4.1.8.67")

登录相关

接口方法版本限制
获取配置路径get_config_path()
初始化微信init()
检查登录状态check_login()
退出登录exit()
扫描二维码scan_qrcode(file)
刷新二维码refresh_qrcode()
自动登录auto_login()4.1.7.30+

个人信息

接口方法版本限制
获取个人信息(缓存)get_self_info()
获取个人信息(网络)get_self_info_by_net()
获取个人二维码get_self_qrcode(wxid, style, opcode, info)
修改个人昵称modify_self_nickname(nickname)
修改个人签名modify_self_signature(signature)
修改个人头像modify_self_headimg(file)

发送消息

接口方法版本限制
发送文本消息send_text(to_wxid, content)
发送群@消息send_room_at(to_wxid, content, at_list)
发送名片send_card(to_wxid, card_wxid)
发送图片send_image(to_wxid, file)
转发图片forward_image(to_wxid, file_id, aes_key)
转发视频forward_video(to_wxid, cdn_video_url, aes_key, ...)4.1.7.30+
通过CDN发送图片send_image_by_cdn(to_wxid, file_id, aes_key, ...)
发送文件send_file(to_wxid, file)
发送语音(silk/mp3)send_voice(to_wxid, file)
发送拍一拍send_pat(room_wxid, to_wxid)
发送引用消息send_quote(to_wxid, from_wxid, ...)不支持4.1.5.16
发送小程序消息send_applet_msg(to_wxid, content, msg_type)
发送位置消息send_location(to_wxid, title, address, latitude, longitude)
发送链接卡片send_link_card(to_wxid, title, description, thumb_url, url)
发送表情send_emotion(to_wxid, file)
发送收藏表情send_collection_emotion(to_wxid, md5, length)4.1.7.30+
发送XML消息send_xml(to_wxid, content, msg_type)

发送文本示例

python
bot.send_text(to_wxid="wxid_xxxxxxxxxxxxxx", content="Hello World!")

发送群@消息示例

python
# @指定成员
bot.send_room_at(
    to_wxid="123456789@chatroom",
    content="@昵称 你好",
    at_list=["wxid_xxxxxxxxxxxxxx"]
)

# @全体成员(需要是群主/管理员)
bot.send_room_at(
    to_wxid="123456789@chatroom",
    content="@全体成员 你好",
    at_list=["notify@all"]
)

发送位置消息示例

python
bot.send_location(
    to_wxid="wxid_xxxxxxxxxxxxxx",
    title="位置名称",
    address="详细地址",
    latitude="116.3975",
    longitude="39.9087"
)

好友管理

接口方法版本限制
获取好友列表get_contacts()
获取好友列表V2get_contacts_v2()4.1.7.30+
获取联系人信息get_contact(wxid)
快速获取联系人信息get_contact_by_cache(wxid)
获取所有好友wxidsget_friend_wxids()4.1.7.30+
获取所有群wxidsget_room_wxids()4.1.7.30+
批量获取wxid信息get_wxids_info(wxids)4.1.7.30+
更新单个联系人资料update_contact(wxid)
更新所有好友资料update_contacts()
设置联系人备注remark_contact(wxid, remark)
搜索好友search_friend(search)
添加好友add_friend(v3, v4, verify_content, scene, friend_flg)
通过好友验证verify_friend(v3, v4, scene)
通过好友验证V2verify_friend_v2(v3, v4, remark, label, scene)4.1.7.30+
删除联系人delete_contact(wxid)

群聊管理

接口方法版本限制
初始化群聊init_rooms()
创建群聊create_room(wxids)
通过链接进入群聊enter_room(url)
退出并删除群聊exit_room(room_wxid)
获取群聊列表get_rooms()
获取群列表V2get_rooms_v2()4.1.7.30+
获取群详情列表get_rooms_detail()4.1.7.30+
获取群聊信息get_room(room_wxid)
获取群详情(缓存)get_room_detail(room_wxid)
获取群成员列表get_room_members(room_wxid)
获取群成员列表(SQL)get_room_members_by_sql(room_wxid)4.1.7.30+
获取群成员联系人信息get_room_member(room_wxid, wxid)
获取群成员昵称get_room_member_nickname(room_wxid, wxid)
获取群成员名称get_room_member_name(room_wxid, wxid)
批量获取群成员信息get_rooms_members()
批量获取群成员信息缓存get_rooms_members_by_cache()
添加群成员add_room_members(room_wxid, wxids)
邀请群成员invite_room_members(room_wxid, wxids)
移除群成员remove_room_member(wxid_list, room_wxid)
设置群管理员add_room_admin(room_wxid, admin)
移除群管理员remove_room_admin(room_wxid, admin)
获取群公告get_room_announcement(room_wxid)4.1.7.30+
设置群公告set_room_announcement(room_wxid, announcement)
修改群名称modify_room_name(wxid, name)
转让群主transfer_room(room_wxid, wxid)
保存群聊到通讯录save_address_book(room_wxid)
从通讯录移除群聊remove_address_book(room_wxid)

标签管理

接口方法版本限制
获取标签列表get_labels()
添加标签add_label(label_name)
删除标签delete_label(label_id)
修改联系人标签modify_contact_label(wxids, label_id)
修改标签名称modify_label_name(label_id, label_name)不支持4.1.5.16

聊天设置

接口方法版本限制
置顶聊天pin(wxid)
取消置顶unpin(wxid)
折叠聊天fold(wxid)4.1.7.30+
取消折叠unfold(wxid)4.1.7.30+
设为星标好友star(wxid)
取消星标好友unstar(wxid)
消息免打扰mute(wxid)
取消消息免打扰unmute(wxid)
加入黑名单add_black(wxid)
移出黑名单remove_black(wxid)

消息操作

接口方法版本限制
撤回消息revoke_msg(to_wxid, new_msg_id, create_time)不支持4.1.5.16
防撤回开关anti_revoke(status)

红包/转账

接口方法版本限制
查询红包详情query_redpacket_detail(native_url)
领取红包grab_redpacket(...)
打开红包open_redpacket(...)
确认转账confirm_transfer(transferid, invalid_time)
退还转账refund_transfer(transferid, invalid_time)
生成收款二维码generate_payment_qrcode(feed, remark)

朋友圈

接口方法版本限制
获取朋友圈首页sns_get_first_page(first_page_md5, max_id)
获取朋友圈下一页sns_get_next_page(max_sns_id)
获取朋友圈详情sns_get_detail(sns_id)
上传朋友圈文件sns_upload(file)
发布文本朋友圈sns_post(content, ...)
发布带图片的朋友圈sns_post_with_image(file_list, content)
发布图片朋友圈V2sns_post_with_image_v2(file_list, content)4.1.7.30+
发送视频朋友圈sns_post_video(content, video_path)4.1.7.30+
朋友圈上传图片(CDN)sns_upload_image_by_cdn(file_path, base64)4.1.7.30+
朋友圈上传视频(CDN)sns_upload_video_by_cdn(video_path, thumb_path, ...)4.1.7.30+
下载朋友圈图片/视频sns_download_media(url, task_id, save_path)4.1.7.30+
解密朋友圈图片sns_decode_image(file_path, save_path)4.1.7.30+
回复朋友圈评论sns_reply_comment(sns_id, comment_id, content)
删除朋友圈评论sns_delete_comment(sns_id, comment_id)
删除朋友圈sns_delete(sns_id)

文件下载

接口方法版本限制
CDN下载cdn_download(file_id, file_type, aes_key, save_path)
下载图片download_image(...)不支持4.1.5.16
下载文件download_file(...)
下载视频download_video(...)
下载语音download_voice(...)
下载企业微信文件download_wxwork_file(url, auth_key, key, save_path)4.1.7.30+
语音转文本voice_to_text(client_msg_id, new_msg_id, length)4.1.7.30+

数据库操作

接口方法版本限制
获取数据库句柄get_db_handle()
执行SQL语句exec_sql(db_name, sql)
解密数据库decrypt_db(db_path, save_path, key)4.1.7.30+
备份数据库backup_db(output_dir, name)

其他功能

接口方法版本限制
获取A8Keyget_a8key(url, url_type, scene)
获取小程序登录码get_mini_app_code(app_id)
获取附近的人get_nearby_people(longitude, latitude)
获取收藏列表get_collections()
获取CDN信息get_cdn_info()

基于 MIT 许可发布