Last active
May 15, 2020 06:46
-
-
Save interrogator/a29b87c5b22cece50c1506566deeec54 to your computer and use it in GitHub Desktop.
first line support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Utility to generate a first-line support message for a user who has submitted a roundup issue. | |
To only ever be one-file to discourage feature creep for a small utility. | |
The only main extensions I'd really consider are automatically posting this message to roundup, | |
but I don't think this is a good idea, as you may need to generate a couple of messages till | |
an appropriate one is generated | |
Example: | |
fline.py -t <msg> -n "Customer Name" -e "A second paragraph with more information." | |
Out: | |
Good evening Customer Name, | |
Your request has been received and may be forwarded on to someone with particular experience in this area. We will be keeping you updated. | |
A second paragraph with more information. | |
Cheers, | |
Danny McDonald | |
daniel.mcdonald@email.address | |
""" | |
import argparse | |
from random import randrange | |
from time import strftime | |
GREET = "Good {timeframe}" | |
SALUTATION = "Cheers," | |
SENDER = "Danny McDonald" | |
SIGNATURE = "daniel.mcdonald@email.address" | |
GOODBYE = f"{SALUTATION}\n{SENDER}\n{SIGNATURE}" | |
# generic templates that can be chosen at random or specified by number | |
TEMPLATES = { | |
"1": "Thanks for getting in touch. Just letting you know that we have received your message. I will review with some colleagues and get back to you with more information as soon as possible.", | |
"2": "Thanks for your message. I'll confer with some colleagues who spend more time with relevent packages and get back to you later with some suggestions.", | |
"3": "I've received your message, and someone from our team will get back to you as soon as possible with some further information.", | |
"4": "Your request has been received. One of our colleagues or I will be in contact with you again soon.", | |
"5": "Your request has been received and may be forwarded on to someone with particular experience in this area. We will be keeping you updated.", | |
"6": "Thanks for your request. I've taken a look through it, and will get back to you shortly if I find a solution or need more information", | |
"7": "Your ticket has been received, thank you. Either myself, or one of our other support staff, will be in touch shortly.", | |
} | |
# special named templates for specific situations | |
SPECIALS = { | |
"tomoz": "Thanks for lodging this request. Given the late hour, somebody will hopefully be able to attend to your issue at some point tomorrow.", | |
"away": "Thanks for submitting this request. I believe I can help with your issue, but unfortunately am unavailable for the next couple of hours. I will get back in a little while and start trying to solve it then.", | |
"forward": "Thanks for lodging this issue. It goes a little beyond my specific level of exertise, so I'm going to consult with a colleague with a bit more knowledge in this area and get back to you as soon as possible.", | |
"weekend": "Thanks for your message. Unfortunately our support desk is closed over the weekend, so it's likely that your ticket will get a more thorough response early next week.", | |
} | |
def _parse_cmdline_args(): | |
""" | |
Command line argument parsing | |
""" | |
parser = argparse.ArgumentParser(description="Generate first line response message") | |
parser.add_argument( | |
"-t", | |
"--template", | |
nargs="?", | |
type=str, | |
required=False, | |
default=str(randrange(1, len(TEMPLATES) + 1)), | |
help=f"Template number to use to use (1--{len(TEMPLATES)}). Leave blank for random, or use one of {'/'.join(SPECIALS)}", | |
) | |
parser.add_argument( | |
"-n", "--name", type=str, required=False, help="Name of addressee", | |
) | |
parser.add_argument( | |
"-e", | |
"--extra", | |
type=str, | |
required=False, | |
help="Extra text to go below first paragraph", | |
) | |
parser.add_argument( | |
"-l", | |
"--list-messages", | |
default=False, | |
action="store_true", | |
required=False, | |
help="Print list of available messages", | |
) | |
return vars(parser.parse_args()) | |
def greeting_time(): | |
""" | |
Morning, afternoon or evening? | |
""" | |
hour = int(strftime("%H")) | |
if hour < 6: | |
return "evening" | |
elif hour < 12: | |
return "morning" | |
elif hour < 17: | |
return "afternoon" | |
return "evening" | |
def generate_error(passed): | |
""" | |
print info when there's no template | |
""" | |
generic = f"1--{len(TEMPLATES)}" | |
special = "/".join(sorted(SPECIALS)) | |
err = f"Template should be either generic ({generic}) or special ({special})." | |
inp = f"You passed {passed}" | |
raise ValueError(" ".join([err, inp])) | |
def main(name=None, template=None, extra=None, list_messages=False): | |
""" | |
Main routine. Build the nice long string and print it to the terminal | |
""" | |
if list_messages: | |
to_show = sorted(list(({**TEMPLATES, **SPECIALS}.items()))) | |
largest_key = max([len(n) for n, m in to_show]) | |
for name, scheme in to_show: | |
print(f"{name.rjust(largest_key)}: {scheme[:110]}...") | |
return | |
# build greeting | |
greet = str(GREET).format(timeframe=greeting_time()) | |
if name: | |
greet += f" {name}" | |
greet += "," | |
# build main part | |
if template not in set(TEMPLATES) | set(SPECIALS): | |
generate_error(template) | |
message = TEMPLATES.get(template, SPECIALS.get(template)) | |
if extra and extra.strip(): | |
message = message.rstrip() + "\n\n" + extra.strip() | |
# generate ending | |
whole = "\n\n".join([greet, message, GOODBYE]) | |
print(f"\n\n\n\n{whole}\n\n\n\n") | |
if __name__ == "__main__": | |
kwargs = _parse_cmdline_args() | |
main(**kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment