def flow_hook_task(ticket_id): """ hook 任务 :param ticket_id: :return: """ # 查询工单状态 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() state_id = ticket_obj.state_id state_obj = State.objects.filter(id=state_id, is_deleted=0).first() participant_type_id = state_obj.participant_type_id if participant_type_id != constant_service_ins.PARTICIPANT_TYPE_HOOK: return False, '' hook_config = state_obj.participant hook_config_dict = json.loads(hook_config) hook_url = hook_config_dict.get('hook_url') hook_token = hook_config_dict.get('hook_token') wait = hook_config_dict.get('wait') extra_info = hook_config_dict.get('extra_info') flag, msg = common_service_ins.gen_hook_signature(hook_token) if not flag: return False, msg flag, all_ticket_data = ticket_base_service_ins.get_ticket_all_field_value( ticket_id) if extra_info is not None: all_ticket_data.update(dict(extra_info=extra_info)) try: r = requests.post(hook_url, headers=msg, json=all_ticket_data, timeout=10) result = r.json() except Exception as e: result = dict(code=-1, msg=e.__str__()) if result.get('code') == 0: # 调用成功 if wait: # date等格式需要转换为str for key, value in all_ticket_data.items(): if type(value) not in [int, str, bool, float]: all_ticket_data[key] = str(all_ticket_data[key]) all_ticket_data_json = json.dumps(all_ticket_data) TicketBaseService().add_ticket_flow_log( dict(ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=constant_service_ins. PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot')) return True, '' else: # 不等待hook目标回调,直接流转 flag, transition_queryset = workflow_transition_service_ins.get_state_transition_queryset( state_id) transition_id = transition_queryset[0].id # hook状态只支持一个流转 new_request_dict = {} new_request_dict.update({ 'transition_id': transition_id, 'suggestion': msg, 'username': '******' }) # 执行流转 flag, msg = ticket_base_service_ins.handle_ticket(ticket_id, new_request_dict, by_hook=True) if not flag: return False, msg else: ticket_base_service_ins.update_ticket_field_value( ticket_id, {'script_run_last_result': False}) flag, result = ticket_base_service_ins.get_ticket_all_field_value_json( ticket_id) all_ticket_data_json = result.get('all_field_value_json') ticket_base_service_ins.add_ticket_flow_log( dict( ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=constant_service_ins.PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot'))
def send_ticket_notice(ticket_id): """ 发送工单通知 :param ticket_id: :return: """ # 获取工单信息 # 获取工作流信息,获取工作流的通知信息 # 获取通知信息的标题和内容模板 # 将通知内容,通知标题,通知人,作为hook的请求参数 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() if not ticket_obj: return False, 'ticket is not exist or has been deleted' workflow_id = ticket_obj.workflow_id workflow_obj = Workflow.objects.filter(id=workflow_id, is_deleted=0).first() notices = workflow_obj.notices if not notices: return True, 'no notice defined' notice_str_list = notices.split(',') notice_id_list = [int(notice_str) for notice_str in notice_str_list] send_notice_result_list = [] title_template = workflow_obj.title_template content_template = workflow_obj.content_template # 获取工单所有字段的变量 flag, ticket_value_info = ticket_base_service_ins.get_ticket_all_field_value( ticket_id) if flag is False: return False, ticket_value_info title_result = title_template.format(**ticket_value_info) content_result = content_template.format(**ticket_value_info) # 获取工单最后一条操作记录 flag, result = ticket_base_service_ins.get_ticket_flow_log( ticket_id, 'loonrobot') flow_log_list = result.get('ticket_flow_log_restful_list') last_flow_log = flow_log_list[0] participant_info_list = [] participant_username_list = [] from apps.account.models import LoonUser if ticket_obj.participant_type_id == constant_service_ins.PARTICIPANT_TYPE_PERSONAL: participant_username_list = [ticket_obj.participant] elif ticket_obj.participant_type_id == constant_service_ins.PARTICIPANT_TYPE_MULTI: participant_username_list = ticket_obj.participant.split(',') elif ticket_obj.participant_type_id == constant_service_ins.PARTICIPANT_TYPE_ROLE: flag, participant_username_list = account_base_service_ins.get_role_username_list( ticket_obj.participant) if flag is False: return False, participant_username_list elif ticket_obj.participant_type_id == constant_service_ins.PARTICIPANT_TYPE_DEPT: flag, participant_username_list = account_base_service_ins.get_dept_username_list( ticket_obj.participant) if not flag: return flag, participant_username_list if participant_username_list: participant_queryset = LoonUser.objects.filter( username__in=participant_username_list, is_deleted=0) for participant_0 in participant_queryset: participant_info_list.append( dict(username=participant_0.username, alias=participant_0.alias, phone=participant_0.phone, email=participant_0.email)) params = { 'title_result': title_result, 'content_result': content_result, 'participant': ticket_obj.participant, 'participant_type_id': ticket_obj.participant_type_id, 'multi_all_person': ticket_obj.multi_all_person, 'ticket_value_info': ticket_value_info, 'last_flow_log': last_flow_log, 'participant_info_list': participant_info_list } for notice_id in notice_id_list: notice_obj = CustomNotice.objects.filter(id=notice_id, is_deleted=0).first() if not notice_obj: continue hook_url = notice_obj.hook_url hook_token = notice_obj.hook_token # gen signature flag, headers = common_service_ins.gen_signature_by_token(hook_token) try: r = requests.post(hook_url, headers=headers, json=params) result = r.json() if result.get('code') == 0: send_notice_result_list.append( dict(notice_id=notice_id, result='success', msg=result.get('msg', ''))) else: send_notice_result_list.append( dict(notice_id=notice_id, result='fail', msg=result.get('msg', ''))) except Exception as e: send_notice_result_list.append( dict(notice_id=notice_id, result='fail', msg=e.__str__())) return True, dict(send_notice_result_list=send_notice_result_list)