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

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
Configure WeCom
Go to the WeCom admin console to obtain your enterprise ID.

Log on to the WeCom Application platform.
Choose App Management and create an application.

Configure the API for receiving messages.


The URL format is https://{your-domain-name}/api/wechat/work/chat.
For the token and encoding AES key, click Generate Randomly.
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.
After you click Save, return to the application configuration page to view the Secret.

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

Procedure

First, call the
ApplyForStreamAccessTokenAPI to obtain authentication credentials. For more information, see Obtain connection information for streaming calls.A successful call to the
ApplyForStreamAccessTokenAPI 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.Listen for and receive messages sent by users. For more information, see Receive messages.
Parse the received messages to extract the user's question and user information. For more information, see Message format.
Call the
GetTokenAPI to obtain the WeCom access token. This token is required for all calls to WeCom APIs. For more information, see Get an access token.Store the WeCom access token for subsequent API calls.
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.
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.
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.
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)
Configure and run the sample
Install the Java Development Kit (JDK). This sample project requires JDK 8. For more information, see Install JDK on Windows.
Follow the steps in this topic to obtain the required sample parameters.
Modify the parameters in the project's
application.propertiesconfiguration file.Run
com.aliyun.chatbot.channel.WebApplication. Alternatively, you can package the project into a JAR file and run it with thejava -jarcommand.
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.





