Beispiel #1
0
def map_reply(msg):
    if '小冰' in str(msg):
        text = getText(msg)
        print('正常:%s' % text)
        global userId
        if not replied.get(md5(userId + '_MSG')):
            replied[md5(userId + '_MSG')] = 1
            if msg['Type'] == 'Picture':
                # msg['Text'](msg['FileName'])
                msg['Text']('./images/' + msg['FileName'])
                res_path = './images/' + msg['FileName']
                itchat.send_msg("[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.] ",
                                userId)
                itchat.send_image(res_path, userId)
            elif msg['Type'] == 'Recording':
                try:
                    wav2text.transcode('./records/' + msg['FileName'])
                    filename = msg['FileName'].replace('mp3', 'wav')
                    text = wav2text.wav_to_text('./records/' + filename)
                    itchat.send_msg(
                        "[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.] " + text,
                        userId)
                except Exception as e:
                    print('转换期间出错,错误信息:%s.回复默认表情' % e)
            else:
                itchat.send_msg(
                    "[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.]  " + text, userId)
        elif replied[md5(userId + '_MSG')] < MAX_LIMIT_MSG:
            replied[md5(userId + '_MSG')] += 1
            if msg['Type'] == 'Picture':
                # msg['Text'](msg['FileName'])
                msg['Text']('./images/' + msg['FileName'])
                res_path = './images/' + msg['FileName']
                itchat.send_msg("[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.] ",
                                userId)
                itchat.send_image(res_path, userId)
            elif msg['Type'] == 'Recording':
                try:
                    wav2text.transcode('./records/' + msg['FileName'])
                    filename = msg['FileName'].replace('mp3', 'wav')
                    text = wav2text.wav_to_text('./records/' + filename)
                    itchat.send_msg(
                        "[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.] " + text,
                        userId)
                except Exception as e:
                    print('转换期间出错,错误信息:%s.回复默认表情' % e)
            else:
                itchat.send_msg(
                    "[陈主人不在.暂由助理Neo开始回复你.有事请留言.主人看到后会及时回复你.]  " + text, userId)
        elif replied[md5(userId + '_MSG')] > MAX_LIMIT_MSG:
            pass
        else:
            replied[md5(userId + '_MSG')] += 1
            itchat.send_msg(u"[陈主人不让我欺负你.So,have a nice day & See you!]",
                            userId)
Beispiel #2
0
def rec_reply(msg):
    # 是否开启语音识别,需要安装ffmpeg和pydub
    msg['Text']('./records/' + msg['FileName'])

    from beta import wav2text, textToMp3
    wav2text.transcode('./records/' + msg['FileName'])
    filename = msg['FileName'].replace('mp3', 'wav')
    text = wav2text.wav_to_text('./records/' + filename)
    text = text.replace(",", "")
    print(text)
    payload = {"user_input": text}
    intent, entity = extract_entities(text)
    if (intent == "phone_number"):
        phone_number = entity
        response = "好的,您的电话是" + str(phone_number) + "," + "那您的安装地址哪里呢?"
        filename2 = textToMp3.textToMp3(response)

        reply = itchat.send_file('D:/tomcat/wechat_bot-master/auido.mp3',
                                 msg['FromUserName'])
    #print(phone_number)
    #itchat.send("好的,您的电话是" + str(phone_number), msg['FromUserName'])
    if (intent == "greet"):
        response = "您好,感谢您访问夏普公司,很高兴为您服务。"
        filename2 = textToMp3.textToMp3(response)

        reply = itchat.send_file('D:/tomcat/wechat_bot-master/auido.mp3',
                                 msg['FromUserName'])
        print(reply['BaseResponse']['ErrMsg'])
        #itchat.send("您好,感谢您访问夏普公司,很高兴为您服务。", msg['FromUserName'])
    if (intent == "address"):
        address = findAddress(text)
        response = "您的地址是" + str(address)
        filename2 = textToMp3.textToMp3(response)
        print(reply['BaseResponse']['ErrMsg'])
        reply = itchat.send_file('D:/tomcat/wechat_bot-master/auido.mp3',
                                 msg['FromUserName'])
    if (intent == "null"):  #处理地址intent识别错误就用hanlp
        #address = findAddress(text)
        #response = "您的地址是" + str(address)
        #filename2 = textToMp3.textToMp3(response)

        #reply = itchat.send_file('D:/tomcat/wechat_bot-master/auido.mp3', msg['FromUserName'])
        #print(reply['BaseResponse']['ErrMsg'])
        response = requests.get(bot_api, params=payload).json()["response"]
        filename2 = textToMp3.textToMp3(response)
        print(response)
        reply = itchat.send_file('D:/tomcat/wechat_bot-master/auido.mp3',
                                 msg['FromUserName'])
        print(reply['BaseResponse']['ErrMsg'])
