コード例 #1
0
def unsubscribe_msg(request, msg):
    user_openid = msg.source
    wechat_users = WechatUser.objects.filter(openid=user_openid, stat='VALID')
    if not wechat_users:
        logger.warn('未保存的微信用户 [%s] 取消关注', user_openid)
    else:
        wechat_user = wechat_users[0]
        wechat_user.stat = 'UNFOLLOWED'
        wechat_user.save()
    retmsg = TextReply(message=msg, content='bye')
    return HttpResponse(retmsg.render())
コード例 #2
0
def subscribe_msg(request, msg):
    user_openid = msg.source
    wechat_get_user(user_openid)
    welcome_url = 'https://espush.cn/weixin/wechat_webapp/'
    quoted_welcome_url = get_wechat_redirect_url(welcome_url)
    content = """欢迎关注ESPUSH
ESPUSH 是专门为ESP8266 WIFI物联网而开发的云平台,针对云端远程控制、数据采集等应用场景做了深度优化。
<a href="%s">点这里手机控制您的WIFI</a>
有什么意见,您也可以直接微信与我沟通,收到您的反馈后我会尽快回复您。
    """ % quoted_welcome_url
    retmsg = TextReply(message=msg, content=content)
    return HttpResponse(retmsg.render())
コード例 #3
0
 def _text_msg_handler(self):
     # 文字消息处理逻辑
     event_key = 'text'
     content = self.data.get('Content')
     pm25_match = pm25.match(content)
     match = momo_learn.match(content)
     if match:
         # 教魔魔说话第一优先级
         conversation = match.groups()
         reply_content = ReplyContent('text', event_key)
         response = reply_content.set(conversation)
         self.reply_params['content'] = response
     elif pm25_match:
         # pm2.5 查询第二优先级
         city = pm25_match.groupdict().get('city')
         reply_content = ReplyContent('text', event_key)
         text = get_pm25(city)
         self.reply_params['content'] = text
     else:
         # 再查询有没有特殊自动回复消息workflow
         to_user = self.reply_params['to_user']
         kwr = KWR(to_user, content)
         value = kwr.get_response()
         if not value:
             reply_content = ReplyContent('text', event_key, content)
             value = reply_content.value
         self.reply_params['content'] = value
     self.reply = TextReply(**self.reply_params).render()
コード例 #4
0
 def _image_msg_handler(self):
     media_id = self.data['MediaId']
     picurl = None
     if not picurl:
         picurl = self.data['PicUrl']
     is_succeed, media_key = media_fetch(picurl, media_id)
     qiniu_url = '{host}/{key}'.format(host=Config.QINIU_HOST, key=media_key)
     self.reply_params['content'] = qiniu_url
     self.reply = TextReply(**self.reply_params).render()
コード例 #5
0
 def _text_msg_handler(self):
     # 文字消息处理逻辑
     event_key = 'text'
     content = self.data.get('Content')
     pm25_match = pm25.match(content)
     learn_match = momo_learn.match(content)
     if learn_match:
         # 教魔魔说话第一优先级
         conversation = learn_match.groups()
         reply_content = ReplyContent('text', event_key)
         response = reply_content.set(conversation)
         self.reply_params['content'] = response
     elif pm25_match:
         # pm2.5 查询第二优先级
         city = pm25_match.groupdict().get('city')
         reply_content = ReplyContent('text', event_key)
         text = get_pm25(city)
         self.reply_params['content'] = text
     elif content.startswith('note '):
         if content.startswith('note -u '):
             note = content[8:]
         else:
             note = content[5:]
         reply_content = ReplyContent('text', event_key)
         to_user = self.reply_params['to_user']
         from momo.note import Note, note_img_config
         filename = '%s_%s.png' % (to_user, int(time.time()))
         note_file = Note(note, filename, **note_img_config).draw_text()
         if content.startswith('note -u '):
             upload_file_to_qcos(note_file, filename)
             qiniu_url = '{host}/{key}'.format(host=Config.QCOS_HOST,
                                               key=filename)
             self.reply_params['content'] = qiniu_url
         else:
             access_token, _ = get_weixinmp_token(appid, secret)
             media_id = get_weixinmp_media_id(access_token, note_file)
             self.reply_params['media_id'] = media_id
             self.reply = ImageReply(**self.reply_params).render()
             return
     elif content == 'xmr_stats':
         text = get_xmr_stats()
         self.reply_params['content'] = text
     else:
         # 再查询有没有特殊自动回复消息workflow
         to_user = self.reply_params['to_user']
         kwr = KWR(to_user, content)
         value = kwr.get_response()
         if not value:
             reply_content = ReplyContent('text', event_key, content)
             value = reply_content.value
         if value.startswith('The current time is'):
             value = content
         self.reply_params['content'] = value
     self.reply = TextReply(**self.reply_params).render()
