From d956a70d8769a42253b7c15485093afa3611830c Mon Sep 17 00:00:00 2001 From: Joseph Pan Date: Sun, 24 Dec 2017 17:52:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BE=AE=E4=BF=A1=E5=A5=BD?= =?UTF-8?q?=E5=8F=8B=E4=BD=BF=E7=94=A8echo=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/WechatBot.py | 77 +++++++++++++++++++++++++++++++++++++++++++++ dingdang.py | 64 ++----------------------------------- 2 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 client/WechatBot.py diff --git a/client/WechatBot.py b/client/WechatBot.py new file mode 100644 index 0000000..eae21a5 --- /dev/null +++ b/client/WechatBot.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8-*- + +import time +import os +from client.wxbot import WXBot + +from client import dingdangpath +from client.tts import SimpleMp3Player +from client.audio_utils import mp3_to_wav + + +class WechatBot(WXBot): + def __init__(self, brain): + WXBot.__init__(self) + self.brain = brain + self.music_mode = None + self.last = time.time() + + def handle_music_mode(self, msg_data): + # avoid repeating command + now = time.time() + if (now - self.last) > 0.5: + # stop passive listening + self.brain.mic.stopPassiveListen() + self.last = now + if not self.music_mode.delegating: + self.music_mode.delegating = True + self.music_mode.delegateInput(msg_data, True) + if self.music_mode is not None: + self.music_mode.delegating = False + + def handle_msg_all(self, msg): + # ignore the msg when handling plugins + profile = self.brain.profile + if (msg['msg_type_id'] == 1 and + msg['to_user_id'] == self.my_account['UserName']): + from_user = profile['first_name'] + '说:' + if msg['content']['type'] == 0: + msg_data = from_user + msg['content']['data'] + if msg_data.startswith(profile['robot_name_cn']+": "): + return + if self.music_mode is not None: + return self.handle_music_mode(msg_data) + self.brain.query([msg_data], self, True) + elif msg['content']['type'] == 4: + mp3_file = os.path.join(dingdangpath.TEMP_PATH, + 'voice_%s.mp3' % msg['msg_id']) + # echo or command? + if 'wechat_echo' in profile and not profile['wechat_echo']: + # 执行命令 + mic = self.brain.mic + wav_file = mp3_to_wav(mp3_file) + with open(wav_file) as f: + command = mic.active_stt_engine.transcribe(f) + if command: + if self.music_mode is not None: + return self.handle_music_mode(msg_data) + self.brain.query(command, self, True) + else: + mic.say("什么?") + else: + # 播放语音 + player = SimpleMp3Player() + player.play_mp3(mp3_file) + elif msg['msg_type_id'] == 4: + if msg['user']['name'] in profile['wechat_echo_text_friends'] and \ + msg['content']['type'] == 0: + from_user = msg['user']['name'] + '说:' + msg_data = from_user + msg['content']['data'] + self.brain.query([msg_data], self, True) + elif msg['user']['name'] in profile['wechat_echo_voice_friends'] \ + and msg['content']['type'] == 4: + mp3_file = os.path.join(dingdangpath.TEMP_PATH, + 'voice_%s.mp3' % msg['msg_id']) + player = SimpleMp3Player() + player.play_mp3(mp3_file) diff --git a/dingdang.py b/dingdang.py index 602902e..0522961 100755 --- a/dingdang.py +++ b/dingdang.py @@ -4,7 +4,6 @@ import os import sys import logging -import time import yaml import argparse import threading @@ -12,11 +11,8 @@ from client import stt from client import dingdangpath from client import diagnose -from client.wxbot import WXBot +from client import WechatBot from client.conversation import Conversation -from client.tts import SimpleMp3Player - -from client.audio_utils import mp3_to_wav # Add dingdangpath.LIB_PATH to sys.path sys.path.append(dingdangpath.LIB_PATH) @@ -40,62 +36,6 @@ from client.mic import Mic -class WechatBot(WXBot): - def __init__(self, brain): - WXBot.__init__(self) - self.brain = brain - self.music_mode = None - self.last = time.time() - - def handle_music_mode(self, msg_data): - # avoid repeating command - now = time.time() - if (now - self.last) > 0.5: - # stop passive listening - self.brain.mic.stopPassiveListen() - self.last = now - if not self.music_mode.delegating: - self.music_mode.delegating = True - self.music_mode.delegateInput(msg_data, True) - if self.music_mode is not None: - self.music_mode.delegating = False - - def handle_msg_all(self, msg): - # ignore the msg when handling plugins - if msg['msg_type_id'] == 1 and \ - msg['to_user_id'] == self.my_account['UserName']: - profile = self.brain.profile - # reply to self - if msg['content']['type'] == 0: - msg_data = msg['content']['data'] - print msg_data - if msg_data.startswith(profile['robot_name_cn']+": "): - return - if self.music_mode is not None: - return self.handle_music_mode(msg_data) - self.brain.query([msg_data], self, True) - elif msg['content']['type'] == 4: - mp3_file = os.path.join(dingdangpath.TEMP_PATH, - 'voice_%s.mp3' % msg['msg_id']) - # echo or command? - if 'wechat_echo' in profile and not profile['wechat_echo']: - # 执行命令 - mic = self.brain.mic - wav_file = mp3_to_wav(mp3_file) - with open(wav_file) as f: - command = mic.active_stt_engine.transcribe(f) - if command: - if self.music_mode is not None: - return self.handle_music_mode(msg_data) - self.brain.query(command, self, True) - else: - mic.say("什么?", cache=True) - else: - # 播放语音 - player = SimpleMp3Player() - player.play_mp3(mp3_file) - - class Dingdang(object): def __init__(self): self._logger = logging.getLogger(__name__) @@ -173,7 +113,7 @@ def run(self): # create wechat robot if self.config['wechat']: - self.wxBot = WechatBot(conversation.brain) + self.wxBot = WechatBot.WechatBot(conversation.brain) self.wxBot.DEBUG = True self.wxBot.conf['qr'] = 'tty' conversation.wxbot = self.wxBot