{"id":21435491,"url":"https://github.com/gusibi/python-weixin","last_synced_at":"2025-04-08T09:09:05.145Z","repository":{"id":28793151,"uuid":"32316024","full_name":"gusibi/python-weixin","owner":"gusibi","description":"微信(weixin|wechat) Python SDK 支持开放平台和公众平台 支持微信小程序云开发","archived":false,"fork":false,"pushed_at":"2021-01-26T14:16:49.000Z","size":151,"stargazers_count":839,"open_issues_count":8,"forks_count":230,"subscribers_count":41,"default_branch":"master","last_synced_at":"2024-09-23T09:12:57.084Z","etag":null,"topics":["python","python-weixin","wechat","wechat-app","wechat-sdk","weixin","weixin-sdk","wxapp","wxcloud"],"latest_commit_sha":null,"homepage":"https://github.com/gusibi/python-weixin/wiki/快速开始","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gusibi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-16T09:59:45.000Z","updated_at":"2024-09-06T10:26:30.000Z","dependencies_parsed_at":"2022-09-26T16:20:24.605Z","dependency_job_id":null,"html_url":"https://github.com/gusibi/python-weixin","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gusibi%2Fpython-weixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gusibi%2Fpython-weixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gusibi%2Fpython-weixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gusibi%2Fpython-weixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gusibi","download_url":"https://codeload.github.com/gusibi/python-weixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809963,"owners_count":20999816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","python-weixin","wechat","wechat-app","wechat-sdk","weixin","weixin-sdk","wxapp","wxcloud"],"created_at":"2024-11-22T23:44:23.533Z","updated_at":"2025-04-08T09:09:05.122Z","avatar_url":"https://github.com/gusibi.png","language":"Python","readme":"python-weixin\n-----\n![Build Status](https://travis-ci.org/gusibi/python-weixin.svg?branch=master)\n![](https://img.shields.io/badge/version-0.5.0--dev-FF00CC.svg)\n![](https://img.shields.io/github/forks/gusibi/python-weixin.svg) \n![](https://img.shields.io/github/stars/gusibi/python-weixin.svg)\n![](https://img.shields.io/github/issues/gusibi/python-weixin.svg)\n![](https://img.shields.io/badge/license-New%20BSD-blue.svg)\n-----\n\nA Python client for the Weixin REST APIs\n\n## Compatibility\n\n|component|compatibility|\n|-----|-----|\n|Python|2.\\*, 3.\\*|\n\n\nInstallation\n-----\n\n```\npip install python-weixin\n```\n\nRequires\n-----\n\n```\n* requests\n* simplejson\n* six\n* xmltodict\n* pycryptodome\n```\n\n### [快速入门](https://github.com/gusibi/python-weixin/wiki)\n\n* [微信公众平台](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%B9%B3%E5%8F%B0)\n* [微信授权](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E6%8E%88%E6%9D%83)\n* [微信支付](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98)\n* [微信小程序云开发](https://github.com/gusibi/python-weixin/wiki/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BA%91%E5%BC%80%E5%8F%91)\n\n### 微信小程序使用示例：\n\n[微信小程序登录文档](https://developers.weixin.qq.com/miniprogram/dev/api/wx.login.html)\n\n微信小程序登录获取用户信息\n-----\n\n[Python 实现小程序登录注册](https://mp.weixin.qq.com/s?__biz=MzAwNjI5MjAzNw==\u0026mid=2655752024\u0026idx=1\u0026sn=cb02c316b9e1b569b6da41b15d348d9a\u0026chksm=80b0b872b7c73164335ebb93c9da45d0840068170a3749d3f285ac95a54734c34afa08c1ad0b#rd)\n\n``` python\nfrom weixin import WXAPPAPI\nfrom weixin.lib.wxcrypt import WXBizDataCrypt\n\napi = WXAPPAPI(appid=APP_ID,\n                  app_secret=APP_SECRET)\nsession_info = api.exchange_code_for_session_key(code=code)\n\n# 获取session_info 后\n\nsession_key = session_info.get('session_key')\ncrypt = WXBizDataCrypt(WXAPP_APPID, session_key)\n\n# encrypted_data 包括敏感数据在内的完整用户信息的加密数据\n# iv 加密算法的初始向量\n# 这两个参数需要js获取\nuser_info = crypt.decrypt(encrypted_data, iv)\n\n```\n\nAuthentication\n-----\n\nWeixin API 使用 OAuth2 认证方式\n详情见: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list\u0026t=resource/res_list\u0026verify=1\u0026lang=zh_CN\n\n### Authenticating a user\n具体使用方法参考 sample app\n\n### Using an access token\n获取到access token 后，可以使用token 获取 用户信息等:\n\n#### 微信开放平台使用示例：\n\n``` python\nfrom weixin.client import WeixinAPI\n\nscope = (\"snsapi_login\", )\napi = WeixinAPI(appid=APP_ID,\n                app_secret=APP_SECRET,\n                redirect_uri=REDIRECT_URI)\nauthorize_url = api.get_authorize_url(scope=scope)\n\naccess_token = api.exchange_code_for_access_token(code=code)\n\napi = WeixinAPI(access_token=access_token)\n\nuser = api.user(openid=\"openid\")\n```\n\n#### 微信公众平台使用示例：\n\n``` python\nfrom weixin.client import WeixinMpAPI\n\nscope = (\"snsapi_base\", )\napi = WeixinMpAPI(appid=APP_ID,\n                  app_secret=APP_SECRET,\n                  redirect_uri=REDIRECT_URI)\nauthorize_url = api.get_authorize_url(scope=scope)\n\naccess_token = api.exchange_code_for_access_token(code=code)\n\napi = WeixinMpAPI(access_token=access_token)\n\nuser = api.user(openid=\"openid\")\n```\n\n#### 微信支付使用示例:\n\n``` python\nfrom weixin.pay import WeixinPay\n\nwxpay = WeixinPay(appid='appid',\n                  mch_id='mchid',\n                  notify_url='url',\n                  partner_key='key')\n                  create_pay_info= {}\n\n# 统一下单\nwxpay.unifiedorder(**create_pay_info)\n# 查询订单\nwxapp.order_query(out_trade_no='out_trade_no')\n\n# 企业付款\nwxepay = WeixinEnterprisePay(appid='appid',\n                             mch_id='mchid',\n                             mch_key='mch_key',\n                             mch_cert='mch_cert',\n                             partner_key='key')\n\nwxepay.transfers(partner_trade_no,\n                 openid,\n                 amount,\n                 desc=u'结算')\n\n# 企业付款查询\nwxepay_query = WeixinEnterprisePayQuery(appid='appid',\n                                        mch_id='mchid',\n                                        mch_key='mch_key',\n                                        mch_cert='mch_cert',\n                                        partner_key='key')\nwxepay_query.gettransferinfo(partner_trade_no)\n```\n\n\n#### 创建自定义菜单\n\n#### 支持的按钮类型\n\n1. click：点击推事件用户点击click类型按钮后，微信服务器会通过消息接口推送消息类型为event的结构给开发者（参考消息接口指南），并且带上按钮中开发者填写的key值，开发者可以通过自定义的key值与用户进行交互；\n2. view：跳转URL用户点击view类型按钮后，微信客户端将会打开开发者在按钮中填写的网页URL，可与网页授权获取用户基本信息接口结合，获得用户基本信息。\n3. scancode_push：扫码推事件用户点击按钮后，微信客户端将调起扫一扫工具，完成扫码操作后显示扫描结果（如果是URL，将进入URL），且会将扫码的结果传给开发者，开发者可以下发消息。\n4. scancode_waitmsg：扫码推事件且弹出“消息接收中”提示框用户点击按钮后，微信客户端将调起扫一扫工具，完成扫码操作后，将扫码的结果传给开发者，同时收起扫一扫工具，然后弹出“消息接收中”提示框，随后可能会收到开发者下发的消息。\n5. pic_sysphoto：弹出系统拍照发图用户点击按钮后，微信客户端将调起系统相机，完成拍照操作后，会将拍摄的相片发送给开发者，并推送事件给开发者，同时收起系统相机，随后可能会收到开发者下发的消息。\n6. pic_photo_or_album：弹出拍照或者相册发图用户点击按钮后，微信客户端将弹出选择器供用户选择“拍照”或者“从手机相册选择”。用户选择后即走其他两种流程。\n7. pic_weixin：弹出微信相册发图器用户点击按钮后，微信客户端将调起微信相册，完成选择操作后，将选择的相片发送给开发者的服务器，并推送事件给开发者，同时收起相册，随后可能会收到开发者下发的消息。\n8. location_select：弹出地理位置选择器用户点击按钮后，微信客户端将调起地理位置选择工具，完成选择操作后，将选择的地理位置发送给开发者的服务器，同时收起位置选择工具，随后可能会收到开发者下发的消息。\n9. media_id：下发消息（除文本消息）用户点击media_id类型按钮后，微信服务器会将开发者填写的永久素材id对应的素材下发给用户，永久素材类型可以是图片、音频、视频、图文消息。请注意：永久素材id必须是在“素材管理/新增永久素材”接口上传后获得的合法id。\n10. view_limited：跳转图文消息URL用户点击view_limited类型按钮后，微信客户端将打开开发者在按钮中填写的永久素材id对应的图文消息URL，永久素材类型只支持图文消息。请注意：永久素材id必须是在“素材管理/新增永久素材”接口上传后获得的合法id。\n\n#### 自定义菜单参数说明\n\n|参数\t        |是否必须\t            |说明\n|:------        |:--------              |:-----\n| button\t    | 是\t                | 一级菜单数组，个数应为1~3个\n| sub_button\t| 否\t                | 二级菜单数组，个数应为1~5个\n| type\t        | 是\t                | 菜单的响应动作类型\n| name\t        | 是\t                | 菜单标题，不超过16个字节，子菜单不超过40个字节\n| key\t        | click等点击类型必须\t| 菜单KEY值，用于消息接口推送，不超过128字节\n| url\t        | view类型必须\t        | 网页链接，用户点击菜单可打开链接，不超过1024字节\nmedia_id\tmedia_id类型和view_limited类型必须\t调用新增永久素材接口返回的合法media_id\n\n### 消息管理\n\n#### 被动回复\n\n被动回复是在用户发出请求后在respone 中包含的内容\n\n消息体见官方参考文档:\n[被动回复用户消息](https://mp.weixin.qq.com/wiki?t=resource/res_main\u0026id=mp1421140543\u0026token=\u0026lang=zh_CN)\n\n#### 客服消息\n\nAPI 列表\n\n* add_customservice     添加客服帐号\n* update_customservice  修改客服帐号\n* delete_customservice  删除客服帐号\n* getall_customservice  获取所有客服帐号\n* custom_message_send   发送客服消息\n\n消息体见官方参考文档:\n[客服消息](https://mp.weixin.qq.com/wiki?t=resource/res_main\u0026id=mp1421140547\u0026token=\u0026lang=zh_CN)\n\n#### 模板消息\n\nAPI 列表\n\n* template_message_send\n\n消息体见官方参考文档:\n[模板消息](https://mp.weixin.qq.com/wiki?t=resource/res_main\u0026id=mp1433751277\u0026token=\u0026lang=zh_CN)\n\n#### 有问题可以在公号讨论\n![关注公号](http://media.gusibi.mobi/ASlCYThVMfqenyMHXATIbnBew5Fyt8D3Y2l6k0fL4kZuIs0zzUUT21BCpOm51o2R)\n\n### 感谢\n\n[![jetbrains](http://media.gusibi.mobi/w3W6TnRrR2oR3BlPeG-0kaqqQLY-Hq9FQ9hsYJe4mdAGHqV_qY0smR7gFmmY8B_t)](https://www.jetbrains.com/)\n\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgusibi%2Fpython-weixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgusibi%2Fpython-weixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgusibi%2Fpython-weixin/lists"}