WeChat 玩 Google Assistant
2017/8/8大约 1 分钟约 307 字
How to
很简单,用Google的工具**googlesamples-assistant-pushtotalk**把 微信的音频文件作为输入 然后用 ItChat 把输出发回给微信。代码不多,但环境配置挺麻烦的,其中有些小坑。
DO
环境配置(Ubuntu)
requirement.txt
cachetools
certifi
cffi
chardet
click
enum34
google-assistant-grpc
google-assistant-library
google-auth
google-auth-oauthlib
googleapis-common-protos
grpcio
idna
itchat
monotonic
oauthlib
protobuf
pyasn1
pyasn1-modules
pycparser
pydub
pypng
PyQRCode
requests
requests-oauthlib
rsa
six
sounddevice
tenacity
urllib3代码
import os
import itchat
import time
from pydub import AudioSegment
from itchat.content import *
# Google Assistant API 处理音频
@itchat.msg_register([RECORDING], isFriendChat=True)
def reply(msg):
if msg['Type'] == 'Recording':
filename = msg['FileName']
# 下载音频文件
msg['Text'](msg['FileName'])
os.popen("mv %s audio.mp3" % filename)
print("INFO:Audio file %s received" % filename)
time.sleep(2)
print("INFO:Start to process audio")
request = AudioSegment.from_mp3('audio.mp3')
# 音频要做调整,否则无法识别
request = request.apply_gain(+17.0)
request = request.set_frame_rate(16000)
request.export("audio.wav",format="wav",bitrate="160k")
# 用pushtotalk.py处理 输出在output.wav
os.popen("python pushtotalk.py -v -o output.wav -i audio.wav ").read()
response = AudioSegment.from_wav('output.wav')
response.export("Google Assistant.mp3",format="mp3",bitrate="160k")
# 发送文件
itchat.send_file("Google Assistant.mp3",toUserName='filehelper')
itchat.auto_login(hotReload=True)
itchat.send("Google Assistant in WeChat",toUserName='filehelper')
itchat.run()