diff --git a/ticket_transfer.py b/ticket_transfer.py index 2f5122f..a233b86 100644 --- a/ticket_transfer.py +++ b/ticket_transfer.py @@ -79,10 +79,7 @@ def checkRate(response): # Globals Variables ################################### ticketArray = [] -transArray = [] groupIdDefs_source = [] -groupIdDefs_dest = [] -contactIdDefs = [] sourceDom = '' destDom = '' sourceKey = '' @@ -113,37 +110,37 @@ print('/_/ /_/\___/_/|_|\___/\__/ /_/ /_/ \__,_/_/ /_/____/_/ \___/_/ print('=========================================================================') ## Getting transfer settings from user -sourceDom = input('Please enter the source domain:\n(ex. "https://your_domain.freshdesk.com")\n') +sourceDom = input('Please enter the source domain:\n(ex. "https://your_domain.freshdesk.com")\n> ') if sourceDom == '': print(f'{RED}Invalid Domain Provided{RESET}') sys.exit(1) else: clear(3) sourceDom = "".join(sourceDom.split()) - print(f'SETTINGS:\nSource Domain: {GREEN}{sourceDom}{RESET}') -sourceKey = input('Please provide the API Key for the source account:\n(Hint: You can find this from your profile settings menu in Freshdesk)\n') + print(f'SETTINGS:\nSource Domain: {GREEN}{sourceDom}{RESET}\n=========================================================================') +sourceKey = input('Please provide the API Key for the source account:\n(Hint: You can find this from your profile settings menu in Freshdesk)\n> ') if sourceKey == '': print(f'{RED}Invalid Key Provided{RESET}') sys.exit(1) else: clear(3) sourceKey = "".join(sourceKey.split()) -destDom = input('Please enter the destination domain:\n') +destDom = input('Please enter the destination domain:\n> ') if destDom == '': print(f'{RED}Invalid Domain Provided{RESET}') sys.exit(1) else: clear(4) destDom = "".join(destDom.split()) - print(f'Source Domain: {GREEN}{sourceDom}{RESET} || Target Domain: {GREEN}{destDom}{RESET}') -destKey = input('Please provide the API key for the destination account:\n') + print(f'Source Domain: {GREEN}{sourceDom}{RESET}\nDestination Domain: {GREEN}{destDom}{RESET}\n=========================================================================') +destKey = input('Please provide the API key for the destination account:\n> ') if destKey == '': print(f'{RED}Invalid Key Provided{RESET}') sys.exit(1) else: - clear(3) + clear(2) destKey = "".join(destKey.split()) -targetGroup = input('What is the name of the Freshdesk group you want to transfer from?\n') +targetGroup = input('What is the name of the Freshdesk group you want to transfer from?\n> ') if targetGroup == '': print(f'{RED}Invalid Group Name Provided{RESET}') sys.exit(1) @@ -158,14 +155,11 @@ print(f'Obtaining account information...◐') groupIdDefs_source = makeCall(sourceDom + '/api/v2/groups', 'get', sourceKey) clear(1) print(f'Obtaining account information...◓') -contactIdDefs = makeCall(sourceDom + '/api/v2/contacts', 'get', sourceKey) -clear(1) -print(f'Obtaining account information...◑') groupIdDefs_dest = makeCall(destDom + '/api/v2/groups', 'get', destKey) clear(1) -print(f'Obtaining account information...◒') +print(f'Obtaining account information...◑') for i, group in enumerate(groupIdDefs_source): - anim = ['◐', '◓', '◑', '◒'] + anim = ['◒','◐', '◓', '◑'] if group["name"] == targetGroup: targetGroupId = group["id"] break @@ -176,24 +170,26 @@ print(f'Obtaining account information...{GREEN}COMPLETE{RESET}') ## Building Ticket Array from Source print(f'Gathering list of tickets from source...') -ticketArray = makeCall(sourceDom + '/api/v2/search/tickets?query=' + urllib.parse.urlencode(f'"group_id:\'{targetGroupId}\'"'), 'get', sourceKey) +#ticketArray = makeCall(sourceDom + '/api/v2/search/tickets?query=' + urllib.parse.urlencode(f'"group_id:\'{targetGroupId}\'"'), 'get', sourceKey) +ticketArray = makeCall(f'{sourceDom}/api/v2/search/tickets?query="group_id:{targetGroupId}"', 'get', sourceKey) clear(1) print(f'Gathering list of tickets from source...{GREEN}COMPLETE{RESET}') ## Transferring tickets to Destination -print(f'*This may take a while*') +print(f'{YELLOW}*This may take a while*{RESET}') print(f'Transferring tickets to destination account...◐') -for i, ticket in enumerate(ticketArray): +for i, ticket in enumerate(ticketArray['results']): attachments = {} anim = ['◓', '◑', '◒', '◐'] ticketData = makeCall(sourceDom + f'/api/v2/tickets/{ticket["id"]}?include=conversations,requester', 'get', sourceKey) - for x, attachment in enumerate(ticket['attachments']): - if attachment['attachment_url']: - fileRes = requests.get(attachment.attachment_url) - if fileRes.status_code == 200: - attachments[x] = (attachment['name'], fileRes.content) - else: - print(f'{RED}File Error:{RESET}\nFailed to download {attachment["name"]}') + if 'attachments' in ticket: + for x, attachment in enumerate(ticket['attachments']): + if attachment.get('attachment_url'): + fileRes = requests.get(attachment.attachment_url) + if fileRes.status_code == 200: + attachments[x] = (attachment['name'], fileRes.content) + else: + print(f'{RED}File Error:{RESET}\nFailed to download {attachment["name"]}') payload = { 'name': ticketData["requester"]["name"], 'email': ticketData["requester"]["email"], @@ -204,7 +200,7 @@ for i, ticket in enumerate(ticketArray): 'description': ticketData["description"], 'source': ticketData["source"], } - makeCall(destDom + '/api/v2/tickets', 'post', destKey, payload, files=attachments if attachments else None) + newTicket = makeCall(destDom + '/api/v2/tickets', 'post', destKey, payload, files=attachments if attachments else None) ## Gaterhing conversation history from ticketData, and then updating the ticket to include them in a private note if len(ticketData['conversations']) > 0: @@ -214,20 +210,23 @@ for i, ticket in enumerate(ticketArray): =====================
''' for convo in ticketData['conversations']: - for contact in contactIdDefs: - if contact['id'] == convo['user_id']: - username = contact['name'] - break - if convo['private'] == 'true': - convos += f'Private Note from {username}:
{convo["body"]}
---------------------------

' - elif convo['private'] == 'false' and convo['incoming'] == 'true': - convos += f'Incoming Reply from {username}:
{convo["body"]}
---------------------------

' - elif convo['private'] == 'false' and convo['incoming'] == 'false': - convos += f'Outgoing Reply from {username}:
{convo["body"]}
---------------------------

' - makeCall(destDom + f'/api/v2/tickets/{ticket['id']}/notes', 'post', destKey,{'body': convos}) + if convo['incoming'] == False: + agent = (makeCall(sourceDom + f'/api/v2/agents/{convo["user_id"]}', 'get', sourceKey)) + username = agent["contact"]["name"] + if convo['incoming'] == True: + user = (makeCall(sourceDom + f'/api/v2/contacts/{convo["user_id"]}', 'get', sourceKey)) + username = user["name"] + if convo['private'] == True: + convos += f'Private Note from {username}:
{convo["body"]}
---------------------------
' + elif convo['private'] == False and convo['incoming'] == True: + convos += f'Incoming Reply from {username}:
{convo["body"]}
---------------------------
' + elif convo['private'] == False and convo['incoming'] == False: + convos += f'Outgoing Reply from {username}:
{convo["body"]}
---------------------------
' + makeCall(destDom + f'/api/v2/tickets/{newTicket["id"]}/notes', 'post', destKey,{'body': convos}) clear(1) print(f'Transferring tickets to destination account...{anim[i % len(anim)]}') + i += 1 clear(2) print(f'Transferring tickets to destination account...{GREEN}COMPLETE{RESET}') print(f'{GREEN}*TICKET TRANSFER COMPLETED SUCCESSFULLY*{RESET}')