def reply_msg_format(msg): ''' turns string reply to reply dict * if format failed, it will return a ReturnValue equals to False * if succeeded, it will return a dict equals to string ''' if isinstance(msg, dict): r = msg elif hasattr(msg, 'capitalize'): msgType, content = msg[:5], msg[5:] if not re.match('@[a-z]{3}@', msgType): content = msgType + content msgType = TEXT elif msgType[1:4] == 'msc': return ReturnValue({'errcode': -10003, 'errmsg': 'msg for type MUSIC should be: {"msgType": MUSIC, ' + '"musicurl" :MUSICURL, "hqmusicurl" :HQMUSICURL, ' + '"thumb_media_id": MEDIA_ID}'}) elif msgType[1:4] not in ('img', 'voc', 'vid', 'txt', 'nws', 'cad'): return ReturnValue({'errcode': -10003, 'errmsg': 'send supports: img, voc, vid, txt, nws, cad'}) else: msgType = {'img': IMAGE, 'voc': VOICE, 'vid': VIDEO, 'txt': TEXT, 'nws': NEWS, 'cad': CARD}[msgType[1:4]] r = {'msgType': msgType, 'mediaId': content} else: r = ReturnValue({'errcode': -10003, 'errmsg': 'msg should be string or dict'}) if r and r.get('msgType') == TEXT: if 'mediaId' not in r and 'content' in r: r['mediaId'] = r['content'] elif 'mediaId' in r and 'content' not in r: r['content'] = r['mediaId'] return r
def reply_msg_format(msg): ''' turns string reply to reply dict * if format failed, it will return a ReturnValue equals to False * if succeeded, it will return a dict equals to string * string content will be filled in MediaId key ''' if isinstance(msg, dict): r = msg elif hasattr(msg, 'capitalize'): msgType, content = msg[1:4], msg[5:] r = {} if not re.match('@[a-z]{3}@', msg[:5]): r['MsgType'] = TEXT r['MediaId'] = msg elif msgType == 'msc': return ReturnValue({ 'errcode': -10003, 'errmsg': 'msg for type MUSIC should be: {"MsgType": MUSIC, ' + '"musicurl" :MUSICURL, "hqmusicurl" :HQMUSICURL, ' + '"thumb_media_id": MEDIA_ID}' }) elif msgType not in ('img', 'voc', 'vid', 'txt', 'nws', 'cad'): return ReturnValue({ 'errcode': -10003, 'errmsg': 'send supports: img, voc, vid, txt, nws, cad' }) elif msgType == 'txt': r['MsgType'] = TEXT r['MediaId'] = content elif msgType in ('nws', 'cad'): r['MsgType'] = NEWS if msgType == 'nws' else CARD try: r.update(json.loads(content)) except: logger.warning('content of %s is not a valid json' % r['MsgType']) else: r['MsgType'] = {'img': IMAGE, 'voc': VOICE, 'vid': VIDEO}[msgType] if os.path.isfile(content): r['FileDir'] = content else: r['MediaId'] = content else: r = ReturnValue({ 'errcode': -10003, 'errmsg': 'msg should be string or dict' }) if 'Content' in r and not 'MediaId' in r and r.get('MsgType') == TEXT: r['MediaId'] = r['Content'] return r
def reply_msg_format(msg): ''' turns string reply to reply dict * if format failed, it will return a ReturnValue equals to False * if succeeded, it will return a dict equals to string * string content will be filled in MediaId key ''' if isinstance(msg, dict): r = msg elif hasattr(msg, 'capitalize'): msgType, content = msg[1:4], msg[5:] r = {} if not re.match('@[a-z]{3}@', msg[:5]): r['MsgType'] = TEXT r['MediaId'] = msg elif msgType == 'msc': return ReturnValue({'errcode': -10003, 'errmsg': 'msg for type MUSIC should be: {"MsgType": MUSIC, ' + '"musicurl" :MUSICURL, "hqmusicurl" :HQMUSICURL, ' + '"thumb_media_id": MEDIA_ID}'}) elif msgType not in ('img', 'voc', 'vid', 'txt', 'nws', 'cad'): return ReturnValue({'errcode': -10003, 'errmsg': 'send supports: img, voc, vid, txt, nws, cad'}) elif msgType == 'txt': r['MsgType'] = TEXT r['MediaId'] = content elif msgType in ('nws', 'cad'): r['MsgType'] = NEWS if msgType == 'nws' else CARD try: r.update(json.loads(content)) except: logger.warning('content of %s is not a valid json' % r['MsgType']) else: r['MsgType'] = {'img': IMAGE, 'voc': VOICE, 'vid': VIDEO}[msgType] if os.path.isfile(content): r['FileDir'] = content else: r['MediaId'] = content else: r = ReturnValue({'errcode': -10003, 'errmsg': 'msg should be string or dict'}) if 'Content' in r and not 'MediaId' in r and r.get('MsgType') == TEXT: r['MediaId'] = r['Content'] return r