-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
289 lines (258 loc) · 12.4 KB
/
__init__.py
File metadata and controls
289 lines (258 loc) · 12.4 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2025 Neongecko.com Inc.
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds,
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo
# BSD-3 License
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import shutil
import sys
import yaml
from copy import deepcopy
from datetime import datetime
from glob import glob
from os.path import join, basename, getsize, isfile
from subprocess import run
from tempfile import mkdtemp
from ovos_bus_client import Message
from neon_utils.user_utils import get_user_prefs
from neon_utils.skills.neon_skill import NeonSkill
from neon_utils.net_utils import get_ip_address
from neon_utils.file_utils import encode_file_to_base64_string
from neon_utils.parse_utils import validate_email
from ovos_utils import classproperty
from ovos_utils.log import LOG
from ovos_utils.process_utils import RuntimeRequirements
from ovos_workshop.decorators import intent_handler
class SupportSkill(NeonSkill):
def __init__(self, **kwargs):
NeonSkill.__init__(self, **kwargs)
self.extra_diagnostic_files = ['/opt/neon/build_info.json']
@classproperty
def runtime_requirements(self):
return RuntimeRequirements(network_before_load=False,
internet_before_load=False,
gui_before_load=False,
requires_internet=True,
requires_network=True,
requires_gui=False,
no_internet_fallback=False,
no_network_fallback=False,
no_gui_fallback=True)
@property
def support_email(self) -> str:
"""
Email to refer users to for support
"""
return self.settings.get("support_email") or "support@neon.ai"
@intent_handler('contact_support.intent')
def handle_contact_support(self, message: Message):
"""
Handle a user request to contact support
:param message: Message associated with request
"""
user_profile = get_user_prefs(message)
email_addr = user_profile["user"]["email"]
if not validate_email(email_addr):
self.speak_dialog("no_email", private=True)
email_addr = self.support_email
if self.ask_yesno("confirm_support",
{"email": email_addr}) == "yes":
if user_profile["response_mode"].get("hesitation"):
self.speak_dialog("one_moment", private=True)
diagnostic_info = self._get_support_info(message, user_profile)
user_description = self.get_response("ask_description",
num_retries=0)
diagnostic_info["user_description"] = user_description
attachment_files = self._parse_attachments(
self._get_attachments(diagnostic_info))
if self.send_email(self.resources.render_dialog("email_title"),
self._format_email_body(diagnostic_info),
message, email_addr,
attachments=attachment_files):
self.gui.show_text(f"Email sent to {self.support_email} from {email_addr}")
self.speak_dialog("complete",
{"email": email_addr},
private=True)
return
LOG.error("Email failed to send, retry without attachments")
if self.send_email(self.resources.render_dialog("email_title"),
self._format_email_body(diagnostic_info),
message, email_addr):
self.gui.show_text(f"Email sent to {self.support_email} from {email_addr}")
self.speak_dialog("complete",
{"email": email_addr},
private=True)
else:
LOG.error("Email failed to send!")
self.gui.show_text(f"Email to {self.support_email} from {email_addr} failed to send")
self.speak_dialog("email_error", private=True)
else:
self.speak_dialog("cancelled", private=True)
@staticmethod
def _parse_attachments(files: list) -> dict:
"""
Parse a list of files into a dict of filenames to B64 contents.
Truncates log files to 50000 lines if they exceed 1MB as an arbitrary
limit that should safely keep all attachments within email provider
limits (~10MB-50MB).
:param files: list of files to include as attachments
"""
attachments = {}
for file in files:
try:
byte_size = getsize(file)
if byte_size > 1000000:
LOG.info(f"{file} is >1MB, truncating")
with open(file, 'rb+') as f:
content = f.read()
trunc = content[-1000000:].split(b'\n', 1)[1]
f.seek(0)
f.write(trunc)
f.truncate()
LOG.debug(f"{file} is {getsize(file)/1024/1024} MiB")
attachments[basename(file).replace('.log', '_log.txt')] = \
encode_file_to_base64_string(file)
except Exception as e:
LOG.exception(e)
return attachments
def _format_email_body(self, diagnostics: dict) -> str:
"""
Format the diagnostic data with email dialog and return a string body
:param diagnostics: diagnostic data to format into the email.
:returns: email body to send
"""
return '\n\n'.join((self.resources.render_dialog("email_intro",
{"email": self.support_email}),
diagnostics.get('user_description') or
"No Description Provided",
self.resources.render_dialog("email_signature")))
def _check_service_status(self, message: Message = None) -> dict:
"""
Query services on the messagebus and report back their status
"""
message = message or Message("get_status")
speech_module = self.bus.wait_for_response(
message.forward("mycroft.speech.is_ready"))
speech_status = speech_module.data.get("status") if speech_module \
else None
voice_module = self.bus.wait_for_response(
message.forward("mycroft.voice.is_ready"))
voice_status = voice_module.data.get("status") if voice_module \
else None
audio_module = self.bus.wait_for_response(
message.forward("mycroft.audio.is_ready"))
audio_status = audio_module.data.get("status") if audio_module \
else None
skills_module = self.bus.wait_for_response(
message.forward("mycroft.skills.is_ready"))
skills_status = skills_module.data.get("status") if skills_module \
else None
gui_module = self.bus.wait_for_response(
message.forward("mycroft.gui_service.is_ready"))
gui_status = gui_module.data.get("status") if gui_module \
else None
phal_module = self.bus.wait_for_response(
message.forward("mycroft.PHAL.is_ready")
)
enclosure_status = phal_module.data.get("status") if phal_module \
else None
admin_module = self.bus.wait_for_response(
message.forward("mycroft.PHAL.admin.is_ready")
)
admin_status = admin_module.data.get("status") if admin_module \
else None
return {"speech": speech_status,
"voice": voice_status,
"audio": audio_status,
"skills": skills_status,
"gui": gui_status,
"enclosure": enclosure_status,
"admin": admin_status}
@staticmethod
def _get_log_files():
log_path = LOG.base_path
log_files = glob(join(log_path, "*.log"))
LOG.info(f"Found log files: {log_files}")
return log_files
def _get_support_info(self, message: Message,
profile: dict = None) -> dict:
"""
Collect relevant information to include in a support ticket
:param message: Message associated with support request
"""
user_profile = profile or get_user_prefs(message)
message_context = deepcopy(message.context)
loaded_skills = self.bus.wait_for_response(
message.forward("skillmanager.list"), "mycroft.skills.list"
)
loaded_skills = loaded_skills.data if loaded_skills else None
core_device_ip = get_ip_address()
packages = run([sys.executable, "-m", "pip", "list"],
capture_output=True).stdout.decode()
return {
"user_profile": user_profile,
"message_context": message_context,
"module_status": self._check_service_status(message),
"loaded_skills": loaded_skills,
"packages": packages,
"host_device": {"ip": core_device_ip},
"generated_time_utc": datetime.utcnow().isoformat()
}
def _get_attachments(self, info: dict) -> list:
"""
Parse dict information into attachment files for a support email
:param info: diagnostic information to parse into attachments
:returns: list of output attachment files
"""
log_files = self._get_log_files()
tempdir = mkdtemp()
att_files = list()
# Make a temp copy of log files to optionally truncate before sending
for log_file in log_files:
output_file = join(tempdir, basename(log_file))
shutil.copyfile(log_file, output_file)
att_files.append(output_file)
for file in self.extra_diagnostic_files:
if isfile(file):
output_file = join(tempdir, basename(file))
shutil.copyfile(file, output_file)
att_files.append(output_file)
packages_file = join(tempdir, "python_packages.txt")
diagnostics_file = join(tempdir, "diagnostics.txt")
core_config_file = join(tempdir, "core_config.txt")
# Add `pip list` output to its own file
with open(packages_file, 'w+') as f:
f.write(info.pop('packages', ""))
att_files.append(packages_file)
# Add core config output to its own file
with open(core_config_file, 'w+') as f:
yaml.dump(dict(self.config_core), f)
att_files.append(core_config_file)
# Dump gathered diagnostics to separate file
with open(diagnostics_file, 'w+') as f:
yaml.dump(info, f)
att_files.append(diagnostics_file)
return att_files
def stop(self):
pass