All Products
Search
Document Center

Function Compute (2.0):Trigger a function by using a third-party service

Last Updated:Jan 23, 2024

EventBridge can integrate with a DingTalk connector, which allows you to build various employee management systems based on input parameters of your business operations. This topic describes how to use a third-party service to call HTTP API operations. In this topic, the process of adding an employee to a DingTalk team is used as an example.

Workflow

In this topic, the following services are used: DingTalk, DingTalk connector, EventBridge, and Function Compute. The following figure shows the workflow for triggering HTTP APIs by using DingTalk.

image

Prerequisites

Before you start, make sure that the following operations are performed:

Step 1: Create a custom event bus

  1. Log on to the EventBridge console. In the top navigation bar, select a region.

  2. In the left-side navigation pane, click Event Buses. In the Custom Event Buses section, click Quickly Create.

  3. In the Create Custom Event Bus panel, perform the following operations in each step:

    1. In the Event Bus step, configure the Name and Description parameters and click Next Step.

    2. In the Event Source step, configure the Event Source Name and Description parameters, proceed with the parameters in the following table, and then click Next Step.

      Parameter

      Value

      Event Provider

      HTTP/HTTPS Events

      Request Type

      HTTPS

      Request Method

      POST

      Security Settings

      Optional

    3. In the Event Rule step, click Skip. The Skip Subsequent Steps message appears. Click OK. This ensures that no event rules and event targets are created on the event bus.

      After you create an event source, it is displayed on the Event Sources page. You can click Details in the Operations column to view the details of the event source. You can use the value of the Internet Request URL parameter as the webhook URL of the DingTalk connector.pic-1

Step 2: Create a DingTalk connection flow

  1. Log on to the DingTalk Open Platform console. In the top navigation bar, choose Open Capabilities > Connected Platform.

  2. In the left-side navigation pane, choose My Connection > Connection Flow.

  3. On the My Connection Flows page, select Business Flow from the Create drop-down list.

    1. In the Create Flow dialog box, specify the name and description of the flow, and set Trigger to Official > Contacts > Contacts User Add.

    2. On the Connection Flow tab, click the 添加 icon and then Network Request.

    3. On the right side of the Network Request section, click the 箭头 icon. In the Network Request panel, configure request parameters and click Save. The following table describes the parameters.

      Parameter

      Description

      Example

      Request Method

      The request method that is configured in Step 1.

      POST

      Request URL

      The URL that you obtained in Step 1.

      https://115964845466****.eventbridge.cn-hangzhou.aliyuncs.com/webhook/putEvents?token=8a5c4e6****

      Body

      The fields in the message.

      root

Step 3: Use Function Compute to call HTTP API operations

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, click Create Service.

  3. In the Create Service panel, enter the service name and description, configure other parameters, and then click OK. The following table describes the parameters.

    Parameter

    Description

    Logging

    Specify whether to enable the logging feature. Valid values:

    • Enable: Function Compute sends function execution logs to Simple Log Service for persistent storage. You can debug code, troubleshoot issues, and analyze data based on the logs.

    • Disable: You cannot use Simple Log Service to store or query function execution logs.

    Advanced options

    Managed Service for OpenTelemetry

    Specifies whether to enable Managed Service for OpenTelemetry. Valid values:

    • Enable: enables Managed Service for OpenTelemetry. You can use Jaeger to upload trace information. This allows you to view the traces of functions. You can also analyze and diagnose performance bottlenecks in serverless architectures. For more information, see Overview of Managed Service for OpenTelemetry.

    • Disable: disables Managed Service for OpenTelemetry.

    Service Role

    Configure the role for functions in the service. This way, the functions can obtain the permissions of the role. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

    Access to VPC

    Specifies whether to allow functions to access virtual private cloud (VPC) resources. For more information, see Configure network settings.

    VPC

    This parameter is required if you set Access to VPC to Yes. Create a VPC or select the ID of an existing VPC that you want to access from the drop-down list.

    vSwitch

    This parameter is required if you set Access to VPC to Yes. Create a vSwitch or select the ID of an existing vSwitch from the drop-down list.

    Security Group

    This parameter is required if you set Access to VPC to Yes. Create a security group or select an existing security group from the drop-down list.

    Access to Internet

    Specifies whether to allow functions in the service to access the Internet. If you set this parameter to No, functions in the service cannot access the Internet over the default network interface controller (NIC) in Function Compute.

    Important

    If you want to use a static public IP address to access the Internet, you must set Access to Internet to No. Otherwise, the configured static public IP address does not take effect. For more information, see Configure static public IP addresses.

  4. On the Services page, click the name of the service that you want to view and click Create Function.

  5. On the Create Function page, select Use Built-in Runtime to create a function, configure the following parameters, and then click Create.

    1. In the Basic Settings section, configure the Function Name parameter and set Handler Type to Event Handler.

    2. In the Code section, set Runtime to Node.js, set Code Upload Method to Use Sample Code, and then click API Gateway-triggered Function.

      创建函数

    After you create the function, you can view the details on the function details page. 函数详情

Step 4: Create an event rule

  1. Log on to the EventBridge console and find the custom event bus that you created in Step 1.

  2. Click the name of the custom event bus. In the left-side navigation pane, click Event Rules.

  3. On the Event Rules page, click Create Rule. In the Create Rule panel, perform the operations in the following steps.

    1. In the Configure Basic Info step, configure the Name and Description parameters and click Next Step.

    2. In the Configure Event Pattern step, set Event Source Type to Custom Event Source and Event Source to the custom event source that you created in Step 1. Then, click Next Step.

    3. In the Configure Targets tab, configure parameters and click Create. The following table describes the parameters.

      Parameter

      Description

      Example

      Service Type

      Select the service type of the event target.

      Function Compute

      Service

      Select the service that is created in Step 3.

      testserver

      Function

      Configure the function that is created in Step 3.

      testfunction

      Event

      Select Complete Event.

      Complete Event

      Invocation Mode

      Select Synchronous.

      Synchronous

Step 5: Add an employee to automatically trigger an HTTP API operation

  1. Invite members to join your DingTalk team. For more information, see How do I add members?

  2. On the details page of the custom event bus that you created in Step 1, click Event Tracking, specify a time range, and then click Query.

  3. Find the event that you want to manage and click Event Track in the Operations column to check whether the new employee event is delivered.

    投递成功

Verify the result

Log on to the Function Compute console and view the function that you created in Step 3. In the Requests List section of the Function Logs tab, you can find that the function is executed once.