|
ID_MAILBOX = 33333 # Support Mailbox |
|
ID_FOLDER = 444444 # Unassigned Folder |
|
TEAM_MEMBERS = [{"id": 11111, "name": "Bert" , "share": 0}, |
|
{"id": 22222, "name": "Ernie" , "share": 1}, |
|
{"id": 33333, "name": "George", "share": 1}] |
|
|
|
import re |
|
import random |
|
import requests |
|
import json |
|
from datetime import datetime |
|
from help_scout_helper import query_helpscout, query_helpscout_all_pages |
|
|
|
################### COMMAND LINE OPTIONS ################################# |
|
|
|
#using optparse instead of argparse b/c of older versions of python, <=2.6 |
|
from optparse import OptionParser |
|
|
|
parser = OptionParser() |
|
parser.add_option('--quiet',dest='quiet',action='store_true', help="don't print final output") |
|
parser.add_option('--debug',dest='debug',action='store_true', help="doesn't make any changes to cases") |
|
parser.set_defaults(quiet=False, debug=False) |
|
|
|
(options, args) = parser. parse_args() |
|
|
|
quiet = options.quiet |
|
debug = options.debug |
|
|
|
########################################################################### |
|
|
|
|
|
|
|
############## SET UP RANDOM LOTTERY OF REP IDS ########################### |
|
|
|
team = TEAM_MEMBERS |
|
|
|
#lottery is an array to help with randomness |
|
lottery = [] |
|
#this will fill the lottery array with the id values in each rep, |
|
#one for each "share" value above |
|
for rep in team: |
|
lottery.extend([rep["id"]] * rep["share"]) |
|
|
|
#shuffle the list |
|
random.shuffle(lottery) |
|
|
|
########################################################################### |
|
|
|
############ GET CONVERSATIONS, ASSIGN THEM TO PEOPLE ##################### |
|
|
|
url = 'mailboxes/%s/folders/%s/conversations.json' % (ID_MAILBOX, ID_FOLDER) |
|
resp, conversations = query_helpscout_all_pages('GET', url) |
|
|
|
i = 0 |
|
for conversation in conversations: |
|
######################## GET PERSON WHO LAST EDITED CONVERSATION ###################### |
|
# |
|
# For each conversation returned, we have to query the API again to get the full |
|
# list of threads. The first thread tells us who last edited it. |
|
# |
|
url = 'conversations/%s.json' % conversation["id"] |
|
resp = query_helpscout('GET', url) |
|
conversation_detail = resp.json() |
|
id_person_last_edited_by = -1 |
|
if 'threads' in conversation_detail['item'] and len(conversation_detail['item']['threads']) > 0: |
|
most_recent_thread = conversation_detail['item']['threads'][0] |
|
if 'createdBy' in most_recent_thread and most_recent_thread['createdBy']['type'] == 'user': |
|
id_person_last_edited_by = most_recent_thread['createdBy']['id'] |
|
|
|
target_rep = id_person_last_edited_by |
|
|
|
########################################################################### |
|
|
|
# If the person who last edited the conversation assigned it to 'Anyone', |
|
# we want to keep iterating through the lottery until we get a different user. |
|
tries = 0 |
|
max_tries = 10 |
|
while target_rep == id_person_last_edited_by and tries < max_tries: |
|
target_rep = lottery[i % len(lottery)] |
|
i += 1 |
|
tries += 1 |
|
|
|
# if target_rep is still -1, this means that we don't have anyone to assign it to, usually |
|
# because there's only 1 person with an inbox share and that person assigned the case to |
|
# Anyone. Just skip it. |
|
if target_rep == -1: |
|
continue |
|
|
|
# If the debug flag is sent, don't actually update the conversation. Good for testing. |
|
if not debug: |
|
# You can't just supply the id of the owner. You have to supply a dummy person object. |
|
data = {"owner": {'id': target_rep, 'type': 'user'}} |
|
resp = query_helpscout('PUT', url, data) |
|
if not quiet: |
|
if resp.status_code == requests.codes.ok: |
|
print "live: assign %s to %s" % (conversation['id'], target_rep) |
|
else: |
|
print "failed: assign %s to %s" % (conversation['id'], target_rep) |
|
print resp.status_code |
|
print resp.text |
|
elif not quiet: |
|
print "debug: assign %s to %s" % (conversation['id'], target_rep) |
|
|
|
########################################################################### |