Integration example: WeCom

Updated at:

Integrate an internal WeCom application with Alibaba Cloud Chatbot to enable Q&A powered by a large language model.

Demo

image

Important
  • Complete WeCom enterprise verification.

  • You need a domain name to receive callback messages. The domain name's registrant must be the same as or related to your verified WeCom entity. For more information, see WeCom Domains.

Before you begin

Chatbot setup

  1. Log on to the Alibaba Cloud Chatbot console. In the left-side navigation pane, choose App Management > Chatbot > Create Chatbot.

  2. Obtain the chatbot ID and publish the chatbot. If the publish button is not available, skip this step.

    image

  3. Go to the Alibaba Cloud Chatbot business space to obtain the AgentKey.

    image

  4. Go to the RAM console to obtain the AccessKey ID and AccessKey secret for your Alibaba Cloud account.

Configure WeCom

  1. Go to the WeCom admin console to obtain your enterprise ID.image

  2. Log on to the WeCom Application platform.

  3. Choose App Management and create an application.image

  4. Configure the API for receiving messages.imageimage

    1. The URL format is https://{your-domain-name}/api/wechat/work/chat.

    2. For the token and encoding AES key, click Generate Randomly.

    3. You must configure the enterprise ID, token, and encoding AES key in your code and deploy this sample to make the callback URL publicly accessible. Otherwise, clicking Done causes an "openapi callback address request failed" error.

  5. After you click Save, return to the application configuration page to view the Secret.image

  6. On the Application Configuration page, you can manage and configure Trusted IP Addresses.image

Procedure

image

  1. First, call the ApplyForStreamAccessToken API to obtain authentication credentials. For more information, see Obtain connection information for streaming calls.

  2. A successful call to the ApplyForStreamAccessToken API returns an access token, a channel ID, and a StreamSecret. These credentials are valid for 2 hours. When they expire, you must call the API again for new credentials.

  3. Listen for and receive messages sent by users. For more information, see Receive messages.

  4. Parse the received messages to extract the user's question and user information. For more information, see Message format.

  5. Call the GetToken API to obtain the WeCom access token. This token is required for all calls to WeCom APIs. For more information, see Get an access token.

  6. Store the WeCom access token for subsequent API calls.

  7. Generate the signature required to call the streaming session API. Calculate this signature by using the current timestamp and the StreamSecret parameter returned in step 2. For more information, see Session API signature.

  8. Concatenate the access token, channel ID, signature, and timestamp to form a complete URL. Use this URL to make a Server-Sent Events (SSE) streaming call to the session API. Listen for SSE events to receive the streaming output from the large language model. For more information, see Streaming session API.

  9. The streaming API for the large language model returns data multiple times. Each response contains the complete reply, so your rendering logic must replace the old content with the new content. For more information, see Streaming session API.

  10. Wait for the large language model to complete its response, and then send the complete message to the user as a single response.

Sample project code

Download source code (Java)

wx-work-example.zip

Configure and run the sample

  1. Install the Java Development Kit (JDK). This sample project requires JDK 8. For more information, see Install JDK on Windows.

  2. Follow the steps in this topic to obtain the required sample parameters.

  3. Modify the parameters in the project's application.properties configuration file.

  4. Run com.aliyun.chatbot.channel.WebApplication. Alternatively, you can package the project into a JAR file and run it with the java -jar command.

Sample parameters

Parameter

Description

ALIYUN_ACCESS_KEY

Your Alibaba Cloud account's AccessKey ID.

ALIYUN_SECRET_KEY

Your Alibaba Cloud account's AccessKey secret.

CHATBOT_INSTANCE_ID

The chatbot ID.

CHATBOT_AGENT_KEY

The AgentKey of the business space. To obtain it, go to Business Space Management.

WX_CORP_ID

The enterprise ID. For instructions, see Step 1 in the Configure WeCom section.

WX_TOKEN

The token from the API for receiving messages configuration. For instructions, see Step 4 in the Configure WeCom section.

WX_ENCODING_AES_KEY

The encoding AES key from the API for receiving messages configuration. For instructions, see Step 4 in the Configure WeCom section.

WX_SECRET

The application's secret. For instructions, see Step 5 in the Configure WeCom section.

FAQ

Q: When I configure the callback URL and click Done, I receive an "openapi callback address request failed" error.

A: Configure the enterprise ID, token, and encoding AES key in your code and deploy the sample to ensure the callback URL is publicly accessible.