コード例 #6
0
 def _unsub_scan_event_handler(self):
     event_key = self.data.get('EventKey')[8:]
     content = ReplyContent('scan', event_key)
     if content.type == 'article':
         article_reply = ArticleReply(**self.reply_params)
         values = content.value
         for value in values:
             article = Article(**value)
             article_reply.add_article(article)
         self.reply = article_reply.render()
     elif content.type == 'text' or not content.type:
         self.reply_params['content'] = content.value
         self.reply = TextReply(**self.reply_params).render()
コード例 #7
0
 def _text_msg_handler(self):
     # 文字消息处理逻辑
     event_key = 'text'
     content = self.data.get('Content')
     match = momo_learn.match(content)
     if match:
         conversation = match.groups()
         reply_content = ReplyContent('text', event_key)
         response = reply_content.set(conversation)
         self.reply_params['content'] = response
     else:
         reply_content = ReplyContent('text', event_key, content)
         self.reply_params['content'] = reply_content.value
     self.reply = TextReply(**self.reply_params).render()
コード例 #8
0
def wechat(request):
    r1 = redis_client()
    WECHAT_TOKEN = r1.get('WECHAT_ESPUSH_TOKEN')
    if not WECHAT_TOKEN:
        logger.error('未配置TOKEN,失败')
        return HttpResponseServerError("服务端错误,未配置")
    signature = request.GET.get('signature')
    timestamp = request.GET.get('timestamp')
    nonce = request.GET.get('nonce')
    echostr = request.GET.get('echostr')
    if (not nonce) or (not timestamp) or (not signature):
        return HttpResponseBadRequest('ERROR')
    sOut = hashlib.sha1(''.join(sorted([WECHAT_TOKEN, timestamp,
                                        nonce]))).hexdigest()
    logger.info('signature: [%s], [%s]', signature, sOut)
    if sOut != signature:
        logger.warn('签名不正确,忽略')
        return HttpResponseBadRequest("ERROR")
    if request.method == 'GET':
        return HttpResponse(echostr)
    if request.method == 'POST':
        msg = parse_user_msg(request.body)
        logger.info(u'消息: [%s]', msg.type)
        if msg.type == 'subscribe':
            return subscribe_msg(request, msg)
        elif msg.type == 'unsubscribe':
            return unsubscribe_msg(request, msg)
        elif msg.type == 'view':
            logger.info('页面跳转事件,忽略')
            return HttpResponse("")
        elif msg.type == 'text':
            logger.info('用户直接沟通反馈')
            weuser = wechat_get_user(msg.source)
            feedback_msg = WechatFeedback(content=msg.content,
                                          wechat_user=weuser)
            feedback_msg.save()
            rsp_content = '小E已经收到您的呼唤,我会尽快给您回复'
            rsp = TextReply(message=msg, content=rsp_content)
            return HttpResponse(rsp.render())
        else:
            logger.warn('未知消息类型')
            content = '小E也不知道您在嘀咕个啥'
            rsp = TextReply(message=msg, content=content)
            return HttpResponse(rsp.render())
コード例 #9
0
 def _subscribe_event_handler(self):
     self.reply_params['content'] = self.auto_reply_content
     self.reply = TextReply(**self.reply_params).render()
コード例 #10
0
 def _subscribe_event_handler(self):
     content = ReplyContent('subscribe', 'subscribe')
     self.reply_params['content'] = content.value or self.auto_reply_content
     self.reply = TextReply(**self.reply_params).render()