消息服务 API v1 用例指南
向买家发送有关其订单的消息
什么是消息服务 API?
The Selling Partner API for Messaging (Messaging API) allows you to create applications that enable sellers to send messages to buyers after they place an order. Sellers can send a variety of message types. These include asking order related questions, arranging a delivery, and more. The Messaging API can send messages to a buyer, even if the buyer has not contacted the seller first. However, you cannot use this API to respond to messages from buyers.
Refer to the Messaging API reference for a list of operations that correspond to the available message types.
术语
Amazon S3 presigned URL: A URL for an Amazon S3 bucket to which you can upload an object without AWS security credentials or permissions. You get an Amazon S3 presigned URL in Step 7. Create an upload destination.
消息可用性
并非所有消息类型在所有情况下都可用。有许多因素会影响消息的可用性,包括订单是否已配送、卖家类型或已为订单发送的消息数量。
发送消息的工作流程
以下内容适用于该应用程序的用户。发送消息是一个多步骤的过程,包括调用消息 API 和上传 API 中的操作。以下是发送消息的基本工作流程:
-
您的应用程序会显示卖家最近的订单,卖家可选择
要向买家发送信息的订单。 -
Your application displays the message types that are available for the order that the seller chooses. Get the available message types by calling the
getMessagingActionsForOrder
operation of the Messaging API. -
卖家为订单选择消息类型。
-
如果消息类型支持自定义消息,则卖家会输入自定义消息。
-
如果消息类型支持附件,卖家会将附件上传到您的应用程序。卖家还要输入附件的文件名。
-
如果卖家将附件上传到您的应用程序,则您的应用程序会通过执行以下操作将附件上传到 Amazon S3 存储桶:
-
计算附件的 Content-MD5 哈希值,这是创建上传目标位置所必需的。
-
通过调用上传 API 的
createUploadDestination
操作创建上传目标位置。 -
将附件上传到目标位置。
-
-
您的应用程序通过调用与卖家在步骤 #3 中选择的消息类型对应的操作,请求卖家向买家发送消息。例如,您的应用程序调用
createConfirmOrderDetails
如果卖家选择 “确认订单详情” 消息类型,则进行操作。 -
Amazon emails a message from the seller to the buyer. Refer to Tutorial: Send a message for the detailed workflow.
教程:发送消息
本教程教您如何制作允许卖家向买家发送消息的应用程序。
先决条件
要完成本教程,您需要:
-
Authorization from the seller for whom you are making calls. Refer to Authorizing Selling Partner API applications for more information.
-
Buyer Communication role approval in your developer profile.
-
The Buyer Communication role selected in the App registration page for your application.
步骤 1. 获取卖家想要为其发送消息的订单
-
显示卖家最近的订单列表,供卖家选择。
-
卖家选择要向买家发送消息的订单。
-
将亚马逊订单编号与卖家选择的订单相关联。
-
Save the Amazon order identifier for Step 2. Get a list of available message types.
步骤 2. 获取可用消息类型的列表
Call the getMessagingActionsForOrder
operation of the Messaging API, specifying the Amazon order identifier from
Step 1. Get the order for which the seller wants to send a message to get a list of message types that are available for the order.
- Call the
getMessagingActionsForOrder
operation of the Messaging API, passing the following parameters:
路径参数:
参数 | 描述 | 必填项 |
---|---|---|
amazonOrderId
|
An Amazon order identifier. This specifies the order for which a message is to be sent. Get this value from Step 1. Get the order for which the seller wants to send a message.
类型:字符串 |
有帮助 |
查询参数:
参数 | 描述 | 必填项 |
---|---|---|
marketplaceIds
|
商城标识符。指定下订单的商城。您只能指定一个商城。 类型:array[string] |
有帮助 |
请求示例
GET https://sellingpartnerapi-na.amazon.com/messaging/v1/orders/902-6786204-8179805?marketplaceIds=ATVPDKIKX0DER
响应
getMessagingActionsForOrder
操作的响应架构。
_links
名称 | 描述 | Schema |
---|---|---|
self required | 对应于 IANA 注册的“self”关系,其目标是资源的 URI。 | LinkObject |
actions required | 针对指定亚马逊订单编号的合格操作。 | < LinkObject > array |
名称 | Schema |
---|---|
actions required | < GetMessagingActionResponse > array |
响应示例
{
"_links":
{
"actions": [
{
"href": "/messaging/v1/orders/902-6786204-
8179805/messages/confirmCustomizationDetails?marketplaceIds=ATVPDKIKX0
DER",
"name": "confirmCustomizationDetails"
},
{
"href": "/messaging/v1/orders/902-6786204-
8179805/messages/negativeFeedbackRemoval?marketplaceIds=ATVPDKIKX0DER"
,
"name": "negativeFeedbackRemoval"
},
{
"href": "/messaging/v1/orders/902-6786204-
8179805/messages/confirmOrderDetails?marketplaceIds=ATVPDKIKX0DER",
"name": "confirmOrderDetails"
},
],
"self":
{
"href": "/messaging/v1/orders/902-6786204-
8179805?marketplaceIds=ATVPDKIKX0DER"
}
},
"_embedded":
{
"actions": [
{
"_links":
{
"schema":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmCustomizationDetails/schema",
"name": "confirmCustomizationDetails"
},
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmCustomizationDetails?marketplaceIds=ATVPDKIKX0DER",
"name": "confirmCustomizationDetails"
}
},
"_embedded":
{
"schema":
{
"_links":
{
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmCustomizationDetails/schema",
"name": "confirmCustomizationDetails"
}
},
"type": "object",
"name": "confirmCustomizationDetails",
"title": "Confirm customization details",
"description": "Ask your customer to provide details or verify the customization input provided (name spelling, imagery, initials).",
"properties":
{
"attachments":
{
"type": "array",
"items":
{
"type": "object",
"required": [],
"properties":
{
"fileName":
{
"type": "string"
},
"id":
{
"type": "string"
}
}
},
"title": "Add attachment",
"description": "You can upload text files, PDFs, Word documents, and these image file types: .jpg, .gif, and .png. The total size of attachments must be less than 10 MB.",
"x-ui-field-type": "attachments",
"maxItems": 5
},
"rawMessageBody":
{
"type": "string",
"title": "Explain why you are contacting and any action you need your customer to take. ",
"description": "800 character limit. Only links related to order completion are allowed, no HTML or email addresses.",
"x-ui-field-type": "text",
"minLength": 1,
"maxLength": 800
}
},
"required": [
"attachments",
"rawMessageBody"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"x-ui-hidden": null
}
},
"name": "confirmCustomizationDetails",
"title": "Confirm customization details"
},
{
"_links":
{
"schema":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/negativeFeedbackRemoval/schema",
"name": "negativeFeedbackRemoval"
},
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/negativeFeedbackRemoval?marketplaceIds=ATVPDKIKX0DER",
"name": "negativeFeedbackRemoval"
}
},
"_embedded":
{
"schema":
{
"_links":
{
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/negativeFeedbackRemoval/schema",
"name": "negativeFeedbackRemoval"
}
},
"type": "object",
"name": "negativeFeedbackRemoval",
"title": "Request to update negative feedback",
"description": "Ask your customer to consider updating their seller feedback rating. You may only send this once per order.",
"properties": {},
"required": [],
"$schema": "http://json-schema.org/draft-04/schema#",
"x-ui-hidden": true
}
},
"name": "negativeFeedbackRemoval",
"title": "Request to update negative feedback"
},
{
"_links":
{
"schema":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmOrderDetails/schema",
"name": "confirmOrderDetails"
},
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmOrderDetails?marketplaceIds=ATVPDKIKX0DER",
"name": "confirmOrderDetails"
}
},
"_embedded":
{
"schema":
{
"_links":
{
"self":
{
"href": "/messaging/v1/orders/902-6786204-8179805/messages/confirmOrderDetails/schema",
"name": "confirmOrderDetails"
}
},
"type": "object",
"name": "confirmOrderDetails",
"title": "Confirm order details",
"description": "Ask your customer a specific order-related question prior to shipping their order.",
"properties":
{
"rawMessageBody":
{
"type": "string",
"title": "Explain why you are contacting and any action you need your customer to take. ",
"description": "2000 character limit. Only links related to order completion are allowed, no HTML or email addresses.",
"x-ui-field-type": "text",
"minLength": 1,
"maxLength": 2000
}
},
"required": [
"rawMessageBody"
],
"$schema": "http://json-schema.org/draft-04/schema#",
"x-ui-hidden": null
}
},
"name": "confirmOrderDetails",
"title": "Confirm order details"
},
]
}
}
-
Save the list of schemas in the
_embedded.actions
array of the response. In the_embedded.schema
section of each array element you can find a schema for each available message type. Use thetitle
value in each schema to create a list of available message types for the seller to choose from in Step 3. Get the seller's choice of message type. Use the schemas to understand how to display and enforce the restrictions for custom messages and attachments that apply for each message type. For more information, refer to Step 4. Get custom message text from the seller (if supported) and Step 5. Get an attachment from the seller (if supported). -
Save the list of paths in the
_links.actions
array of the response. Thehref
value in each array element contains a path for each available message type. You can use one of these paths in a call for the appropriate operation in Step 9. Send a message to the buyer.
步骤 3. 获取卖家选择的消息类型
-
Display a list of available message types for the seller to choose from. Get these from the
title
values that you saved in Step 2. Get a list of available message types. -
让卖家选择他们想要发送的消息类型并保存。此选择将影响后续的几个步骤,包括:
-
Step 4. Get custom message text from the seller (if supported). The choice of message type determines whether a custom message is supported.
-
Step 5. Get an attachment from the seller (if supported). The choice of message type determines whether attachments are supported.
-
Step 9. Send a message to the buyer. The choice of message type determines which operation you will call in Step 9. For example if the seller chooses the "Confirm customization details" message type, you will call the
confirmCustomizationDetails
operation in Step 9.
-
步骤 4. 从卖家那里获取自定义消息文本(如果支持)
某些消息类型允许卖家在给买家的电子邮件中包含自定义消息。要了解消息类型是否支持自定义消息,请查看消息服务 API 参考,查找与消息类型相对应的操作。例如,createConfirmServiceDetails
操作对应于“联系家庭服务客户”消息类型。如果操作包含文本正文参数,则该消息类型支持自定义消息。
-
Determine if the message type that the seller chose in Step 3. Get the seller's choice of message type supports custom messages. If custom messages are not supported, go directly to Step 5. Get an attachment from the seller (if supported). If custom messages are supported, continue with this procedure.
-
让卖家在自定义消息中输入所需的原始文本。
-
Display and enforce any restrictions on the message text that the seller inputs. You can find the text restrictions in the
_embedded.schema.properties.rawMessageBody
section of the_embedded.actions
array element that corresponds to the seller's message type. Find this in the response for thegetMessagingActionsForOrder
operation in Step 2. Get a list of available message types. -
Save the message text and use it as input for Step 9. Send a message to the buyer.
步骤 5. 从卖家那里获取附件(如果支持)
某些消息类型允许卖家在给买家的电子邮件中包含附件链接。要了解消息类型是否支持附件,请查看消息服务 API 参考,查找与该消息类型相对应的操作。例如,createWarranty
操作对应于“发送保修信息”消息类型。如果操作包含附件正文参数,则该消息类型支持附件。
-
Determine if the message type that the seller chose in Step 3. Get the seller's choice of message type supports attachments. If attachments are not supported, go directly to Step 9. Send a message to the buyer. If attachments are supported, continue with this procedure.
-
让卖家将附件上传到您的应用程序。
-
Display and enforce any restrictions on the attachment that the seller uploads. You can find the attachment restrictions in the
_embedded.schema.properties.attachments
section of the_embedded.actions
array element that corresponds to the seller's message type. Find this in the response for thegetMessagingActionsForOrder
operation in Step 2. Get a list of available message types. -
Convert the attachment into an input stream and save it for Step 8. Upload the attachment.
-
让卖家输入附件的文件名,包括文件扩展名。
-
Save the file name and use it as input for Step 9. Send a message to the buyer.
步骤 6. 计算 Content-MD5 哈希值
Calculate a Content-MD5 hash for the attachment from Step 5. Get an attachment from the seller(if supported). You will need it to create an upload destination in Step 7. Create an upload destination.
此步骤中的 Java 示例代码包含用于计算 Content-MD5 哈希值的逻辑。此示例代码使用 Apache HTTP 客户端。
- 使用以下内容作为示例代码的输入:
The attachment input stream from Step 5. Get an attachment from the seller (if supported), is the argument for the fis
parameter of the computeContentMD5Value
method of the CreateMD5
class.
- Save the
md5Content
value to pass in with theContent-MD5
parameter in Step 7. Create an upload destination.
Java 代码示例
package io.swagger.client.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CreateMD5
{
public static String computeContentMD5Value(FileInputStream fis)
throws IOException,
NoSuchAlgorithmException
{
DigestInputStream dis = new DigestInputStream(fis,
MessageDigest.getInstance("MD5"));
byte[]buffer = new byte[8192];
while (dis.read(buffer) > 0);
String md5Content = new String(org.apache.commons.codec.binary.Base64
.encodeBase64(dis.getMessageDigest().digest()));
// Effectively resets the stream to be beginning of the file
// via a FileChannel.
fis.getChannel().position(0);
return md5Content;
}
}
步骤 7. 创建上传目标位置
Call the createUploadDestination
operation of the Uploads API to create an upload destination for your attachment, in the form of an Amazon S3 presigned URL. For definitions, refer to Terminology.
- Call the
createUploadDestination
operation of the Uploads API, passing the following parameters:
查询参数:
Type | 名称 | 描述 | Schema |
---|---|---|---|
Query | marketplaceIds required | 商城标识符列表。这指定了可以上传的商城。只能指定一个商城。 最大数量 : 1 | < string > 数组 |
Query | contentmd5 required | The Content-MD5 hash that you calculated in Step 6. Calculate a Content MD5 hash. | string |
Path | 资源 required | 您正在创建的上传目标的资源。例如,如果您要为 Messaging API 的 createLegalDisclosure 操作创建上传目的地, {resource} 会是 /messaging/v1/orders/{amazonOrderId}/messages/legalDisclosure ,整条路径将是 /uploads/2020-11-01/uploadDestinations/messaging/v1/orders/{amazonOrderId}/messages/legalDisclosure 。如果您要为 Aplus 内容文档创建上传目的地, {resource} 会是 aplus/2020-11-01/contentDocuments 而路径将是 /uploads/v1/uploadDestinations/aplus/2020-11-01/contentDocuments 。 | string |
Query | contentType optional | 要上传的文件的内容类型。 | string |
请求示例
POST https://sellingpartnerapi-na.amazon.com/uploads/2020-11-01/uploadDestinations/messaging/v1/orders/7XX-5XXXXX-8XXXXXX/messages/invoice?marketplaceIds=A2Q3Y263D00KWC&contentMD5=qCcR3o%2BU0FUR1%2BgXhApNfQ%3D%3D
响应
createUploadDestination
操作的响应架构。
名称 | 描述 | Schema |
---|---|---|
payload optional | 有关上传目标位置的信息。 | UploadDestination |
errors optional | 请求失败时返回的错误响应的列表。 | ErrorList |
响应示例
{
"payload":
{
"uploadDestinationId": "sc/7ae2d3b1-fdd3-42c4-98c4-9cc509fb95d8.png",
"url": "https://aplus-media.s3.amazonaws.com/?x-amz-date=20201116T184623Z&x-amz-signature=c5c8efd95d883b6787a2b1a93c7c066f01cb4e8d7be3ece4360aa800332e0cf9&x-amz-meta-owner=A2CZ04NGKYDXDV&acl=private&key=sc/7ae2d3b1-fdd3-42c4-98c4-9cc509fb95d8.png&x-amz-algorithm=AWS4-HMAC-SHA256&policy=eyJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJhcGx1cy1tZWRpYS1iZXRhIn0seyJrZXkiOiJzb3RhLzdhZTJkM2IxLWZkZDMtNDJjNC05OGM0LTljYzUwOWZiOTVkOC5wbmcifSx7ImFjbCI6InByaXZhdGUifSx7IngtYW16LW1ldGEtb3duZXIiOiJBMkNaMDROR0tZRFhEViJ9LHsieC1hbXotYWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUE2TDZSN1FFNTZGNkdNRzVFLzIwMjAxMTE2L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWRhdGUiOiIyMDIwMTExNlQxODQ2MjNaIn0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMSwzMTQ1NzI4XV0sImV4cGlyYXRpb24iOiIyMDIwLTExLTE2VDIxOjQ2OjIzLjg2OFoifQ==&x-amz-credential=AKIA6L6R7QE56F6GMG5E/20201116/us-east-1/s3/aws4_request",
"headers":
{
"Content-MD5": "1jnGuPv7xwPiat6NesKL/w=="
}
}
}
- 保存以下值:
- url and headers. Use these values in Step 8. Upload the attachment.
uploadDestinationId
. Use this value in Step 9. Send a message to the buyer.
步骤 8. 上传附件
Upload to an Amazon S3 bucket the attachment that the seller uploaded to your application in Step 5. Get an attachment from the seller (if supported).
此步骤中的 Java 示例代码包含上传附件的逻辑。此示例代码使用 Apache HTTP 客户端。
使用以下内容作为示例代码的输入:
-
Use the url value that you saved in Step 7. Create an upload destination as the argument for
URL(uploadDestinationPayload.getPayload().getUrl())
in theUploadFileToDestination
class. -
Use the headers value that you saved in Step 7. Create an upload destination as input for
(Map < String, String > )uploadDestinationPayload.getPayload().getHeaders()
in theUploadFileToDestination
class. -
Use the attachment input stream that you saved in Step 4. Get custom message text from the seller (if supported) and use it as input for the
out.write()
method of theUploadFileToDestination
class.
Java 代码示例
package io.swagger.client.util;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import io.swagger.client.model.CreateUploadDestinationResponse;
public class UploadFileToDestination
{
private static String requestMethod = "PUT";
private static String contentType = "Content-Type";
private static String contentValue = "application/text";
private static URL url;
private static Map < String,
String > headers;
/* Upload file to destination using the response returned in
CreateUploadDestination. Reference https://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObjectJavaSDK.html)
*/
public static void uploadFileInDestination(
CreateUploadDestinationResponse uploadDestinationPayload)
throws IOException
{
url = new URL(uploadDestinationPayload.getPayload().getUrl());
headers = (Map < String, String > )uploadDestinationPayload.getPayload()
.getHeaders();
// Create the connection and use it to upload the new object using the
// pre-signed URL.
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod(requestMethod);
connection.setRequestProperty(contentType, contentValue);
// headers that are returned in createDestination call
for (Map.Entry < String, String > headersEntry: headers.entrySet())
{
connection.setRequestProperty(headersEntry.getKey(),
headersEntry.getValue());
}
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("Text of the file");
out.close();
// Check the HTTP response code. To complete the upload and make the
// object available,
// you must interact with the connection object in some way.
connection.getResponseCode();
System.out.println("HTTP response code: " + connection.getResponseCode()
+ connection.getResponseMessage());
connection.disconnect();
}
}
附件已上传到 Amazon S3 存储桶。
步骤 9. 向买家发送消息
To send a message from the seller to the buyer you need to call the operation that corresponds to the message type that the seller chose in Step 2. Get a list of available message types. For example, if the seller chose the "Confirm customization details" message type, you would call the confirmCustomizationDetails
operation. Refer to the Messaging API reference for the list of operations that are available. In this example, we will call the confirmCustomizationDetails
operation.
- 调用消息服务 API 的
confirmCustomizationDetails
操作,传递以下参数:
路径参数:
参数 | 描述 | 必填项 |
---|---|---|
amazonOrderId
|
An Amazon order identifier. This specifies the order for which a message is to be sent. Use the Amazon order identifier that you saved in Step 1. Get the order for which the seller wants to send a message.
类型:字符串 |
有帮助 |
查询参数:
参数 | 描述 | 必填项 |
---|---|---|
marketplaceIds
|
商城标识符。指定下订单的商城。您只能指定一个商城。 类型:array[string] |
有帮助 |
主体参数:
参数 | 描述 | 必填项 |
---|---|---|
text
|
The custom message to be included in the email to the buyer. Use the message text from Step 4. Get custom message text from the seller (if supported).
类型:字符串 |
有帮助 |
attachments
|
要包含在发送给买家的消息中的附件
|
有帮助 |
请求示例
POST https://sellingpartnerapi-na.amazon.com/messaging/v1/orders/902-6786204-8179805/messages/confirmCustomizationDetails?marketplaceIds=ATVPDKIKX0DER
{
"text": "This is the message from the seller to the buyer that will appear in the email to the buyer.",
"attachments": [
{
"uploadDestinationId": "4370d51b-2954-445c-9f30-EXAMPLE6b9de",
"fileName": "example.txt"
}
]
}
响应
没有回应。相反,亚马逊会通过电子邮件将卖家的消息发送给买家。
2 个月前更新