mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 22:27:13 +08:00
wechatmp_service: image and voice reply supported
This commit is contained in:
@@ -10,7 +10,7 @@ from bridge.reply import Reply, ReplyType
|
||||
from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
|
||||
import os
|
||||
|
||||
class PyttsVoice(Voice):
|
||||
engine = pyttsx3.init()
|
||||
@@ -24,15 +24,23 @@ class PyttsVoice(Voice):
|
||||
if "Chinese" in voice.name:
|
||||
self.engine.setProperty("voice", voice.id)
|
||||
|
||||
self.engine.setProperty("voice", "zh")
|
||||
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
wavFile = TmpDir().path() + "reply-" + str(int(time.time())) + ".wav"
|
||||
self.engine.save_to_file(text, wavFile)
|
||||
mp3FileName = "reply-" + str(int(time.time())) + ".mp3"
|
||||
mp3File = TmpDir().path() + mp3FileName
|
||||
self.engine.save_to_file(text, mp3File)
|
||||
self.engine.runAndWait()
|
||||
|
||||
# engine.runAndWait() will return before the file created
|
||||
while mp3FileName not in os.listdir(TmpDir().path()):
|
||||
time.sleep(0.1)
|
||||
|
||||
logger.info(
|
||||
"[Pytts] textToVoice text={} voice file name={}".format(text, wavFile)
|
||||
"[Pytts] textToVoice text={} voice file name={}".format(text, mp3File)
|
||||
)
|
||||
reply = Reply(ReplyType.VOICE, wavFile)
|
||||
reply = Reply(ReplyType.VOICE, mp3File)
|
||||
except Exception as e:
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user