Location>code7788 >text

python connection pinned automated submission of OA approvals

Popularity:476 ℃/2024-08-13 17:14:36

I. Preparatory work

1. Install the AliCloud support package and click on the jump:/document/resourcedownload/download-server-sdk
2. Register for a pinned developer account and click on the link:/
3、Acquire AK,SK
4, USERID through the enterprise management background can view each user's ID or through the interface to get
5, PROCESS_CODE in the process of editing the page above the URL (is an odd setting, speechless family)

II. Development process

1、Acquire enterprise token
2. Submission for approval
3、Get approval return

import os
import sys

from typing import List

from alibabacloud_dingtalk.workflow_1_0.client import Client as dingtalkworkflow_1_0Client
from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client
from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dingtalk.workflow_1_0 import models as dingtalkworkflow__1__0_models
from alibabacloud_tea_util import models as util_models


USER_ID = "1405122143xxxxxx"
PROCESS_CODE = "PROC-B897CFC7-CA66-449C-8C70-xxxxxx"

class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> dingtalkoauth2_1_0Client:
        """
        utilization Token Initialize AccountClient
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config()
         = 'https'
        config.region_id = 'central'
        return dingtalkoauth2_1_0Client(config)

    @staticmethod
    def create_client_1_0() -> dingtalkworkflow_1_0Client:
        """
        utilization Token Initialize AccountClient
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config()
         = 'https'
        config.region_id = 'central'
        return dingtalkworkflow_1_0Client(config)

    @staticmethod
    def get_token() -> str:
        """
        Get InstanceTOKEN
        """
        client = Sample.create_client()
        get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest(
            app_key='xxxxx',
            app_secret='yyyyyyyyyyyyyyyyy'
        )
        res = client.get_access_token(get_access_token_request)
        accessToken = .access_token
        print("accessToken", accessToken)
        return accessToken

    @staticmethod
    def get_user_info(_token, _code):
        """
        incompetent,under test
        """
        client = Sample.create_client()
        get_sso_user_info_headers = dingtalkoauth_2__1__0_models.GetSsoUserInfoHeaders()
        get_sso_user_info_headers.x_acs_dingtalk_access_token = _token
        get_sso_user_info_request = dingtalkoauth_2__1__0_models.GetSsoUserInfoRequest(
            code=_code
        )
        print(_token, _code)
        res = client.get_sso_user_info_with_options(get_sso_user_info_request, get_sso_user_info_headers,
                                              util_models.RuntimeOptions())
        print("res", res)

    @staticmethod
    def get_process_schema_info(token, process_code):
        """
        Get Approval Structure,For example what fields in the approval need to be filled in,The following structure can be obtained
        /apiExplorer?spm=ding_open_doc.document.0.0.61e032c7Lzbckc#/?devType=org&api=workflow_1.0%23QuerySchemaByProcessCode
        _formList = [
            {'id': 'TextField_1B9X1OGLRIDC0', 'name': 'sectoral', 'value': "flat-roofed building"},
            {'id': 'MoneyField_265MJ5CX7OLC', 'name': 'sum of money(unit of money (in PRC: Chinese yuan, in USA: dollar, etc))', 'value': "100"},
            {'id': 'TextareaField_1JQHWE0CZJ6O0', 'name': 'Breakdown of profit sharing', 'value': ""},
            {'id': 'TextField_12VLX2OCTGNK0', 'name': 'fair-Full name of the company', 'value': "Toujon (grape type)"},
            {'id': 'TextField_23VZA2Q6HHLS0', 'name': 'account opening bank', 'value': "Toujon (grape type)"},
            {'id': 'NumberField_5DTYPD4WMP40', 'name': 'fair账号', 'value': "123123123"},
            {'id': 'TextField_15B01GLYJL280', 'name': 'associates', 'value': "Toujon (grape type)"},
            {'id': 'TextField_UTT9HNYLMV40', 'name': 'contact number', 'value': "Toujon (grape type)"},
            {'id': 'TextareaField_BIO57GH5LFC0', 'name': 'note', 'value': ""}
        ]
        """
        client = Sample.create_client_1_0()
        query_schema_by_process_code_headers = dingtalkworkflow__1__0_models.QuerySchemaByProcessCodeHeaders()
        query_schema_by_process_code_headers.x_acs_dingtalk_access_token = token
        query_schema_by_process_code_request = dingtalkworkflow__1__0_models.QuerySchemaByProcessCodeRequest(
            process_code=process_code
        )

        res = client.query_schema_by_process_code_with_options(query_schema_by_process_code_request, query_schema_by_process_code_headers, util_models.RuntimeOptions())
        # print("res", .schema_content.items)
        items = .schema_content.items
        formComponentObj = {
            "name":"",
            "value":""
        }
        formComponentValues = []
        for item in items:
            Obj = {
                "id": ,
                "name": ,
                "value": 123
            }
            (Obj)
        return formComponentValues

    @staticmethod
    def post_process_instance(token, user_id, process_code, form_list):
        """
        Create a new approval, Each field of thename value is a required field
        /apiExplorer?spm=ding_open_doc.document.0.0.61e032c7Lzbckc#/?devType=org&api=workflow_1.0%23StartProcessInstance
        _formList = [
            {'id': 'TextField_1B9X1OGLRIDC0', 'name': 'sectoral', 'value': "flat-roofed building"},
            {'id': 'MoneyField_265MJ5CX7OLC', 'name': 'sum of money(unit of money (in PRC: Chinese yuan, in USA: dollar, etc))', 'value': "100"},
            ...
        ]
        """
        client = Sample.create_client_1_0()
        start_process_instance_headers = dingtalkworkflow__1__0_models.StartProcessInstanceHeaders()
        start_process_instance_headers.x_acs_dingtalk_access_token = token
        form_component_values = []
        for item in form_list:
            values_obj = dingtalkworkflow__1__0_models.StartProcessInstanceRequestFormComponentValues(
                id=item['id'],
                name=item['name'],
                value=item['value']
            )
            form_component_values.append(values_obj)
            # break
        start_process_instance_request = dingtalkworkflow__1__0_models.StartProcessInstanceRequest(
            dept_id=890417386,
            originator_user_id=user_id,
            process_code=process_code,
            form_component_values=form_component_values
        )

        res = client.start_process_instance_with_options(start_process_instance_request, start_process_instance_headers,
                                                   util_models.RuntimeOptions())
        print("res====", res)
        return .instance_id

    @staticmethod
    def get_instance_info(token, instance_id):
        """
        Query Instance Approval Status
        /apiExplorer?spm=ding_open_doc.document.0.0.61e032c7Lzbckc#/?devType=org&api=workflow_1.0%23GetProcessInstance
        {
            "business_id": .business_id,  # Approval Example Business Number。 Available via pinning
            "status": ,  # state of affairs RUNNING:Approval in progress  TERMINATED:withdrawn  COMPLETED:Approval completed
            "result":  # agree:agree with  refuse:rejection statusbecause ofCOMPLETEDboth (... and...)resultbecause ofagreehour,Indicates that the approval order is completed and approved。
        }
        """
        client = Sample.create_client_1_0()
        get_process_instance_headers = dingtalkworkflow__1__0_models.GetProcessInstanceHeaders()
        get_process_instance_headers.x_acs_dingtalk_access_token = token
        get_process_instance_request = dingtalkworkflow__1__0_models.GetProcessInstanceRequest(
            process_instance_id=instance_id
        )
        res = client.get_process_instance_with_options(get_process_instance_request, get_process_instance_headers,
                                                 util_models.RuntimeOptions())
        if :
            return {
                "business_id": .business_id,  # Approval Example Business Number。 Available via pinning
                "status": ,  # state of affairs RUNNING:Approval in progress  TERMINATED:withdrawn  COMPLETED:Approval completed
                "result":  # agree:agree with  refuse:rejection statusbecause ofCOMPLETEDboth (... and...)resultbecause ofagreehour,Indicates that the approval order is completed and approved。
            }
        else:
            return {}


