# Step 2: Configure message processing business logic

When the user sends a message to the app robot in the Feishu session, the app will send a **receive message** event to the server request address. After the service receives the event data, it needs to match and return the hit encyclopedia information according to the content of the message. This tutorial only introduces the processing logic of the server-side code and provides JavaScript sample code. You need to improve the code yourself to implement event processing and message sending according to the actual situation of the local service.

## Code logic description

1. Receive the event callback data sent by the open platform application.

An example of the data content is as follows, including information about the sender of the message and the content of the message.

```JSON
     {
         "schema": "2.0",
         "header": {
             "event_id": "5e3702a84e847582be8db7fb73283c02", // Event ID
             "event_type": "im.message.receive_v1", // event type
             "create_time": "1608725989000",//Event creation time
             "token": "rvaYgkND1GOiu5MM0E1rncYC6PLtF7JV", //Event Token
             "app_id": "cli_9f5343c580712544", // Application ID
             "tenant_key": "2ca1d211f64f6438" // Tenant Key
         },
         "event": {
             "sender": {
                 "sender_id": { // User ID information set
                     "union_id": "on_8ed6aa67826108097d9ee143816345",
                     "user_id": "e33ggbyz",
                     "open_id": "ou_84aad35d084aa403a838cf73ee18467"
                 },
                 "sender_type": "user", // message sender type
                 "tenant_key": "736588c9260f175e" // The unique identifier of the tenant on Feishu
             },
             "message": {
                 "message_id": "om_5ce6d572455d361153b7cb51da133945", // Message ID
                 "root_id": "om_5ce6d572455d361153b7cb5xxfsdfsdfdsf", // root message ID
                 "parent_id": "om_5ce6d572455d361153b7cb5xxfsdfsdfdsf", // ID of parent message
                 "create_time": "1609073151345", // message sending time
                 "chat_id": "oc_5ce6d572455d361153b7xx51da133945", // the group ID of the message
                 "chat_type": "group", // the type of group where the message is located
                 "message_type": "text", // message type
                 "content": "{\"text\":\"@_user_1 hello\"}", //message content
                 "mentions": [ // information about the mentioned user
                     {
                         "key": "@_user_1",
                         "id": { // User ID information set
                             "union_id": "on_8ed6aa67826108097d9ee143816345",
                             "user_id": "e33ggbyz",
                             "open_id": "ou_84aad35d084aa403a838cf73ee18467"
                         },
                         "name": "Tom", // username
                         "tenant_key": "736588c9260f175e" // The unique identifier of the tenant on Feishu
                     }
                 ]
             }
         }
     }
    ```

2. Identify the event content in the data.

Since there may be more than one subscribed event, it is necessary to identify the event through the characteristics of the **received message** event. According to the [receive message](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/events/receive) event document, get the key features of the event: the event type (`header.event_type`) is `im. message.receive_v1`.

3. Extract the content and ID of the message sent by the user.

- In the received message event parameters, the content sent by the user is included in the `event.message.content` field, which is a serialized JSON string, so it needs to be deserialized before use.

In the follow-up, business processing needs to be performed according to the content sent by the user, and encyclopedia information related to the content needs to be searched.

- In the received message event parameter, the ID of the message sent by the user corresponds to the `event.message.message_id` field.

Then use `message_id` to locate and reply to the user.

4. Perform business logic processing.

For example, based on the content of user messages, search and save matching encyclopedia information on the intranet.

5. Call the [Reply message](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/reply) interface of the development platform, and reply the saved encyclopedia information to the user through `message_id`.

## Sample code

The overall logic is described in JavaScript code as follows.

```JavaScript
const axios = require('axios')
// Assume data is original content of the event body called back
function keywords_search(data) {
    if (data.header.event_type !== 'im.message.receive_v1') {
        return
    }
    // Obtain the message content and message ID
    const {content, message_id} = data.event.message
    // Deserializ, and obtain the content users enter
    const {text} = JSON.parse(content)
    // Process custom business logic
    const resp = await custom_business_logic(text)
    if (resp.code !== 0) {
        return resp
    }
    // Obtain the result
    const result = resp.data
    const card = `{
  "header": {
    "title": {
      "tag": "lark_md",
      "i18n": {
        "en_us": "🔍 Example"
      }
    },
    "template": "blue"
  },
  "i18n_elements": {
    "en_us": [
      {
        "tag": "div",
        "fields": [
          {
            "is_short": false,
            "text": {
              "tag": "lark_md",
              "content": "result",
            }
          }
        ]
      }
    ]
  }
}`
    try {
        const resp = await axios.post("https://open.feishu.cn/open-apis/im/v1/messages/${message_id}/reply", {
            msg_type: "interactive",
            content: "${JSON.stringify({card})}",
        })
        return {
            code: resp.data.code,
            msg: resp.data.msg
        }
    } catch(e) {
        return {
            code: -1,
            msg: "${e}"
        }
    }
}
```