# LINE API

import { Badge } from '@astrojs/starlight/components';

<Aside type="caution" title="/createLineMessage 已弃用">
新的集成应使用 [Messaging API v2](/zh/developer/api-reference/messaging-api-v2/) — 将 `platforms: ["LINE"]` 传递给 `Notify`，并在 `payload.content.localized_content` 中使用 `line` 块。请参阅[迁移指南](/zh/developer/api-reference/messaging-api-v2/migration-from-v1/#from-createlinemessage)。对于富媒体图片/轮播/flex 消息，请在您的 Pushwoosh Control Panel 中将其预配置为 LINE 模板，并通过 `line.template` 引用模板代码。
</Aside>

<Aside> 在发送 LINE 消息之前，请确保已正确配置 Line 平台。[了解更多](/zh/developer/first-steps/connect-messaging-services/line-configuration/) </Aside>

## createLineMessage <Badge text="已弃用" variant="caution" size="small" />

用于向用户发送 LINE 消息

`POST` `https://api.pushwoosh.com/json/1.3/createLineMessage`

### 发送文本消息

仅包含纯文本、不含图片或按钮的简单 LINE 消息。[了解更多](https://developers.line.biz/en/reference/messaging-api/#text-message)

> **提示：** 如需高级格式和富媒体，请使用消息模板，如 [Flex](#send-a-flex-message)、[图片](#send-an-image-message) 或 [轮播](#send-an-image-carousel-message)。

##### 请求正文

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `application` | string | 是 | [Pushwoosh application code](/zh/developer/api-reference/api-identifiers/#application-code) |
| `auth` | string | 是 | 用于验证请求的 [API access token](/zh/developer/api-reference/api-identifiers/#api-access-token)。 |
| `notifications` | array of objects | 是 | 要发送的 LINE 消息对象列表。 |
| `content` | string | 是 | 要发送的 LINE 消息的文本。最大字符限制：5000。<br/><strong>注意：</strong> 如果同时包含 <code>preset</code> 和 <code>content</code>，请求中的值将覆盖 <code>preset</code>。 |
| `preset` | string | 否 | 您在 Pushwoosh Control Panel 中创建的 [LINE 预设](/zh/product/content/line-presets/) 的代码。**注意：** 如果同时包含 `preset` 和 `content`，请求中的值将覆盖 `preset`。 |
| `send_date` | string | 是 | 发送消息的日期和时间。使用格式 `YYYY-MM-DD HH:mm` 或 `now` 立即发送。 |
| `devices` | array of strings | 是 | 要将 LINE 消息发送到的设备代码（user ID）列表。 |

```
{
    "request": {
    "application": "XXXXXX-XXXXXX",
    "auth": "**************************************",
        "notifications": [
            {
                "content": "test",
                "preset": "preset_code",
                "send_date":"now",
                "devices": ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]
            }
        ]
    }
}

```

### 发送图片消息

您可以通过在请求的模板字段中指定一个图片对象，向 LINE 用户发送[图片消息](https://developers.line.biz/en/reference/messaging-api/#image-message)。

每条图片消息需要两个图片 URL：一个用于**全尺寸图片 (image\_url)**，另一个用于**预览 (preview\_image\_url)**。当用户点击预览图时，会打开全尺寸图片。请确保两个 URL 都使用 HTTPS (TLS 1.2 或更高版本) 并链接到可公开访问的图片文件。

有关详细信息，请参阅 LINE Messaging API 文档中的[图片消息部分](https://developers.line.biz/en/reference/messaging-api/#image-message)。

##### 请求正文

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | ----- | ----- | ----- |
| `application` | string | 是 | [Pushwoosh application code](/zh/developer/api-reference/api-identifiers/#application-code) |
| `auth` | string | 是 | 用于验证请求的 [API access token](/zh/developer/api-reference/api-identifiers/#api-access-token)。 |
| `notifications` | array of objects | 是 | 要发送的消息列表。 |
| `content` | string | 是 | 用作消息的后备或预览文本。您在 Pushwoosh Control Panel 中创建的 LINE 预设代码。<br/><strong>注意：</strong> 如果同时包含 <code>preset</code> 和 <code>template</code>，则使用请求中的 <code>template</code>。<br/>如果同时包含 <code>preset</code> 和 <code>content</code>，请求中的 <code>content</code> 将覆盖预设。 |
| `send_date` | string | 是 | 发送消息的日期和时间。使用格式 `YYYY-MM-DD HH:mm` 或 `now` 立即发送。 |
| `devices` | array of strings | 是 | 要将 LINE 消息发送到的设备代码（user ID）列表。 |
| `preset` | string | 否 | 您在 Pushwoosh Control Panel 中创建的 [LINE 预设](/zh/product/content/line-presets/) 的代码。<br/><strong>注意：</strong> 如果请求中同时包含 <code>preset</code> 和 <code>template</code> 参数，<code>template</code> 中的值将覆盖预设中定义的值。<br/>如果同一请求中同时包含 <code>preset</code> 和 <code>content</code>，则直接在请求中提供的 <code>content</code> 将覆盖 <code>preset</code> 中的内容。 |
| `template` | object | 是 | 消息布局模板。支持多种消息类型。详见下文。 |

##### 模板参数

**类型：** image

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `image_url` | string | 是 | 全尺寸图片的 URL（必须使用 HTTPS）。**最大长度：** 2000 个字符。**格式：** JPEG、PNG。**最大大小：** 10 MB。 |
| `preview_image_url` | string | 是 | 聊天中显示的预览图片的 URL（必须使用 HTTPS）。**最大长度：** 2000 个字符。**格式：** JPEG、PNG。**最大大小：** 1 MB。 |

##### 请求示例

```json
{
  "request": {
    "application": "XXXXXX-XXXXXX",
    "auth": "**************************************",
    "notifications": [
      {
        "content": "test",
        "send_date": "now",
        "devices": [
          "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        ],
        "preset": "preset_code",
        "template": {
          "en": {
            "image": {
              "alt_text": "some text",
              "image_url": "https://images.com/1.jpg",
              "preview_image_url": "https://images.com/1.png"
            }
          }
        }
      }
    ]
  }
}


```

### 发送图片轮播消息

图片轮播消息允许您以水平滚动格式显示多张图片。每张图片都显示为一个独立的、不可点击的列，用户可以在 LINE 聊天界面中滑动浏览。

这种格式非常适合以引人入胜的方式展示商品、促销活动或视觉选择。

有关详细信息，请参阅 LINE Messaging API 文档中的[图片轮播模板](https://developers.line.biz/en/reference/messaging-api/#carousel)部分。

##### 请求正文

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `application` | string | 是 | [Pushwoosh application code](/zh/developer/api-reference/api-identifiers/#application-code) |
| `auth` | string | 是 | 用于验证请求的 [API access token](/zh/developer/api-reference/api-identifiers/#api-access-token)。 |
| `notifications` | array of objects | 是 | 要发送的消息列表。 |
| `content` | string | 是 | 用作消息的后备或预览文本。<br/><strong>注意：</strong> 如果同时设置了 <code>content</code> 和 <code>template</code>，则使用 <code>template</code>。<br/>如果同一请求中同时包含 <code>preset</code> 和 <code>content</code>，则直接在请求中提供的 <code>content</code> 将覆盖 <code>preset</code> 中的内容。 |
| `send_date` | string | 是 | 发送消息的日期和时间。使用格式 `YYYY-MM-DD HH:mm` 或 `"now"`。 |
| `devices` | array of strings | 是 | 要将 LINE 消息发送到的设备代码（user ID）列表。 |
| `preset` | string | 否 | 您在 Pushwoosh Control Panel 中创建的 [LINE 预设](/zh/product/content/line-presets/) 的代码。<br/><strong>注意：</strong> 如果请求中同时包含 <code>preset</code> 和 <code>template</code> 参数，<code>template</code> 中的值将覆盖 <code>preset</code> 中定义的值。<br/>如果同一请求中同时包含 <code>preset</code> 和 <code>content</code>，则直接在请求中提供的 <code>content</code> 将覆盖 <code>preset</code> 中的内容。 |
| `template` | object | 是 | 消息布局模板。支持多种消息类型。详见下文。 |



##### 模板参数

**类型：** image\_carousel

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `alt_text` | string | 是 | 在推送预览和不受支持的设备上显示的回退文本。最多 400 个字符。 |
| `columns` | array of objects | 是 | 图片列数组（支持 1-10 个）。每列包含一张图片。 |
| `image_url` | string | 是 | 指向可公开访问的 JPEG 或 PNG 文件的每张轮播列中显示的图片的 URL。必须使用 HTTPS。 |

##### 请求示例

```json
{
  "request": {
    "application": "XXXXXX-XXXXXX",
    "auth": "**************************************",
    "notifications": [
      {
        "content": "test",
        "send_date": "now",
        "devices": [
          "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        ],
        "preset": "preset_code",
        "template": {
          "en": {
            "image_carousel": {
              "alt_text": "some text",
              "columns": [
                { "image_url": "https://images.com/1.jpg" },
                { "image_url": "https://images.com/2.jpg" },
                { "image_url": "https://images.com/3.jpg" }
              ]
            }
          }
        }
      }
    ]
  }
}
```

### 发送 Flex 消息

Flex 消息是一种功能强大、可自定义的消息类型，允许您使用文本、图片、按钮和其他组件创建结构化布局。它们非常适用于收据、产品卡、菜单或任何需要视觉结构化的内容。

要发送 Flex 消息，请在您的请求中包含一个带有 `raw` 负载的 `template` 对象。消息布局必须遵循 [LINE Flex 消息规范](https://developers.line.biz/en/docs/messaging-api/using-flex-messages/)。

**提示：** 您可以使用 [LINE Flex 消息模拟器](https://developers.line.biz/flex-simulator/) 来设计和预览 Flex 消息。

##### 请求正文

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `application` | string | 是 | [Pushwoosh application code](/zh/developer/api-reference/api-identifiers/#application-code) |
| `auth` | string | 是 | 用于验证请求的 [API access token](/zh/developer/api-reference/api-identifiers/#api-access-token)。 |
| `notifications` | array of objects | 是 | 要发送的消息列表。 |
| `content` | string | 是 | 用作消息的后备或预览文本。<br/><strong>注意：</strong> 如果同时设置了 <code>content</code> 和 <code>template</code>，则使用模板。<br/>如果同时包含 <code>preset</code> 和 <code>content</code>，请求中的内容将覆盖预设。 |
| `send_date` | string | 是 | 发送消息的时间。使用 `"now"` 或 `YYYY-MM-DD HH:mm` 格式。 |
| `devices` | array of strings | 是 | 接收消息的 LINE 设备令牌（user ID）列表。 |
| `preset` | string | 否 | 您在 Pushwoosh Control Panel 中创建的 [LINE 预设](/zh/product/content/line-presets/) 的代码。<br/><strong>注意：</strong> 如果同时包含 <code>preset</code> 和 <code>template</code>，模板将覆盖预设。<br/>如果同时包含 <code>preset</code> 和 <code>content</code>，请求中的内容将覆盖预设。 |
| `template` | object | 是 | 消息布局模板。支持多种消息类型。详见下文。 |

##### 模板参数

对于 Flex 消息，请使用原始结构。
类型：raw (Flex)

| 参数 <div style="width:180px"></div>| 类型 <div style="width: 80px"></div>| 必需 | 描述 <div style="width:180px"></div> |
| :---- | :---- | :---- | :---- |
| `alt_text` | string | 是 | 在通知、聊天预览和引用中显示的回退文本。最多 400 个字符。 |
| `content` | object | 是 | Flex 消息布局，根据 LINE 的 Flex 规范，使用 `bubble`、`box`、`text` 和其他组件进行结构化。 |

##### 请求示例

```json
{
  "request": {
    "application": "XXXXXX-XXXXXX",
    "auth": "**************************************",
    "notifications": [
      {
        "content": "test",
        "send_date": "now",
        "devices": ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],
        "preset": "preset_code", 
        "template": {
          "en": {
            "raw": {
              "alt_text": "My raw template",
              "content": {
                "type": "bubble",
                "body": {
                  "type": "box",
                  "layout": "vertical",
                  "contents": [
                    {
                      "type": "text",
                      "text": "RECEIPT",
                      "weight": "bold",
                      "color": "#1DB446",
                      "size": "sm"
                    }
                    // Additional components...
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
```