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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import hashlib import json from django.utils.encoding import smart_str from django.views.decorators.csrf import csrf_exempt from django.http import HttpResponse
import xml.etree.ElementTree as ET import time import requests import json
access_token = "" expire_time = 0 app_id = 'wxb523466a6324ff01' secret = '90b355162a8bd1e25dff55eec8cd47c4'
token = 'C0GtrfPTCfY7cBCn94rj'
def get_access_token(): global access_token, expire_time now = time.time() if expire_time <= now: ans = requests.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}".format( app_id, secret)).text ans = json.loads(ans) access_token = ans["access_token"] expire_time = ans["expires_in"] + now return access_token
def send_NLP(text, user): url = 'https://api.weixin.qq.com/semantic/semproxy/search?access_token={}'.format(get_access_token()) data = { "query": text, "city": "北京", "category": "flight,hotel", "appid": app_id, "uid": '' } ans = requests.post(url, json.dumps(data, ensure_ascii=False).encode()) print(user, type(user)) return ans.text
def send_text(toUserName, fromUserName, content): XmlForm = """ <xml> <ToUserName><![CDATA[{ToUserName}]]></ToUserName> <FromUserName><![CDATA[{FromUserName}]]></FromUserName> <CreateTime>{CreateTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{Content}]]></Content> </xml> """.format(ToUserName=toUserName, FromUserName=fromUserName, CreateTime=int(time.time()), Content=content) return HttpResponse(XmlForm)
def send_img(toUserName, fromUserName, imageID): XmlForm = """ <xml> <ToUserName><![CDATA[{ToUserName}]]></ToUserName> <FromUserName><![CDATA[{FromUserName}]]></FromUserName> <CreateTime>{CreateTime}</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[{Content}]]></MediaId> </Image> </xml> """.format(ToUserName=toUserName, FromUserName=fromUserName, CreateTime=int(time.time()), Content=imageID) return HttpResponse(XmlForm)
@csrf_exempt def weixin_main(request): get_access_token() if request.method == "GET": signature = request.GET.get('signature', None) timestamp = request.GET.get('timestamp', None) nonce = request.GET.get('nonce', None) echostr = request.GET.get('echostr', None) hashlist = [token, timestamp, nonce] hashlist.sort() hashstr = ''.join([s for s in hashlist]) hashstr = hashlib.sha1(bytes(hashstr, encoding='utf-8')).hexdigest() if hashstr == signature: print("OK") return HttpResponse(echostr) else: print("HHH") return HttpResponse("field") else: othercontent = autoreply(request) return HttpResponse(othercontent)
def autoreply(request): webData = request.body xmlData = ET.fromstring(webData)
msg_type = xmlData.find('MsgType').text ToUserName = xmlData.find('ToUserName').text FromUserName = xmlData.find('FromUserName').text CreateTime = xmlData.find('CreateTime').text MsgType = xmlData.find('MsgType').text MsgId = xmlData.find('MsgId').text
toUser = FromUserName fromUser = ToUserName
if msg_type == 'text': received_content = xmlData.find('Content').text content = received_content if content.startswith("查一下"): return send_text(toUser, fromUser, send_NLP(content, toUser)) content = content.replace("?", "!") content = content.replace("?", "!") content = content.replace("吗", "") return send_text(toUser, fromUser, content) elif msg_type == 'image': mediaId = xmlData.find('MediaId').text return send_img(toUser, fromUser, mediaId) elif msg_type == 'voice': content = "语音已收到,谢谢" return send_text(toUser, fromUser, content) elif msg_type == 'video': content = "视频已收到,谢谢" return send_text(toUser, fromUser, content) elif msg_type == 'shortvideo': content = "小视频已收到,谢谢" return send_text(toUser, fromUser, content) elif msg_type == 'location': content = "位置已收到,谢谢" return send_text(toUser, fromUser, content) else: msg_type == 'link' content = "链接已收到,谢谢" return send_text(toUser, fromUser, content)
|