-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
62 lines (53 loc) · 1.83 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
# -*- coding: utf-8 -*-
import os
import subprocess
import glob
import sys
import importlib
home_path = '/home/pi'
akane_path = os.path.join(home_path, 'akane-chan', 'main.py')
aques_path = os.path.join(home_path, 'speak_api/lib/aquestalkpi/AquesTalkPi')
class Bot(object):
def __init__(self):
self._cmd = ''
self._listeners = self._load_scripts()
@property
def cmd(self):
return self._cmd
@property
def listeners(self):
return self._listeners
@cmd.setter
def cmd(self, value):
self._cmd = value
def say(self, message):
self.stop_say()
# print('bot >: ' + message.encode('utf-8'))
voice_process = subprocess.Popen([aques_path, message.encode('utf-8')], stdout=subprocess.PIPE)
subprocess.Popen(['aplay'], stdin=voice_process.stdout)
# subprocess.Popen([python3_path, akane_path, message, "2.0", "1.4", "1.0", "1.0"])
def listen(self, cmd):
for l in self.listeners:
if not hasattr(l, "_command"):
continue
try:
if l._command == cmd:
self.cmd = cmd
l(self)
break
except Exception as e:
print(e)
def _load_scripts(self):
sys.path += ['scripts']
listeners = []
for f in glob.glob('scripts/*.py'):
module_name = os.path.splitext(os.path.basename(f))[0]
module = importlib.import_module('scripts.' + module_name)
for x in dir(module):
method = getattr(module, x)
if hasattr(method, "_hook"):
listeners.append(getattr(module, x))
return listeners
def stop_say(self):
killaudio_path = os.path.join(home_path, 'killaudio')
subprocess.Popen(['sh', killaudio_path])