generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
358 lines (272 loc) · 8.28 KB
/
functions.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import sys
import os
from random import choice
from time import sleep
from datetime import datetime
from send_email import send_email
def get_datetime():
"""
Returns the current time.
e.g. 17:47:15
"""
return datetime.now().strftime("%H:%M:%S")
def typing_effect(text):
"""
Prints text with a typing effect.
"""
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
sleep(0.05)
def chatbot_message(text):
"""
Returns the chatbot message.
"""
log(text, "Agent")
return typing_effect(text)
def choose_name_randomly():
"""
Returns a random name from an external file called names.txt.
"""
with open("names.txt", "r") as name_list:
name = [name for name in name_list.read().splitlines()]
return choice(name)
def greetings():
"""
Prints a greeting to the user.
"""
chosen_name = choose_name_randomly()
chatbot_message(
f"Hello, I'm {chosen_name}, your digital assistant."
"\nIt's nice to speak with you."
"\nMay I please have your name?"
)
username = get_str_input().title()
chatbot_message(
f"Hi, {username}. Thank you for using our chat service.\n"
"How may I assist you today?\n"
)
def print_menu():
"""
Prints the menu of options.
"""
chatbot_message(
"\n[1] Add a new task"
"\n[2] View all tasks"
"\n[3] Delete a task"
"\n[4] Restore a task"
"\n[5] Exit"
)
def get_int_input():
"""
Returns the user input.
Run a while loop to collect a valid data from the user
via the terminal, which must be a digit.
The loop will continue to prompt the user until the data is valid.
"""
while True:
user_input = input("\n>> ")
is_a_digit = validate_input(user_input, str.isdigit)
if is_a_digit:
break
log(user_input, "User")
return int(user_input)
def get_str_input():
"""
Returns the user input.
Run a while loop to collect a valid data from the user
via the terminal, which must be a string.
The loop will continue to prompt the user until the data is valid.
"""
while True:
user_input = input("\n>> ").strip().lower()
new_string = "".join(user_input.split())
is_a_string = validate_input(new_string, str.isalpha)
if is_a_string:
break
log(user_input, "User")
return user_input
def validate_input(user_input, str_method):
"""
Validates the user input.
Inside the try/except block, the user is prompted
to enter the data type. Raises a ValueError if
data type is not a digit or an alpha,
depending to the data type specification
by the user.
"""
try:
# str_method: str.isalpha() or str.isdigit()
# depending to the data type specification by the user.
if not str_method(user_input):
raise ValueError(f"\n'{user_input}' isn't a valid input.")
except ValueError as err:
# Otherwise, a ValueError is raised.
chatbot_message(f"{err}")
return False
return True
def start_bot():
"""
Starts bot.
Run a while loop to get the user choice.
The loop will continue to prompt the user until
he/she decides to exit the program.
"""
while True:
print_menu()
user_choice = get_int_input()
if user_choice == 1:
add_new_task()
elif user_choice == 2:
chatbot_message("\nHere is your list of tasks:\n")
view_all_tasks(task_list)
elif user_choice == 3:
remove_task()
elif user_choice == 4:
restore_task()
elif user_choice == 5:
end_chat()
else:
chatbot_message(
f"\nThis option does not exist in the menu. Please try again."
)
def add_new_task():
"""
Adds a new task to the list.
"""
chatbot_message("What task would you like to add?")
task = get_str_input()
chatbot_message(f"Great! Let's add [{task.upper()}] to your list.")
chatbot_message("\nAdding task...")
task_list.append(task.capitalize())
sleep(2)
chatbot_message("\nTask added!")
ask_to_add_task()
def view_all_tasks(list):
"""
Prints a list of tasks.
"""
if not list:
sleep(1)
chatbot_message("Your list is still empty.\n")
ask_to_add_task()
else:
for i, task in enumerate(list):
i += 1
print(f"\n\t{i} - {task}")
sleep(1)
def remove_task():
"""
Returns a removed task from the list.
"""
if not task_list:
sleep(1)
chatbot_message("\nThere is no tasks to be removed.\n")
ask_to_add_task()
else:
chatbot_message("Which task would you like to delete?")
view_all_tasks(task_list)
task_to_delete = get_int_input()
for i, task in enumerate(task_list):
if task_to_delete != i + 1:
return chatbot_message("Task not found.\n")
else:
removed_task = task_list.pop(i)
removed_items.append(removed_task)
sleep(1)
return chatbot_message(f"Task [{task}] removed!\n")
def restore_task():
"""
Restores a removed task.
"""
if not removed_items:
sleep(1)
chatbot_message("There is no tasks to be restored.\n")
ask_to_add_task()
else:
chatbot_message("Which task would you like to restore?")
view_all_tasks(removed_items)
task_to_restore = get_int_input()
for i, task in enumerate(removed_items):
if task_to_restore != i + 1:
return chatbot_message("Task not found.\n")
else:
restored_task = removed_items.pop(i)
task_list.append(restored_task)
sleep(1)
return chatbot_message(f"Task [{task}] restored!\n")
def end_chat():
"""
Prints a message to the user ending the conversation.
"""
chatbot_message(
"I'm glad I was able to get that sorted out for you."
"\nBefore you go, would you like to get a copy of this chat? [y/N]"
)
user_answer = get_str_input()[0]
while user_answer not in ["y", "n"]:
chatbot_message(
"Sorry. I didn't understand what you said."
"\nCan you please say it again?"
)
user_answer = get_str_input()[0]
if user_answer == "y":
chatbot_message("Great! Enter your email address below:")
email = log(input("\n>> "), "User")
sent = send_email(email)
if sent:
# if email address has been successfully validated
# and the email has been sent.
chatbot_message(
"OK, all done! Check your inbox for an email "
"with the LiveChat transcript.\n"
)
chatbot_message(
"\nThank you so much for using our chat service.\n"
"We hope we will hear from you soon.\n"
"Have a great day!\n"
)
sleep(2)
os.remove("log.txt")
print("\n>>> The agent has left the chat.")
sleep(3)
clear_output()
sys.exit()
def ask_to_add_task():
"""
Asks the user if he/she wants to add a new task.
"""
chatbot_message("\nWould you like to add a new task? [y/N]")
answer = get_str_input()[0]
if answer == "y":
add_new_task()
else:
chatbot_message("Okay, taking you back to the main menu.\n")
# then loop goes back to the main thread (print_menu)
def log(message, person):
"""
Logs the conversation and save to an external file.
"""
path = "log.txt"
with open(path, "a", newline="") as log_file:
log_file.write(f"[{person}][{get_datetime()}] - {message}\n")
return message
def clear_output():
"""
This function clears the output of the terminal
right after the user exits the program.
"""
return os.system("cls") if os.name == "nt" else os.system("clear")
def main():
"""
Run all program functions.
"""
# Starts the program with the console clear.
clear_output()
# Prints the welcome message.
greetings()
# Starts the main loop.
start_bot()
# global variables
removed_items = []
task_list = []