-
-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add echo-encrypted example for test [ci skip]
- Loading branch information
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn app:app -b $VCAP_APP_HOST:$VCAP_APP_PORT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
import os | ||
from flask import Flask, request, abort, render_template | ||
from wechatpy.crypto import WeChatCrypto | ||
from wechatpy import parse_message, create_reply | ||
from wechatpy.utils import check_signature | ||
from wechatpy.exceptions import InvalidSignatureException | ||
from wechatpy.exceptions import InvalidAppIdException | ||
|
||
# set token or get from environments | ||
TOKEN = os.getenv('WECHAT_TOKEN', '123456') | ||
EncodingAESKey = os.getenv('WECHAT_ENCODING_AES_KEY', '') | ||
AppId = os.getenv('WECHAT_APP_ID', '') | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
host = request.url_root | ||
return render_template('index.html', host=host) | ||
|
||
|
||
@app.route('/wechat', methods=['GET', 'POST']) | ||
def wechat(): | ||
signature = request.args.get('signature', '') | ||
timestamp = request.args.get('timestamp', '') | ||
nonce = request.args.get('nonce', '') | ||
echo_str = request.args.get('echostr', '') | ||
encrypt_type = request.args.get('encrypt_type', '') | ||
msg_signature = request.args.get('msg_signature', '') | ||
|
||
print('signature:', signature) | ||
print('timestamp: ', timestamp) | ||
print('nonce:', nonce) | ||
print('echo_str:', echo_str) | ||
print('encrypt_type:', encrypt_type) | ||
print('msg_signature:', msg_signature) | ||
|
||
try: | ||
check_signature(TOKEN, signature, timestamp, nonce) | ||
except InvalidSignatureException: | ||
abort(403) | ||
if request.method == 'GET': | ||
return echo_str | ||
else: | ||
print('Raw message: \n%s' % request.data) | ||
crypto = WeChatCrypto(TOKEN, EncodingAESKey, AppId) | ||
try: | ||
msg = crypto.decrypt_message( | ||
request.data, | ||
msg_signature, | ||
timestamp, | ||
nonce | ||
) | ||
print('Descypted message: \n%s' % msg) | ||
except (InvalidSignatureException, InvalidAppIdException): | ||
abort(403) | ||
msg = parse_message(msg) | ||
if msg.type == 'text': | ||
reply = create_reply(msg.content, msg) | ||
else: | ||
reply = create_reply('Sorry, can not handle this for now', msg) | ||
return crypto.encrypt_message( | ||
reply.render(), | ||
nonce, | ||
timestamp | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run('127.0.0.1', 5001, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask | ||
wechatpy | ||
gunicorn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<html> | ||
<head> | ||
<title>Coding wechatpy Demo</title> | ||
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | ||
<style type="text/css"> | ||
body { | ||
font-family: "Microsoft YaHei",微软雅黑,"Microsoft JhengHei","Hiragino Sans GB",Verdana, Geneva, sans-serif; | ||
} | ||
|
||
.wrapper { | ||
width: 900px; | ||
margin: 0 auto; | ||
} | ||
|
||
p { | ||
margin-top: 20px; | ||
} | ||
|
||
a:link { | ||
text-decoration: none; | ||
color: #2d59a2; | ||
} | ||
|
||
a:visited { | ||
text-decoration: none; | ||
color: #2d59a2; | ||
} | ||
|
||
a:hover { | ||
text-decoration: none; | ||
color: #203f71; | ||
} | ||
|
||
a:active { | ||
text-decoration: none; | ||
color: #203f71; | ||
} | ||
|
||
#forkongithub a { | ||
background: #000; | ||
color: #fff; | ||
text-decoration: none; | ||
font-family: arial,sans-serif; | ||
text-align: center; | ||
font-weight: bold; | ||
padding: 5px 40px; | ||
font-size: 1rem; | ||
line-height: 2rem; | ||
position: relative; | ||
transition: 0.5s; | ||
} | ||
|
||
#forkongithub a:hover { | ||
background: #c11; | ||
color: #fff; | ||
} | ||
|
||
#forkongithub a::before,#forkongithub a::after { | ||
content: ""; | ||
width: 100%; | ||
display: block; | ||
position: absolute; | ||
top: 1px; | ||
left: 0; | ||
height: 1px; | ||
background: #fff; | ||
} | ||
|
||
#forkongithub a::after { | ||
bottom: 1px; | ||
top: auto; | ||
} | ||
|
||
@media screen and (min-width:800px) { | ||
#forkongithub { | ||
position: absolute; | ||
display: block; | ||
top: 0; | ||
right: 0; | ||
width: 200px; | ||
overflow: hidden; | ||
height: 200px; | ||
z-index: 9999; | ||
} | ||
|
||
#forkongithub a { | ||
width: 200px; | ||
position: absolute; | ||
top: 60px; | ||
right: -60px; | ||
transform: rotate(45deg); | ||
-webkit-transform: rotate(45deg); | ||
-ms-transform: rotate(45deg); | ||
-moz-transform: rotate(45deg); | ||
-o-transform: rotate(45deg); | ||
box-shadow: 4px 4px 10px rgba(0,0,0,0.8); | ||
} | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="wrapper"> | ||
<h1>wechatpy</h1> | ||
<p>这是 <a href="https://coding.net" target="_blank">Coding</a> 演示平台 <a href="https://coding.net/u/messense/p/wechatpy/git" target="_blank">wechatpy</a> 示例项目运行效果页面。</p> | ||
<p>微信公众号回调地址:{{ host }}wechat</p> | ||
<p>请在微信公共平台后台使用上述地址开启回调模式。</p> | ||
<div> | ||
<h2>演示平台环境变量配置</h2> | ||
<ul> | ||
<li><strong>WECHAT_TOKEN</strong>: 微信回调 token</li> | ||
<li><strong>WECHAT_ENCODING_AES_KEY</strong>: 微信回调加密用的 encoding_aes_key</li> | ||
<li><strong>WECHAT_APP_ID</strong>: 微信应用 ID</li> | ||
</ul> | ||
</div> | ||
<span id="forkongithub"><a href="https://coding.net/u/messense/p/wechatpy/git">Fork me on Coding</a></span> | ||
</div> | ||
</body> | ||
</html> |