Beispiel #3
0
def rec_reply(msg):
    print('我发送的语音类型', msg['Type'])
    global userId
    userId = msg['FromUserName']
    xb = itchat.search_mps(name='小冰')[0]
    # 是否开启语音识别,需要安装ffmpeg和pydub
    enable_voice_rec = True
    # enable_voice_rec = False
    msg['Text']('./records/' + msg['FileName'])
    User = itchat.search_friends(userName=msg['FromUserName'])
    if User['RemarkName'] == '':
        NickName = User['NickName']
    else:
        NickName = '%s(%s)' % (User['NickName'], User['RemarkName'])

    if enable_voice_rec:
        msg['Text']('./records/' + msg['FileName'])
        # from beta import wav2text
        try:
            wav2text.transcode('./records/' + msg['FileName'])
            filename = msg['FileName'].replace('mp3', 'wav')
            text = wav2text.wav_to_text('./records/' + filename)
            itchat.send_msg(text, xb['UserName'])
        except Exception as e:
            print('转换期间出错,错误信息:%s.回复默认表情' % e)
            return '@img@./records/default.gif'
        # reply = talk(text, md5(msg['FromUserName']))
        print(
            '------------------------------------------------------------------------------'
        )
        print('%s给您发送了一条语音,已经存入records目录,文件名:%s' % (NickName, msg['FileName']))
        print('智能识别该消息内容为:%s' % text)
        # print('AI帮您回复%s:%s' % (NickName, reply))
        print(
            '------------------------------------------------------------------------------'
        )
        # return reply
    else:
        print(
            '------------------------------------------------------------------------------'
        )
        print('%s给您发送了一条语音,已经存入records目录,文件名:%s' % (NickName, msg['FileName']))
        print('AI帮您回复%s默认表情default.gif' % NickName)
        print(
            '------------------------------------------------------------------------------'
        )
        return '@img@./records/default.gif'
Beispiel #4
0
def rec_reply(msg):
    # 是否开启语音识别,需要安装ffmpeg和pydub
    enable_voice_rec = False
    msg['Text']('./records/' + msg['FileName'])
    User = itchat.search_friends(userName=msg['FromUserName'])
    if User['RemarkName'] == '':
        NickName = User['NickName']
    else:
        NickName = '%s(%s)' % (User['NickName'], User['RemarkName'])

    if enable_voice_rec:
        msg['Text']('./records/' + msg['FileName'])
        from beta import wav2text
        wav2text.transcode('./records/' + msg['FileName'])
        filename = msg['FileName'].replace('mp3', 'wav')
        text = wav2text.wav_to_text('./records/' + filename)
        reply = talk(text, md5(msg['FromUserName']))
        print(
            '------------------------------------------------------------------------------'
        )
        print('%s给您发送了一条语音,已经存入records目录,文件名:%s' % (NickName, msg['FileName']))
        print('智能识别该消息内容为:%s' % text)
        print('AI帮您回复%s:%s' % (NickName, reply))
        print(
            '------------------------------------------------------------------------------'
        )
        return reply
    else:
        print(
            '------------------------------------------------------------------------------'
        )
        print('%s给您发送了一条语音,已经存入records目录,文件名:%s' % (NickName, msg['FileName']))
        print('AI帮您回复%s默认表情default.gif' % NickName)
        print(
            '------------------------------------------------------------------------------'
        )
        return '@img@./records/default.gif'