python-weixin
A Python client for the Weixin REST APIs
Compatibility
component | compatibility |
---|
Python | twenty three.* |
Installation
pip install python-weixin
Requires
* requests
* simplejson
* six
* xmltodict
* pycryptodome
Quick Start
- WeChat public platform
- WeChat authorization
- WeChat Pay
- WeChat Mini Program Cloud Development
Examples of using WeChat applet:
WeChat applet login document
Log in to WeChat applet to obtain user information
Python implements small program login and registration
from weixin import WXAPPAPI
from weixin . lib . wxcrypt import WXBizDataCrypt
api = WXAPPAPI ( appid = APP_ID ,
app_secret = APP_SECRET )
session_info = api . exchange_code_for_session_key ( code = code )
# 获取session_info 后
session_key = session_info . get ( 'session_key' )
crypt = WXBizDataCrypt ( WXAPP_APPID , session_key )
# encrypted_data 包括敏感数据在内的完整用户信息的加密数据
# iv 加密算法的初始向量
# 这两个参数需要js获取
user_info = crypt . decrypt ( encrypted_data , iv )
Authentication
For details on the OAuth2 authentication method used by Weixin API, please see: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
Authenticating a user
Please refer to the sample app for specific usage methods.
Using an access token
After obtaining the access token, you can use the token to obtain user information, etc.:
Examples of using WeChat open platform:
from weixin . client import WeixinAPI
scope = ( "snsapi_login" , )
api = WeixinAPI ( appid = APP_ID ,
app_secret = APP_SECRET ,
redirect_uri = REDIRECT_URI )
authorize_url = api . get_authorize_url ( scope = scope )
access_token = api . exchange_code_for_access_token ( code = code )
api = WeixinAPI ( access_token = access_token )
user = api . user ( openid = "openid" )
Examples of using WeChat public platform:
from weixin . client import WeixinMpAPI
scope = ( "snsapi_base" , )
api = WeixinMpAPI ( appid = APP_ID ,
app_secret = APP_SECRET ,
redirect_uri = REDIRECT_URI )
authorize_url = api . get_authorize_url ( scope = scope )
access_token = api . exchange_code_for_access_token ( code = code )
api = WeixinMpAPI ( access_token = access_token )
user = api . user ( openid = "openid" )
WeChat payment usage example:
from weixin . pay import WeixinPay
wxpay = WeixinPay ( appid = 'appid' ,
mch_id = 'mchid' ,
notify_url = 'url' ,
partner_key = 'key' )
create_pay_info = {}
# 统一下单
wxpay . unifiedorder ( ** create_pay_info )
# 查询订单
wxapp . order_query ( out_trade_no = 'out_trade_no' )
# 企业付款
wxepay = WeixinEnterprisePay ( appid = 'appid' ,
mch_id = 'mchid' ,
mch_key = 'mch_key' ,
mch_cert = 'mch_cert' ,
partner_key = 'key' )
wxepay . transfers ( partner_trade_no ,
openid ,
amount ,
desc = u'结算' )
# 企业付款查询
wxepay_query = WeixinEnterprisePayQuery ( appid = 'appid' ,
mch_id = 'mchid' ,
mch_key = 'mch_key' ,
mch_cert = 'mch_cert' ,
partner_key = 'key' )
wxepay_query . gettransferinfo ( partner_trade_no )
Create a custom menu
Supported button types
- click: Click push event After the user clicks the click type button, the WeChat server will push the message type event structure to the developer through the message interface (refer to the message interface guide), and bring the key value filled in by the developer in the button. The developer can Interact with users through customized key values;
- view: Jump URL After the user clicks the view type button, the WeChat client will open the web page URL filled in by the developer in the button. It can be combined with the web page authorization interface to obtain the user's basic information to obtain the user's basic information.
- scancode_push: After the user clicks the button in the scan code push event, the WeChat client will activate the scan tool. After completing the scan code operation, the scan result will be displayed (if it is a URL, the URL will be entered), and the scan code result will be passed to the developer. , developers can send messages.
- scancode_waitmsg: Scan the code to push the event and pop up the "Message Receiving" prompt box. After the user clicks the button, the WeChat client will launch the scan tool. After completing the code scanning operation, the result of the code scanning will be sent to the developer and at the same time, it will be closed. Scan the tool, and then the "Message Receiving" prompt box will pop up, and you may receive a message from the developer.
- pic_sysphoto: After the user clicks the button to take a picture and send a picture in the system, the WeChat client will call up the system camera. After completing the picture taking operation, the taken picture will be sent to the developer and the event will be pushed to the developer. At the same time, the system camera will be put away, and then You may receive messages from developers.
- pic_photo_or_album: After the user clicks the button to take a photo or send a picture to an album, the WeChat client will pop up a selector for the user to choose "take a photo" or "select from the mobile phone album". After the user chooses, he will go through the other two processes.
- pic_weixin: After the user clicks the button of the pop-up WeChat photo album sender, the WeChat client will call up the WeChat photo album. After completing the selection operation, the selected photos will be sent to the developer's server, and the event will be pushed to the developer. At the same time, the photo album will be closed. You may then receive a message from the developer.
- location_select: After the user clicks the button of the pop-up geographical location selector, the WeChat client will call up the geographical location selection tool. After completing the selection operation, the selected geographical location will be sent to the developer's server, and the location selection tool will be closed at the same time. The location selection tool may then be Receive a message from the developer.
- media_id: Send messages (except text messages). After the user clicks the media_id type button, the WeChat server will send the material corresponding to the permanent material ID filled in by the developer to the user. The permanent material type can be pictures, audios, videos, and graphic messages. . Please note: the permanent material ID must be a legal ID obtained after uploading through the "Material Management/Add Permanent Material" interface.
- view_limited: Jump to the graphic message URL. After the user clicks the view_limited type button, the WeChat client will open the graphic message URL corresponding to the permanent material ID filled in by the developer in the button. The permanent material type only supports graphic messages. Please note: the permanent material ID must be a legal ID obtained after uploading through the "Material Management/Add Permanent Material" interface.
Custom menu parameter description
parameter | Is it necessary | illustrate |
---|
button | yes | First-level menu array, the number should be 1~3 |
sub_button | no | Secondary menu array, the number should be 1~5 |
type | yes | Menu response action type |
name | yes | Menu title, no more than 16 bytes, submenu no more than 40 bytes |
key | Click and other click types are required | Menu KEY value, used for message interface push, no more than 128 bytes |
url | view type must | Web link, the user can click the menu to open the link, no more than 1024 bytes |
media_id media_id type and view_limited type must be the legal media_id returned by calling the new permanent material interface | | |
Message management
Passive reply
A passive reply is what is included in the response after the user makes a request
Please see the official reference document for the message body: Passively reply to user messages
Customer service message
API list
- add_customservice Add customer service account
- update_customservice Modify customer service account
- delete_customservice delete customer service account
- getall_customservice Get all customer service accounts
- custom_message_send Send customer service message
Please see the official reference document for the message: Customer Service Message
template message
API list
For the message body, see the official reference document: Template Message
If you have any questions, you can discuss them on the official account
grateful