1 2 3 4 5 6 7 8 | def getTokenIntime(CorpId,Secret): res = urllib2.urlopen('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'%(CorpId,Secret)) res_dict = simplejson.loads(res.read()) token = res_dict.get('access_token',False) if not token: with open(r'./wx_log.txt','ab') as f: f.write(u'异常: 无法取得token') return token |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | def sendTxtMsg(token,content,to_user="@all",to_party="",to_tag="",application_id=0,safe=0): try: data = { "touser": to_user, "toparty": to_party, "totag": to_tag, "msgtype": "text", "agentid": application_id, "text": { "content": content, }, "safe":safe } data = simplejson.dumps(data,ensure_ascii=False) if Debug: with open(r'./wx_data.txt','ab') as f: f.write(data+'\r\n') req = urllib2.Request('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'%(token,)) resp = urllib2.urlopen(req,data) msg = u'返回值:' + resp.read() except Exception,ex: msg = u'异常:' + str(ex) finally: with open(r'./wx_log.txt','ab') as f: f.write(msg+'\r\n') |
1 | url(r'^$','wx_handler.views.veryfi_callback'), |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # -*- coding: utf-8 -*- from django.shortcuts import render from django.http import HttpResponse from .WXBizMsgCrypt import WXBizMsgCrypt import xml.etree.cElementTree as ET def veryfi_callback(req): msg_signature = req.GET.get('msg_signature',False) timestamp = req.GET.get('timestamp',False) nonce = req.GET.get('nonce',False) wxcpt = WXBizMsgCrypt('MJyh7es2bn1R','8j1F3fXp3eGn6RMeC17RtYiHiEUxUpgCrXassD3vHVE','wxd33505f2be01d541') if req.method =='GET': echostr = req.GET.get('echostr',False) ret,sEchoStr=wxcpt.VerifyURL(msg_signature, timestamp,nonce,echostr) if(ret!=0): ret_val = "ERR: VerifyURL ret: " + str(ret) return ret_val return HttpResponse(sEchoStr) else: if req.method=='POST': data = req.body #not POST ret,sMsg=wxcpt.DecryptMsg(data, msg_signature, timestamp, nonce) if( ret!=0 ): ret_val = "ERR: DecryptMsg ret: " + str(ret) return HttpResponse(ret_val) xml_tree = ET.fromstring(sMsg) from_uid = xml_tree.find('FromUserName').text content = xml_tree.find("Content").text sRespData = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[wxd33505f9be01d541]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><MsgId>1</MsgId><AgentID>1</AgentID></xml>"%(from_uid,'you saied:' + content,) ret,sEncryptMsg=wxcpt.EncryptMsg(sRespData, nonce, timestamp) return HttpResponse(sEncryptMsg) else: return HttpResponse("http method not suppoted") |
欢迎光临 Odoo中文网|Odoo实施培训 (http://www.chinaodoo.net/) | Powered by Discuz! X3.2 |