

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 API 或 CLI 建立對外行銷活動
<a name="create-campaigns-api-cli"></a>

您可以使用 CLI AWS 或 [Amazon Connect Outbound Campaigns API，以程式設計方式建立和管理對外行銷活動](https://docs.aws.amazon.com/connect/latest/APIReference/API_Operations_Amazon_Connect_Outbound_Campaigns_V2.html)。本主題說明如何使用 CLI 建立 Connect Customer 對外行銷活動、定義行銷活動流程，以及參考生命週期命令。

## 先決條件
<a name="create-campaigns-api-cli-prereqs"></a>

使用 API 或 CLI 建立行銷活動之前，請確定您有下列項目：
+ [已啟用外撥通話](enable-outbound-calls.md)的 Connect Customer 執行個體。
+ 針對對外行銷活動設定的 AWS KMS 金鑰。請參閱 [建立 AWS KMS 金鑰](enable-outbound-campaigns.md#create-kms-key-campaigns)。
+ 在您的執行個體上啟用外撥行銷活動。請參閱 [設定對外行銷活動](enable-outbound-campaigns.md#configure-outbound-campaigns)。
+ 行銷活動收件人[的客戶設定檔區段](segmentation-admin-website.md) ARN。
+ 在客服人員中建立的訊息範本可協助知識庫。如需詳細資訊，請參閱[建立訊息範本](https://docs.aws.amazon.com/connect/latest/adminguide/create-message-templates.html)。
+ 已安裝並設定 AWS CLI 第 2 版。如需詳細資訊，請參閱[安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

## 建立行銷活動流程
<a name="create-campaigns-api-cli-flows"></a>

行銷活動流程會定義為每個收件人執行的動作順序。您可以使用 [CreateContactFlow](https://docs.aws.amazon.com/connect/latest/APIReference/API_CreateContactFlow.html) API 建立流程，並將流程類型設定為 `CAMPAIGN`。如需每個動作類型的詳細資訊，請參閱 [旅程流程區塊定義](journey-flow-block-definitions.md)。

### 簡單流程 （不重試）
<a name="create-campaigns-api-cli-flows-simple"></a>

簡單流程會傳送單一通訊給每個收件人，而不檢查交付狀態。這是最簡單的流程結構：

```
{
  "Version": "2019-10-30",
  "StartAction": "SendSMS",
  "Actions": [
    {
      "Identifier": "SendSMS",
      "Type": "SendSMS",
      "Parameters": {
        "Message": {
          "MessageSourceType": "TEMPLATE",
          "TemplatedMessage": {
            "WisdomKnowledgeBaseArn": "arn:aws:wisdom:us-east-1:123456789012:knowledge-base/your-kb-id",
            "WisdomMessageTemplateArn": "arn:aws:wisdom:us-east-1:123456789012:message-template/your-kb-id/your-template-id"
          }
        },
        "SourceEndpoint": {
          "Address": "arn:aws:connect:us-east-1:123456789012:phone-number/your-phone-number-id",
          "Type": "CONNECT_PHONENUMBER_ARN"
        }
      },
      "Transitions": {
        "NextAction": "EndFlow",
        "Conditions": [],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Identifier": "EndFlow",
      "Type": "EndFlowExecution",
      "Parameters": {}
    }
  ]
}
```

### 具有交付狀態檢查和重試的流程
<a name="create-campaigns-api-cli-flows-retry"></a>

對於檢查交付狀態並在失敗時重試的流程，請使用下列結構。流程會傳送通訊、等待交付接收、擷取通訊狀態，然後根據結果進行分支。

重試流程中的關鍵動作為：

1. **SendSMS**、**SendOutboundEmail** 或 **PutDialRequest** - 傳送傳出通訊。

1. **Wait** - 等待交付接收。

1. **GetOutboundCommunicationStatus** - 擷取最新通訊的交付狀態。

1. **比較** - 根據結果評估交付接收和分支 （例如，退信時重試、成功時結束）。

1. **EndFlowExecution** - 終止流程。所有流程路徑必須以此動作結尾。

下列範例顯示行銷活動的流程，該`MANAGED`行銷活動會傳送簡訊、等待交付接收、檢查狀態，並在訊息退信時透過電子郵件重試：

```
{
  "Version": "2019-10-30",
  "StartAction": "SendSMS",
  "Actions": [
    {
      "Identifier": "SendSMS",
      "Type": "SendSMS",
      "Parameters": {
        "Message": {
          "MessageSourceType": "TEMPLATE",
          "TemplatedMessage": {
            "WisdomKnowledgeBaseArn": "arn:aws:wisdom:us-east-1:123456789012:knowledge-base/your-kb-id",
            "WisdomMessageTemplateArn": "arn:aws:wisdom:us-east-1:123456789012:message-template/your-kb-id/your-sms-template-id"
          }
        },
        "SourceEndpoint": {
          "Address": "arn:aws:connect:us-east-1:123456789012:phone-number/your-phone-number-id",
          "Type": "CONNECT_PHONENUMBER_ARN"
        }
      },
      "Transitions": {
        "NextAction": "Wait",
        "Conditions": [],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Identifier": "Wait",
      "Type": "Wait",
      "Parameters": {
        "TimeLimitSeconds": "900"
      },
      "Transitions": {
        "NextAction": "GetOutboundCommunicationStatus",
        "Conditions": [
          {
            "NextAction": "GetOutboundCommunicationStatus",
            "Condition": {
              "Operator": "Equals",
              "Operands": ["WaitCompleted"]
            }
          }
        ],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Identifier": "GetOutboundCommunicationStatus",
      "Type": "GetOutboundCommunicationStatus",
      "Parameters": {
        "OutboundCommunicationIds": ["$.OutboundCommunication.Latest.Id"]
      },
      "Transitions": {
        "NextAction": "Compare",
        "Conditions": [],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Identifier": "Compare",
      "Type": "Compare",
      "Parameters": {
        "ComparisonValue": "$.DeliveryReceipts['`$.OutboundCommunication.Latest.Id`'].Bounce"
      },
      "Transitions": {
        "NextAction": "EndFlow",
        "Conditions": [
          {
            "NextAction": "SendEmail",
            "Condition": {
              "Operator": "Exists",
              "Operands": []
            }
          }
        ],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingCondition"
          }
        ]
      }
    },
    {
      "Identifier": "SendEmail",
      "Type": "SendOutboundEmail",
      "Parameters": {
        "EmailMessage": {
          "MessageSourceType": "TEMPLATE",
          "TemplatedMessage": {
            "WisdomKnowledgeBaseArn": "arn:aws:wisdom:us-east-1:123456789012:knowledge-base/your-kb-id",
            "WisdomMessageTemplateArn": "arn:aws:wisdom:us-east-1:123456789012:message-template/your-kb-id/your-email-template-id"
          }
        },
        "FromEmailAddress": {
          "EmailAddress": "noreply@example.com"
        }
      },
      "Transitions": {
        "NextAction": "EndFlow",
        "Conditions": [],
        "Errors": [
          {
            "NextAction": "EndFlow",
            "ErrorType": "NoMatchingError"
          }
        ]
      }
    },
    {
      "Identifier": "EndFlow",
      "Type": "EndFlowExecution",
      "Parameters": {}
    }
  ]
}
```

**重要**  
如果您的流程使用多種管道類型 （例如，簡訊和電子郵件），請在建立行銷活動時將所有管道包含在 `--channel-subtype-config` 參數中。
對於用於檢查交付狀態類型`MANAGED`之行銷活動的流程，所需的動作序列為：**等待** → **GetOutboundCommunicationStatus** → **比較**。`Wait` 動作會在指定的持續時間內暫停流程，讓交付接收有時間到達。`GetOutboundCommunicationStatus` 動作會擷取交付狀態。根據結果`Compare`的動作分支。
中的 `OutboundCommunicationIds` 參數`GetOutboundCommunicationStatus`必須參考 `$.OutboundCommunication.Latest.Id`。`Compare` 動作中的交付接收參考必須使用相同的金鑰。例如：`$.DeliveryReceipts['`$.OutboundCommunication.Latest.Id`'].Bounce`。
`MANAGED` 使用語音頻道的行銷活動需要 `PutDialRequest`動作`dialCriteriaRules`：  

  ```
  {
    "Identifier": "PutDialRequest",
    "Type": "PutDialRequest",
    "Parameters": {
      "dialCriteriaRules": [
        {
          "type": "CheckSegmentMembershipForCustomerProfile",
          "segmentArn": "arn:aws:profile:us-east-1:123456789012:domains/your-domain/segments/your-segment"
        }
      ]
    },
    ...
  }
  ```

## 建立行銷活動流程版本
<a name="create-campaigns-api-cli-version"></a>

建立行銷活動流程之後，您必須建立流程的版本。建立行銷活動時，需要版本控制的流程 ARN。使用 [CreateContactFlowVersion](https://docs.aws.amazon.com/connect/latest/APIReference/API_CreateContactFlowVersion.html) API 建立版本。

版本化流程 ARN 包含版本尾碼。例如：`arn:aws:connect:us-east-1:123456789012:instance/your-instance-id/contact-flow/your-flow-id:1`

如需詳細資訊，請參閱 [create-contact-flow-version CLI 參考](https://docs.aws.amazon.com/cli/latest/reference/connect/create-contact-flow-version.html)。

## 建立對外行銷活動
<a name="create-campaigns-api-cli-create"></a>

使用 `create-campaign`命令來建立行銷活動。下列範例會使用無代理程式傳出模式建立 SMS 行銷活動：

```
aws connectcampaignsv2 create-campaign \
  --name "My SMS Campaign" \
  --connect-instance-id "your-instance-id" \
  --type MANAGED \
  --connect-campaign-flow-arn "arn:aws:connect:us-east-1:123456789012:instance/your-instance-id/contact-flow/your-flow-id:1" \
  --source '{"customerProfilesSegmentArn": "arn:aws:profile:us-east-1:123456789012:domains/your-domain/segments/your-segment"}' \
  --channel-subtype-config '{
    "sms": {
      "outboundMode": {"agentless": {}},
      "defaultOutboundConfig": {
        "connectSourcePhoneNumberArn": "arn:aws:connect:us-east-1:123456789012:phone-number/your-phone-number-id",
        "wisdomTemplateArn": "arn:aws:wisdom:us-east-1:123456789012:message-template/your-kb-id/your-template-id"
      }
    }
  }' \
  --schedule '{"startTime": "2026-07-01T09:00:00", "endTime": "2026-07-01T17:00:00", "refreshFrequency": "PT30M"}' \
  --communication-time-config '{
    "localTimeZoneConfig": {"defaultTimeZone": "America/New_York"},
    "sms": {
      "openHours": {
        "dailyHours": {
          "MONDAY": [{"startTime": "T09:00", "endTime": "T17:00"}],
          "TUESDAY": [{"startTime": "T09:00", "endTime": "T17:00"}],
          "WEDNESDAY": [{"startTime": "T09:00", "endTime": "T17:00"}],
          "THURSDAY": [{"startTime": "T09:00", "endTime": "T17:00"}],
          "FRIDAY": [{"startTime": "T09:00", "endTime": "T17:00"}]
        }
      }
    }
  }' \
  --region us-east-1
```

成功時，命令會傳回行銷活動 ID 和 ARN：

```
{
  "id": "campaign-id",
  "arn": "arn:aws:connect-campaigns:us-east-1:123456789012:campaign/campaign-id"
}
```

**注意**  
`--type` 參數指定行銷活動類型。將 `MANAGED`用於對外行銷活動。使用 `JOURNEY`進行多步驟、多頻道的旅程，請參閱 [視覺化旅程建置器](create-a-multi-step-and-multi-channel-journey.md)。

**注意**  
您也可以指定 `--communication-limits-override`來控制可以聯絡收件人的次數。如需參數的完整清單，請參閱 [create-campaign CLI 參考](https://docs.aws.amazon.com/cli/latest/reference/connectcampaignsv2/create-campaign.html)。

若要管理行銷活動生命週期操作 （開始、停止、暫停、繼續、刪除），請參閱 [AWS connectcampaignsv2 的 CLI 參考](https://docs.aws.amazon.com/cli/latest/reference/connectcampaignsv2/index.html)。