def SendMsg(params): try: Log("params:%s" % params, Type="DEBUG") strAppID = params["appID"] strToUser = params["toUser"] text = params["text"] Log("SendMsg AppID:%s, toUser:%s, Text:%s" % (strAppID, strToUser, text), Type="DEBUG") #authorizer_appid,authorizer_access_token, authorizer_refresh_token = GetAuthInfo(self.strQueryCode) #Log("authorizer_appid: %s, Authorizer_access_token:%s"%(authorizer_appid,authorizer_access_token)) #GetAccountInfo(authorizer_appid, authorizer_access_token) wxTempObj = wxAccountInterface(strAppID) strAccessToken = wxTempObj.GetAccountToken() Log("GetAccessToken:%s" % strAccessToken) url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" % strAccessToken values = { "touser": strToUser, "msgtype": "text", "text": { "content": text } } Log("Read to send CRM message:%s" % values) res = Post(url, values) except Exception, e: Log("Error in SendMsg:%s" % e, Type="DEBUG")
def SendImage(params): try: strAppID = params["appID"] strToUser = params["toUser"] media_id = params["media_id"] Log("AppID:%s, toUser:%s, Text:%s" % (strAppID, strToUser, media_id)) #authorizer_appid,authorizer_access_token, authorizer_refresh_token = GetAuthInfo(self.strQueryCode) #Log("authorizer_appid: %s, Authorizer_access_token:%s"%(authorizer_appid,authorizer_access_token)) #GetAccountInfo(authorizer_appid, authorizer_access_token) wxTempObj = wxAccountInterface(strAppID) strAccessToken = wxTempObj.GetAccountToken() Log("GetAccessToken:%s" % strAccessToken) url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" % strAccessToken values = { "touser": strToUser, "msgtype": "image", "image": { "media_id": media_id } } Log("Read to send CRM message:%s" % values) res = Post(url, values) except Exception, e: Log("Error in SendVoice%s" % e)
def SendSignupTemplateMseeage(params): appid = params["appID"] openid = params["openID"] className = params["name"] signupTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") template_id = "cZBmCjkFan3TW9nXpfEYciKJV9pDqCbKPt12BtWb7dc" wxObj = wxAccountInterface(appid) url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s" % wxObj.GetAccountToken( ) params = { "touser": openid, "template_id": template_id, #"url":"https://applinzi.ddianke.com/wxJSWeb/bindusertoken", "data": { "first": { "value": "报名成功!", "color": "#173177" }, "keyword1": { "value": className, "color": "#173177" }, "keyword2": { "value": signupTime, "color": "#173177" }, "remark": { "value": "祝您学习愉快", "color": "#173177" } } } res = Post(url, params) return res
def home(request): try: wxAppId = "wx92a26ba6653d5b56" #生菜阅读服务号 wxObj = wxAccountInterface(wxAppId) url = "http://applinzi.ddianke.com%s" % request.get_full_path() ticket = wxObj.GetTicket("jsapi") signature = wxSign(ticket, url).sign() params = request.GET template = loader.get_template('eshop.html') #user = wxUserClass() if request.method == 'GET' and ('code' in request.GET.keys()): code = params['code'] (accesstoken, openId) = GetWebAccessToken(code) userInfo = GetUserInfo(accesstoken, openId) user = wxUserClass(wxAppId, openId) Log("userData:%s" % user.wxUserData.Name) context = RequestContext( request, { "userinfo": user.wxUserData, "appid": wxAppId, "sign": signature, "ticket": ticket }) return HttpResponse(template.render(context)) except Exception, e: Log("play error!%s" % e) return HttpResponse("Error! %s" % params)
def verificationcode(request, wxAppId, openId): try: wx_user = wxUser.objects.filter(SourceAccount=wxAppId, openID=openId) if wx_user: user = wx_user[0] if user.Subscribed != 1: Log("bindusertoken 1", Type="DEBUG") return {"subscribed": False} elif user.MobileBound: #指定发送开课模板 wxObj = wxAccountInterface(wxAppId) wxObj.CallTemplateMessage(6, openId, user.Name, '', user.Mobile or "您绑定的手机号") return {"subscribed": True, "bind": True} Log("bindusertoken 2", Type="DEBUG") return {"subscribed": True, "bind": False} except Exception, e: Log("bindusertoken Error! :%s" % e, Type="DEBUG")
def wxJSWeb(request, page): try: wxAppId = "wx457b9d0e6f93d1c5" #大指点课服务号 url = "http://applinzi.ddianke.com%s" % request.get_full_path() Log("wxJSWeb URL: %s" % url) wxObj = wxAccountInterface(wxAppId) if request.method == 'GET' and ('code' in request.GET.keys()): #Log("wxJSWeb start:", Type="DEBUG") code = request.GET.get("code") (accesstoken, openId) = GetWebAccessToken(code) userInfo = GetUserInfo(accesstoken, openId) #strtest = userInfo.get("nickname", "匿名用户") user = wxUserClass(wxAppId, openId) #user.wxUserData = user.SetUserInfo(userInfo) user.SetUserInfo(userInfo) else: redirecturl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" % ( wxAppId, url) return HttpResponseRedirect(redirecturl) #Log("wxJSWeb openid: %s" % openId, Type="Debug") ticket = wxObj.GetTicket("jsapi") signature = wxSign(ticket, url).sign() parameter = { "appid": wxAppId, "sign": signature, "ticket": ticket, "openid": openId, "name": userInfo.get("nickname", "匿名用户") } if WXJSWEB_OPERATOR.get(page): parameter.update( WXJSWEB_OPERATOR.get(page)(request, wxAppId, openId)) #Log("UserList: %s" % userlist) template = loader.get_template('%s.html' % page) context = RequestContext(request, parameter) return HttpResponse(template.render(context)) except Exception, e: Log("wxJSWeb play error!%s" % e, Type="DEBUG") return HttpResponse("Error! %s" % e)
def __init__(self, token, xml): try: #mc.set("log", "Handler initiated started!") self.token = token self.wxObj = wxAccountInterface(token) #self.config = self.GetConfig(self.wxObj.GetConfig()) self.xml = xml self.fromUserName = xml.find("FromUserName").text self.toUserName = xml.find("ToUserName").text self.msgType = xml.find("MsgType").text self.user = wxUserClass(token, self.fromUserName) if (self.msgType == "image" or self.msgType == "voice"): self.mediaId = xml.find("MediaId").text #self.InfoType = xml.find("InfoType").text #self.ComponentVerifyTicket = xml.find("ComponentVerifyTicket").text userinfo = self.wxObj.GetUser(self.fromUserName) #self.user.wxUserData = self.user.SetUserInfo(userinfo) self.user.SetUserInfo(userinfo) #Log("user ID: %s" % self.user.wxUserData.ID, Type="DEBUG") Log("Handler initiated successfully! Token: %s, FromUser:%s, ToUser:%s, msgType: %s" % (token, self.fromUserName, self.toUserName, self.msgType)) except Exception, e: Log("Couldn't do __init__: %s" % e, Type="DEBUG")
def dzUserBind(request, wxAppId, openId): wxObj = wxAccountInterface(wxAppId) return {"userlist": wxObj}
def BuildIndex(appid): wxObj = wxAccountInterface(appid) wxObj.BuildIndex()