if __name__ == '__main__':
    token = Sample.get_token()
    formList = Sample.get_process_schema_info(token, PROCESS_CODE)
    # If the approval structure is known you can just write it to death,No need to get the structure
    _formList = [
        {'id': 'TextField_1B9X1OGLRIDC0', 'name': 'sectoral', 'value': "flat-roofed building"},
        {'id': 'MoneyField_265MJ5CX7OLC', 'name': 'sum of money(unit of money (in PRC: Chinese yuan, in USA: dollar, etc))', 'value': "100"},
        {'id': 'TextareaField_1JQHWE0CZJ6O0', 'name': 'Breakdown of profit sharing', 'value': ""},
        {'id': 'TextField_12VLX2OCTGNK0', 'name': 'fair-Full name of the company', 'value': "Toujon (grape type)"},
        {'id': 'TextField_23VZA2Q6HHLS0', 'name': 'account opening bank', 'value': "Toujon (grape type)"},
        {'id': 'NumberField_5DTYPD4WMP40', 'name': 'fair账号', 'value': "123123123"},
        {'id': 'TextField_15B01GLYJL280', 'name': 'associates', 'value': "Toujon (grape type)"},
        {'id': 'TextField_UTT9HNYLMV40', 'name': 'contact number', 'value': "Toujon (grape type)"},
        {'id': 'TextareaField_BIO57GH5LFC0', 'name': 'note', 'value': ""}
    ]
	# Submission for approval,and return for approvalID
    instanceId = Sample.post_process_instance("beab04e9490a3700a577a4c1c15d9698", USER_ID, PROCESS_CODE, _formList)
	# Get additional details on approvals
    result = Sample.get_instance_info("beab04e9490a3700a577a4c1c15d9698", "tlB2uVSURhOhp9SzVPbi3g08441723284181")
    # print("result", result)