Python微信企業(yè)號開發(fā)之回調(diào)模式接收微信端客戶端發(fā)送消息及被動返回消息示例
本文實例講述了Python微信企業(yè)號開發(fā)之回調(diào)模式接收微信端客戶端發(fā)送消息及被動返回消息。分享給大家供大家參考,具體如下:
說明:此代碼用于接收手機微信端發(fā)送的消息
#-*- coding:utf-8 -*-
from flask import Flask,request
from WXBizMsgCrypt import WXBizMsgCrypt
import xml.etree.cElementTree as ET
import sys
app = Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
sToken = 'Uxxxx'
sEncodingAESKey = 'U2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
sCorpID = 'wx1xxxxxxxxxxxxx'
wxcpt=WXBizMsgCrypt(sToken,sEncodingAESKey,sCorpID)
#獲取url驗證時微信發(fā)送的相關(guān)參數(shù)
sVerifyMsgSig=request.args.get('msg_signature')
sVerifyTimeStamp=request.args.get('timestamp')
sVerifyNonce=request.args.get('nonce')
sVerifyEchoStr=request.args.get('echostr')
#
sReqMsgSig = sVerifyMsgSig
sReqTimeStamp = sVerifyTimeStamp
sReqNonce = sVerifyNonce
#
sResqMsgSig = sVerifyMsgSig
sResqTimeStamp = sVerifyTimeStamp
sResqNonce = sVerifyNonce
#驗證url
if request.method == 'GET':
ret,sEchoStr=wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp,sVerifyNonce,sVerifyEchoStr)
print type(ret)
print type(sEchoStr)
if (ret != 0 ):
print "ERR: VerifyURL ret:" + ret
sys.exit(1)
return sEchoStr
#接收客戶端消息
if request.method == 'POST':
#sReqMsgSig = request.form.get('msg_signature')
#sReqTimeStamp = request.form.get('timestamp')
#sReqNonce = request.form.get('nonce')
#賦值url驗證請求相同的參數(shù),使用上面注釋掉的request.form.get方式獲取時,測試有問題
sReqMsgSig = sVerifyMsgSig
sReqTimeStamp = sVerifyTimeStamp
sReqNonce = sVerifyNonce
sReqData = request.data
print sReqData
ret,sMsg=wxcpt.DecryptMsg( sReqData, sReqMsgSig, sReqTimeStamp, sReqNonce)
if (ret != 0):
print "ERR: VerifyURL ret:"
sys.exit(1)
#解析發(fā)送的內(nèi)容并打印
xml_tree = ET.fromstring(sMsg)
content = xml_tree.find("Content").text
print content
#被動響應消息,將微信端發(fā)送的消息返回給微信端
sRespData = '''<xml>
<ToUserName><![CDATA[mycreate]]></ToUserName>
<FromUserName><![CDATA[wx177d1233ab4b730b]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[''' +content +''']]></Content>
<MsgId>1234567890123456</MsgId>
<AgentID>1</AgentID>
</xml>'''
ret,sEncryptMsg=wxcpt.EncryptMsg(sRespData, sReqNonce, sReqTimeStamp)
if( ret!=0 ):
print "ERR: EncryptMsg ret: " + ret
sys.exit(1)
return sEncryptMsg
if __name__ == '__main__':
app.run(host='0.0.0.0',port=6000,debug=True)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進階經(jīng)典教程》。
希望本文所述對大家Python程序設(shè)計有所幫助。
- python實現(xiàn)的自動發(fā)送消息功能詳解
- 使用python3調(diào)用wxpy模塊監(jiān)控linux日志并定時發(fā)送消息給群組或好友
- python實現(xiàn)給微信指定好友定時發(fā)送消息
- python實現(xiàn)微信定時每天和女友發(fā)送消息
- python3+pyqt5+itchat微信定時發(fā)送消息的方法
- Python定時發(fā)送消息的腳本:每天跟你女朋友說晚安
- python模仿網(wǎng)頁版微信發(fā)送消息功能
- python實現(xiàn)給微信公眾號發(fā)送消息的方法
- Python 網(wǎng)絡(luò)編程起步(Socket發(fā)送消息)
- Python調(diào)用飛書發(fā)送消息的示例
相關(guān)文章
python 腳本生成隨機 字母 + 數(shù)字密碼功能
本文通過一小段簡單的代碼給大家分享基于python 腳本生成隨機 字母 + 數(shù)字密碼功能,感興趣的朋友跟隨腳本之家小編一起學習吧2018-05-05
Python字典創(chuàng)建 遍歷 添加等實用基礎(chǔ)操作技巧
字段是Python是字典中唯一的鍵-值類型,本文講述了Python中字典如何創(chuàng)建 遍歷 添加等實用基礎(chǔ)操作技巧,內(nèi)容非?;A(chǔ)但非常重要,一定要熟練掌握2018-09-09
Python中dictionary items()系列函數(shù)的用法實例
這篇文章主要介紹了Python中dictionary items()系列函數(shù)的用法,很實用的函數(shù),需要的朋友可以參考下2014-08-08

