This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
bot.py
85 lines (77 loc) · 2.48 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
import itchat
import os
import requests
import logging
import time
import yaml
import json
import base64
from pydub import AudioSegment
from itchat.content import *
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
config = {}
with open('config.yml', 'r') as f:
config = yaml.safe_load(f)
if any (key not in config for key in ['host', 'port', 'validate']):
logging.error("配置文件格式有误,程序将退出!")
exit(1)
url = '{}:{}/chat'.format(config['host'], config['port'])
def convert_mp3_to_wav(mp3_path):
"""
将 mp3 文件转成 wav
:param mp3_path: mp3 文件路径
:returns: wav 文件路径
"""
target = mp3_path.replace(".mp3", ".wav")
if not os.path.exists(mp3_path):
logging.critical("文件错误 {}".format(mp3_path))
return None
AudioSegment.from_mp3(mp3_path).export(target, format="wav")
return target
@itchat.msg_register([RECORDING])
def download_files(msg):
if msg.toUserName == 'filehelper':
logging.info('received voice {}'.format(msg))
msg.download(msg.fileName)
wav = convert_mp3_to_wav(msg.fileName)
with open(wav, 'rb') as f:
data = base64.b64encode(f.read())
param = {
'validate': config['validate'],
'type': 'voice',
'voice': data,
'uuid': str(int(time.time()))
}
r = requests.post(url, data=param)
r.encoding = 'utf-8'
try:
resp = r.json()['resp']
msg.user.send('wukong: %s' % (resp))
except Exception as e:
logging.error(e)
finally:
if os.path.exists(msg.fileName):
os.remove(msg.fileName)
if os.path.exists(wav):
os.remove(wav)
@itchat.msg_register(TEXT)
def text_reply(msg):
if msg.toUserName == 'filehelper' and not msg.text.startswith('wukong: '):
logging.info('received text {}'.format(msg.text))
param = {
'validate': config['validate'],
'type': 'text',
'query': msg.text,
'uuid': str(int(time.time()))
}
r = requests.post(url, data=param)
r.encoding = 'utf-8'
try:
resp = r.json()['resp']
msg.user.send('wukong: %s' % (resp))
except Exception as e:
logging.error(e)
itchat.auto_login(enableCmdQR=2, hotReload=True)
itchat.run(True)