本文整理汇总了Python中wechat_sdk.WechatBasic.response_text方法的典型用法代码示例。如果您正苦于以下问题:Python WechatBasic.response_text方法的具体用法?Python WechatBasic.response_text怎么用?Python WechatBasic.response_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wechat_sdk.WechatBasic
的用法示例。
在下文中一共展示了WechatBasic.response_text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def post(self):
# 实例化 wechat
wechat = WechatBasic(token=token)
signature = self.get_argument("signature", None)
timestamp = self.get_argument("timestamp", None)
nonce = self.get_argument("nonce", None)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
# look request method
# print dir(self.request)
content = self.request.body
wechat.parse_data(content)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'''
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?
appid=wxafac6b4bc457eb26&redirect_uri=http://tuteng.info/login_access&
response_type=code&scope=snsapi_userinfo&state=14#wechat_redirect">testtt</a>
''')
else:
response = wechat.response_text(u'world')
elif message.type == 'image':
response = wechat.response_text(u'image')
else:
response = wechat.response_text(u'no image')
示例2: weixin
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def weixin(request):
wechat = WechatBasic(token=token)
if wechat.check_signature(signature=request.GET['signature'],
timestamp=request.GET['timestamp'],
nonce=request.GET['nonce']):
if request.method == 'GET':
rsp = request.GET.get('echostr', 'error')
else:
wechat.parse_data(request.body)
message = wechat.get_message()
# write_log(message.type)
if message.type == u'text':
response_content = 'this is text'
# response_content = handle_text(message.content)
if message.content == u'wechat':
# response_content = 'hello wechat'
rsp = wechat.response_text(u'^_^')
elif re.findall(u'锐捷', message.content):
rsp = wechat.response_news(ruijie_response)
else:
rsp = wechat.response_news(default_response)
else:
# rsp = wechat.response_text(u'消息类型: {}'.format(message.type))
rsp = wechat.response_news(default_response)
else:
rsp = wechat.response_text('check error')
return HttpResponse(rsp)
示例3: weixin_response
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def weixin_response(token, signature, timestamp, nonce, body_text=''):
debug_log('check sign ...\n')
# 用户的请求内容 (Request 中的 Body)
# 请更改 body_text 的内容来测试下面代码的执行情况
# 实例化 wechat
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature, timestamp, nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'^_^')
else:
response = wechat.response_text(u'文字')
elif message.type == 'image':
response = wechat.response_text(u'图片')
else:
response = wechat.response_text(u'未知')
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
return response
return HttpResponse('')
示例4: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def index(request):
# 实例化 We_chat_Basic
we_chat = WechatBasic(token=WECHAT_TOKEN, appid=AppID, appsecret=AppSecret)
if request.method == "GET":
signature = request.GET.get("signature")
timestamp = request.GET.get("timestamp")
nonce = request.GET.get("nonce")
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
else:
return HttpResponse(request.GET.get("echostr"), content_type="text/plain")
else:
signature = request.GET.get("signature")
timestamp = request.GET.get("timestamp")
nonce = request.GET.get("nonce")
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
try:
we_chat.parse_data(data=request.body)
except ParseError:
return HttpResponseBadRequest("Invalid XML Data")
message = we_chat.get_message()
if isinstance(message, EventMessage):
if message.type == "click":
if message.key == "STEP_COUNT":
step_array = Record.objects.filter(user=message.source)
if step_array:
step = step_array[len(step_array) - 1].step
response = we_chat.response_text("跑了" + str(step) + "步咯")
# 里面的数字应由其他函数获取
return HttpResponse(response)
else:
response = we_chat.response_text("Sorry, there' no data about you in our database.")
return HttpResponse(response)
if message.key == "CHART":
print 1
response = we_chat.response_news(
[
{
"title": "Today's amount of exercise",
"description": "data analysis",
"picurl": "http://7xn2s5.com1.z0.glb.clouddn.com/ez.png",
"url": SERVER_IP + "TodayChart/" + message.source,
}
]
)
return HttpResponse(response)
response = we_chat.response_text("Cheer up!")
return HttpResponse(response)
示例5: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def index(request):
# 实例化 We_chat_Basic
we_chat = WechatBasic(
token=WECHAT_TOKEN,
appid=AppID,
appsecret=AppSecret
)
if request.method == "GET":
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
else:
return HttpResponse(request.GET.get("echostr"), content_type="text/plain")
else:
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
try:
we_chat.parse_data(data=request.body)
except ParseError:
return HttpResponseBadRequest('Invalid XML Data')
message = we_chat.get_message()
if isinstance(message, EventMessage):
if message.type == 'click':
if message.key == 'STEP_COUNT':
step_array = Record.objects.filter(user=message.source)
if step_array:
step = step_array[len(step_array) - 1].step
response = we_chat.response_text(u'跑了' + str(step) + u'步咯')
# 里面的数字应由其他函数获取
return HttpResponse(response)
else:
response = we_chat.response_text(u'Sorry, there\' no data about you in our database.')
return HttpResponse(response)
if message.key == 'CHART':
print 1
response = we_chat.response_news([{
'title': u'Today\'s amount of exercise',
'description': 'data analysis',
'picurl': 'http://7xn2s5.com1.z0.glb.clouddn.com/ez.png',
'url': SERVER_IP + 'TodayChart/' + message.source}])
return HttpResponse(response)
response = we_chat.response_text(u'Cheer up!')
return HttpResponse(response)
示例6: wechatChatForFun
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def wechatChatForFun(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
wechat_instance = WechatBasic(conf=ChatForFunConf)
reply_text = ""
if not wechat_instance.check_signature(
signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponseBadRequest('Verify Failed')
else:
if request.method == 'GET':
response = request.GET.get('echostr', 'error')
else:
try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
if isinstance(message, TextMessage):
reply_text = str(message.content).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
elif isinstance(message, ImageMessage):
reply_text = str(message.picurl).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
elif isinstance(message, LocationMessage):
reply_text = str(message.label).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
else:
reply_text = U"服务器:目前只支持文字消息,非常抱歉"
response = wechat_instance.response_text(content=reply_text)
except ParseError:
return HttpResponseBadRequest("Invalid XML Data")
return HttpResponse(response, content_type='application/xml')
示例7: entrance
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def entrance(request):
def get_access_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+APP_ID+'&secret='+APP_SECRET
access_token = get_rsp_data(url)['access_token']
return access_token
def get_user_info():
access_token = get_access_token()
url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='+access_token+'&openid='+message.source
ueser_info_dic = get_rsp_data(url)
user_info = ""
for k, v in ueser_info_dic.items():
user_info = user_info+str(k)+":"+str(v)+"\n"
return user_info
def OAuth():
REDIRECT_URL = 'http://115.159.160.143/oauth'
url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+APP_ID+'&redirect_uri='+REDIRECT_URL+'&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect=0'
rsp = '<a href="'+url+'">点我授权</a>'
return rsp
wechat = WechatBasic(token=TOKEN)
if wechat.check_signature(signature=request.GET['signature'],
timestamp=request.GET['timestamp'],
nonce=request.GET['nonce']):
if request.method == 'GET':
rsp = request.GET.get('echostr', 'error')
else:
wechat.parse_data(request.body)
message = wechat.get_message()
rsp = wechat.response_text(OAuth())
else:
rsp = "failed"
return HttpResponse(rsp)
示例8: validate
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def validate():
signature = request.args.get('signature')
echostr = request.args.get('echostr')
timestamp = request.args.get('timestamp')
nonce = request.args.get('nonce')
token = "test"
body_text = request.get_data()
# 实例化 wechat
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'subscribe':
response = wechat.response_text("谢谢关注V2EX热帖分享,输入任意内容以获取最新热帖咨询")
if message.type == 'text':
newsList = generate()
response = wechat.response_news(newsList)
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
return response
示例9: api_wechat
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def api_wechat(request):
# 从 request 中提取基本信息 (signature, timestamp, nonce, xml)
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
xml = request.body
# 实例化 WechatBasic 并检验合法性
wechat_instance = WechatBasic(token='CCPL')
# if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# return HttpResponseBadRequest('Verify Failed')
# 解析本次请求的 XML 数据
try:
wechat_instance.parse_data(data=xml)
message = wechat_instance.get_message() # 获取解析好的微信请求信息
print message
except ParseError:
print xml
return HttpResponseBadRequest('Invalid XML Data')
ret = u'感谢关注“心理地图”!欢迎您登陆我们的网站:http://ccpl.psych.ac.cn/PsyMap/ (网站功能还在开发中,敬请期待。)' #message.type
response = wechat_instance.response_text(ret)
return HttpResponse(response)
示例10: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def index():
token = app.config['TOKEN']
signature = request.args.get('signature', '')
timestamp = request.args.get('timestamp', '')
nonce = request.args.get('nonce', '')
# 实例化 wechat
wechat = WechatBasic(token=token)
if not wechat.check_signature(signature=signature,
timestamp=timestamp, nonce=nonce):
return 'fail'
# 对签名进行校验
echostr = request.args.get('echostr')
if echostr:
return echostr
wechat.parse_data(request.data)
message = wechat.get_message()
if message.type == 'text':
response = wechat.response_text(content=settings.auto_replay_text)
elif message.type == 'image':
response = wechat.response_text(u'图片')
elif isinstance(message, EventMessage):
if message.type == 'subscribe':
if message.key and message.ticket:
scene = message.key.startswith('qrscene_') and message.key[8:] or 'default'
else:
scene = 'default'
SubscribeEvent.create_event(message.source, scene, message.time)
response = wechat.response_text(content=settings.greetings)
elif message.type == 'unsubscribe':
UnsubscribeEvent.create_event(message.source, message.time)
# TODO
response = ''
elif message.type == 'scan':
# TODO
response = ''
elif message.type == 'location':
response = wechat.response_text(content=u'上报地理位置事件')
elif message.type == 'click':
content = settings.click_menu_text_mapper.get(message.key, u'未知')
response = wechat.response_text(content=content)
elif message.type == 'view':
response = wechat.response_text(content=u'自定义菜单跳转链接事件')
elif message.type == 'templatesendjobfinish':
response = wechat.response_text(content=u'模板消息事件')
else:
response = wechat.response_text(u'未知')
return response
示例11: wechat_home
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def wechat_home(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
wechat_instance = WechatBasic(conf=MyConf)
reply_text = ""
if not wechat_instance.check_signature(
signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponseBadRequest('Verify Failed')
else:
reply_text = "Test"
response = wechat_instance.response_text(content=reply_text)
return HttpResponse(response, content_type='application/xml')
示例12: weixin_test
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def weixin_test(request):
# 下面这些变量均假设已由 Request 中提取完毕
token = 'WECHAT_TOKEN' # 你的微信 Token
signature = 'f24649c76c3f3d81b23c033da95a7a30cb7629cc' # Request 中 GET 参数 signature
timestamp = '1406799650' # Request 中 GET 参数 timestamp
nonce = '1505845280' # Request 中 GET 参数 nonce
body_text = """
<xml>
<ToUserName><![CDATA[touser]]></ToUserName>
<FromUserName><![CDATA[fromuser]]></FromUserName>
<CreateTime>1405994593</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[锐捷]]></Content>
<MsgId>6038700799783131222</MsgId>
</xml>
"""
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'^_^')
else:
response = wechat.response_text(u'文字')
elif message.type == 'image':
response = wechat.response_text(u'图片')
else:
response = wechat.response_text(u'未知')
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
print response
return HttpResponse(response)
示例13: receive
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def receive():
wechat = WechatBasic(token=settings['wechat_app']['token'], appid=settings['wechat_app']['appID'],
appsecret=settings['wechat_app']['appsecret'])
data = request.get_data(as_text=True)
for k, v in request.headers:
print '%s : %s' % (k, v)
print data
wechat.parse_data(data)
message = wechat.get_message()
if message.type == 'text':
content = message.content
res = validator.extract(content)
if not res:
return wechat.response_text(HELP_MESSAGE)
# has track no in the text message
isadding = content.startswith(u'ZQQ')
if isadding:
nums = ','.join(map(lambda x: '"%s"' % x, res))
push_sock.send((u'{"track_no_list": [%s]}' % nums).encode('utf-8'))
response = wechat.response_text(u'%s\n已添加.' % (u'\n'.join(res),))
else:
track_no = res[0]
record = db.find_one(track_no)
if record['status'] == 0:
return wechat.response_text(status_message(record['status']))
# has track data for this track No.
latest = record['data'][-1]
track = latest['location'] + ', ' + latest['context']
track_time = latest['time']
response = wechat.response_text(TRACK_INFO_TMP.format(time=record['time'], track_info=track,
track_time=track_time))
return response
else:
return wechat.response_text(u'对不起,目前不支持非文本消息~ ^_^')
return 'success'
示例14: test_response_text
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def test_response_text(self):
wechat = WechatBasic()
wechat.parse_data(data=self.test_message)
response_xml_1 = wechat.response_text('test message')
response_xml_2 = wechat.response_text('测试文本')
response_xml_3 = wechat.response_text(u'测试文本')
response_xml_4 = wechat.response_text('<h1>你好</h1>')
response_xml_5 = wechat.response_text('<h1>你好</h1>', escape=True)
response_1 = xmltodict.parse(response_xml_1)
response_2 = xmltodict.parse(response_xml_2)
response_3 = xmltodict.parse(response_xml_3)
response_4 = xmltodict.parse(response_xml_4)
response_5 = xmltodict.parse(response_xml_5)
self.assertEqual(response_1['xml']['ToUserName'], 'fromUser')
self.assertEqual(response_1['xml']['FromUserName'], 'toUser')
self.assertEqual(response_1['xml']['MsgType'], 'text')
self.assertEqual(response_1['xml']['Content'], 'test message')
self.assertEqual(response_2['xml']['Content'], '测试文本')
self.assertEqual(response_3['xml']['Content'], '测试文本')
self.assertEqual(response_4['xml']['Content'], '<h1>你好</h1>')
self.assertEqual(response_5['xml']['Content'], '<h1>你好</h1>')
示例15: WeChat
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import response_text [as 别名]
def WeChat(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
# 实例化 WechatBasic 类
wechat_instance = WechatBasic(conf=conf)
# 验证微信公众平台的消息
if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponseBadRequest('Verify Failed')
else:
if request.method == 'GET':
response = request.GET.get('echostr', 'error')
else:
try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
# 判断消息类型
if isinstance(message, TextMessage):
reply_text = 'text'
elif isinstance(message, VoiceMessage):
reply_text = 'voice'
elif isinstance(message, ImageMessage):
reply_text = 'image'
elif isinstance(message, LinkMessage):
reply_text = 'link'
elif isinstance(message, LocationMessage):
reply_text = 'location'
elif isinstance(message, VideoMessage):
reply_text = 'video'
elif isinstance(message, ShortVideoMessage):
reply_text = 'shortvideo'
else:
reply_text = 'other'
response = wechat_instance.response_text(content=reply_text)
except ParseError:
return HttpResponseBadRequest('Invalid XML Data')
return HttpResponse(response, content_type="application/xml")