Package com.bytedesk.social.meta
Class MetaMessageService
java.lang.Object
com.bytedesk.social.meta.MetaMessageService
Meta平台消息服务
用于发送各种类型的消息到Meta平台
限制:
标准消息时间范围指的是 24 小时的时段,在此期间,您可向用户发送消息
消息模板:
https://developers.facebook.com/docs/messenger-platform/send-messages/templates
https://developers.facebook.com/docs/messenger-platform/send-messages
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanisValidFacebookUrl(String url) 验证是否为有效的Facebook网址prepareMessageRequest(String pageId, String recipientId, String pageAccessToken) 发送消息前的通用准备voidsendAttachmentConfirmationTemplate(String pageId, String recipientId, String attachmentUrl, String pageAccessToken) 发送附件确认模板消息(测试功能) 向用户发送一个带有确认按钮的模板消息,询问用户是否确认附件booleansendAudioMessage(String pageId, String recipientId, String audioUrl, String pageAccessToken) 发送音频消息booleansendButtonTemplateMessage(String pageId, String recipientId, String text, List<Map<String, Object>> buttons, String pageAccessToken) 发送按钮模板消息 https://developers.facebook.com/docs/messenger-platform/reference/templates/buttonbooleansendCouponTemplateMessage(String pageId, String recipientId, String title, String couponCode, String subtitle, String couponUrl, String couponUrlButtonTitle, String couponPreMessage, String imageUrl, String payload, String pageAccessToken) 发送优惠券模板消息booleansendCustomerFeedbackTemplateMessage(String pageId, String recipientId, String title, String subtitle, String buttonTitle, List<Map<String, Object>> feedbackScreens, String privacyUrl, Integer expiresInDays, String pageAccessToken) 发送客户反馈模板消息booleansendFileMessage(String pageId, String recipientId, String fileUrl, String pageAccessToken) 发送文件消息booleansendGenericTemplateMessage(String pageId, String recipientId, List<Map<String, Object>> elements, String pageAccessToken) 发送常规模板消息booleansendImageMessage(String pageId, String recipientId, String imageUrl, String pageAccessToken) 发送图片消息booleansendMarkSeenAction(String pageId, String recipientId, String pageAccessToken) 发送已读状态booleansendMediaTemplateMessageWithAttachmentId(String pageId, String recipientId, String mediaType, String attachmentId, List<Map<String, Object>> buttons, String pageAccessToken) 发送媒体模板消息booleansendMediaTemplateMessageWithUrl(String pageId, String recipientId, String mediaType, String facebookUrl, List<Map<String, Object>> buttons, String pageAccessToken) 发送媒体模板消息(使用Facebook URL)booleansendMessage(String pageId, String recipientId, String messageType, Object messageContent, String pageAccessToken) 发送消息的通用方法booleansendMessageWithFallback(String pageId, String recipientId, String messageType, Object messageContent, String pageAccessToken, String fallbackMessage) 发送消息并处理错误booleansendMultipleImagesMessage(String pageId, String recipientId, List<String> imageUrls, String pageAccessToken) 发送多张图片消息booleansendNotificationMessage(String pageId, String notificationMessagesToken, Object messageContent, String pageAccessToken) 发送营销消息booleansendNotificationMessagesSubscription(String pageId, String recipientId, String title, String imageUrl, String payload, String timezone, String pageAccessToken) 发送营销消息订阅请求booleansendProductTemplateMessage(String pageId, String recipientId, List<String> productIds, String pageAccessToken) 发送商品模板消息booleansendReceiptTemplateMessage(String pageId, String recipientId, String recipientName, String orderNumber, String currency, String paymentMethod, String orderUrl, String timestamp, Map<String, String> address, Map<String, Object> summary, List<Map<String, Object>> adjustments, List<Map<String, Object>> elements, String pageAccessToken) 发送回执模板消息private boolean发送请求到Meta平台booleansendStructuredInformationTemplateMessage(String pageId, String recipientId, List<String> countries, String privacyUrl, Integer expiresInDays, List<Map<String, Object>> addressOverrides, List<Map<String, Object>> contactOverrides, String purpose, String pageAccessToken) 发送结构化信息模板消息booleansendTemplateMessage(String pageId, String recipientId, String templateType, Map<String, Object> templatePayload, String pageAccessToken, String messageTag) 发送通用模板消息booleansendTextMessage(String pageId, String recipientId, String text, String pageAccessToken) 发送基础文本消息booleansendTextMessageWithQuickReplies(String pageId, String recipientId, String text, List<Map<String, Object>> quickReplies, String pageAccessToken) 发送带快速回复的文本消息booleansendTypingOnAction(String pageId, String recipientId, String pageAccessToken) 发送输入中状态booleansendVideoMessage(String pageId, String recipientId, String videoUrl, String pageAccessToken) 发送视频消息booleansetGetStartedButton(String pageId, String pageAccessToken) 设置"开始"按钮 这是使用固定菜单的必需条件booleansetPageLevelPersistentMenu(String pageId, List<Map<String, Object>> persistentMenu, String pageAccessToken) 设置页面级固定菜单booleansetUserLevelPersistentMenu(String pageId, String psid, List<Map<String, Object>> persistentMenu, String pageAccessToken) 设置用户级固定菜单private booleanvalidateMessageParams(String pageId, String recipientId, String pageAccessToken) 发送消息前的通用验证
-
Field Details
-
restTemplate
private final org.springframework.web.client.RestTemplate restTemplate -
GRAPH_API_BASE_URL
- See Also:
-
API_VERSION
- See Also:
-
-
Constructor Details
-
MetaMessageService
public MetaMessageService()
-
-
Method Details
-
sendTextMessage
public boolean sendTextMessage(String pageId, String recipientId, String text, String pageAccessToken) 发送基础文本消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDtext- 消息文本pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendImageMessage
public boolean sendImageMessage(String pageId, String recipientId, String imageUrl, String pageAccessToken) 发送图片消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDimageUrl- 图片URLpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendMultipleImagesMessage
public boolean sendMultipleImagesMessage(String pageId, String recipientId, List<String> imageUrls, String pageAccessToken) 发送多张图片消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDimageUrls- 图片URL列表pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendAudioMessage
public boolean sendAudioMessage(String pageId, String recipientId, String audioUrl, String pageAccessToken) 发送音频消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDaudioUrl- 音频URLpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendVideoMessage
public boolean sendVideoMessage(String pageId, String recipientId, String videoUrl, String pageAccessToken) 发送视频消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDvideoUrl- 视频URLpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendFileMessage
public boolean sendFileMessage(String pageId, String recipientId, String fileUrl, String pageAccessToken) 发送文件消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDfileUrl- 文件URLpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendButtonTemplateMessage
public boolean sendButtonTemplateMessage(String pageId, String recipientId, String text, List<Map<String, Object>> buttons, String pageAccessToken) 发送按钮模板消息 https://developers.facebook.com/docs/messenger-platform/reference/templates/button- Parameters:
pageId- 页面IDrecipientId- 接收者IDtext- 消息文本buttons- 按钮列表pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendTypingOnAction
发送输入中状态- Parameters:
pageId- 页面IDrecipientId- 接收者IDpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendMarkSeenAction
发送已读状态- Parameters:
pageId- 页面IDrecipientId- 接收者IDpageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendRequest
发送请求到Meta平台- Parameters:
url- API端点URLrequestBody- 请求体recipientId- 接收者ID- Returns:
- 发送结果
-
validateMessageParams
发送消息前的通用验证- Parameters:
pageId- 页面IDrecipientId- 接收者IDpageAccessToken- 页面访问令牌- Returns:
- 验证结果
-
prepareMessageRequest
private Map.Entry<String,Map<String, prepareMessageRequestObject>> (String pageId, String recipientId, String pageAccessToken) 发送消息前的通用准备- Parameters:
pageId- 页面IDrecipientId- 接收者IDpageAccessToken- 页面访问令牌- Returns:
- 请求URL和请求体
-
sendMessage
public boolean sendMessage(String pageId, String recipientId, String messageType, Object messageContent, String pageAccessToken) 发送消息的通用方法- Parameters:
pageId- 页面IDrecipientId- 接收者IDmessageType- 消息类型messageContent- 消息内容pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendMessageWithFallback
public boolean sendMessageWithFallback(String pageId, String recipientId, String messageType, Object messageContent, String pageAccessToken, String fallbackMessage) 发送消息并处理错误- Parameters:
pageId- 页面IDrecipientId- 接收者IDmessageType- 消息类型messageContent- 消息内容pageAccessToken- 页面访问令牌fallbackMessage- 失败时的备用消息- Returns:
- 发送结果
-
sendCouponTemplateMessage
public boolean sendCouponTemplateMessage(String pageId, String recipientId, String title, String couponCode, String subtitle, String couponUrl, String couponUrlButtonTitle, String couponPreMessage, String imageUrl, String payload, String pageAccessToken) 发送优惠券模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDtitle- 优惠券标题couponCode- 优惠券代码subtitle- 优惠券副标题couponUrl- 优惠券URLcouponUrlButtonTitle- 优惠券按钮标题couponPreMessage- 优惠券前置消息imageUrl- 图片URLpayload- 额外信息pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendCustomerFeedbackTemplateMessage
public boolean sendCustomerFeedbackTemplateMessage(String pageId, String recipientId, String title, String subtitle, String buttonTitle, List<Map<String, Object>> feedbackScreens, String privacyUrl, Integer expiresInDays, String pageAccessToken) 发送客户反馈模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDtitle- 模板标题subtitle- 模板副标题buttonTitle- 按钮标题feedbackScreens- 反馈屏幕配置privacyUrl- 隐私政策URLexpiresInDays- 过期天数(1-7)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendTemplateMessage
public boolean sendTemplateMessage(String pageId, String recipientId, String templateType, Map<String, Object> templatePayload, String pageAccessToken, String messageTag) 发送通用模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDtemplateType- 模板类型templatePayload- 模板负载pageAccessToken- 页面访问令牌messageTag- 消息标签(可选)- Returns:
- 发送结果
-
sendGenericTemplateMessage
public boolean sendGenericTemplateMessage(String pageId, String recipientId, List<Map<String, Object>> elements, String pageAccessToken) 发送常规模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDelements- 模板元素列表(最多10个)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendMediaTemplateMessageWithAttachmentId
public boolean sendMediaTemplateMessageWithAttachmentId(String pageId, String recipientId, String mediaType, String attachmentId, List<Map<String, Object>> buttons, String pageAccessToken) 发送媒体模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDmediaType- 媒体类型(image|video)attachmentId- 附件IDbuttons- 按钮列表(最多3个)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendMediaTemplateMessageWithUrl
public boolean sendMediaTemplateMessageWithUrl(String pageId, String recipientId, String mediaType, String facebookUrl, List<Map<String, Object>> buttons, String pageAccessToken) 发送媒体模板消息(使用Facebook URL)- Parameters:
pageId- 页面IDrecipientId- 接收者IDmediaType- 媒体类型(image|video)facebookUrl- Facebook媒体URLbuttons- 按钮列表(最多3个)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
isValidFacebookUrl
验证是否为有效的Facebook网址- Parameters:
url- 要验证的URL- Returns:
- 是否为有效的Facebook网址
-
sendProductTemplateMessage
public boolean sendProductTemplateMessage(String pageId, String recipientId, List<String> productIds, String pageAccessToken) 发送商品模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDproductIds- 商品ID列表(最多10个)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendReceiptTemplateMessage
public boolean sendReceiptTemplateMessage(String pageId, String recipientId, String recipientName, String orderNumber, String currency, String paymentMethod, String orderUrl, String timestamp, Map<String, String> address, Map<String, Object> summary, List<Map<String, Object>> adjustments, List<Map<String, Object>> elements, String pageAccessToken) 发送回执模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDrecipientName- 收件人姓名orderNumber- 订单号currency- 货币代码paymentMethod- 支付方式orderUrl- 订单链接timestamp- 订单时间戳address- 地址信息summary- 订单汇总adjustments- 调整项列表elements- 订单项目列表pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendStructuredInformationTemplateMessage
public boolean sendStructuredInformationTemplateMessage(String pageId, String recipientId, List<String> countries, String privacyUrl, Integer expiresInDays, List<Map<String, Object>> addressOverrides, List<Map<String, Object>> contactOverrides, String purpose, String pageAccessToken) 发送结构化信息模板消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDcountries- 国家代码列表privacyUrl- 隐私政策URLexpiresInDays- 过期天数(1-7)addressOverrides- 地址字段覆盖contactOverrides- 联系信息字段覆盖purpose- 模板用途(默认SHIPPING)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendAttachmentConfirmationTemplate
public void sendAttachmentConfirmationTemplate(String pageId, String recipientId, String attachmentUrl, String pageAccessToken) 发送附件确认模板消息(测试功能) 向用户发送一个带有确认按钮的模板消息,询问用户是否确认附件- Parameters:
pageId- Meta页面IDrecipientId- 接收者IDattachmentUrl- 附件URLpageAccessToken- 页面访问令牌
-
sendTextMessageWithQuickReplies
public boolean sendTextMessageWithQuickReplies(String pageId, String recipientId, String text, List<Map<String, Object>> quickReplies, String pageAccessToken) 发送带快速回复的文本消息- Parameters:
pageId- 页面IDrecipientId- 接收者IDtext- 消息文本quickReplies- 快速回复列表pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendNotificationMessagesSubscription
public boolean sendNotificationMessagesSubscription(String pageId, String recipientId, String title, String imageUrl, String payload, String timezone, String pageAccessToken) 发送营销消息订阅请求- Parameters:
pageId- 页面IDrecipientId- 接收者IDtitle- 订阅标题imageUrl- 图片URLpayload- 额外信息timezone- 时区pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
sendNotificationMessage
public boolean sendNotificationMessage(String pageId, String notificationMessagesToken, Object messageContent, String pageAccessToken) 发送营销消息- Parameters:
pageId- 页面IDnotificationMessagesToken- 通知消息令牌messageContent- 消息内容(可以是文本或附件)pageAccessToken- 页面访问令牌- Returns:
- 发送结果
-
setPageLevelPersistentMenu
public boolean setPageLevelPersistentMenu(String pageId, List<Map<String, Object>> persistentMenu, String pageAccessToken) 设置页面级固定菜单- Parameters:
pageId- 页面IDpersistentMenu- 固定菜单配置pageAccessToken- 页面访问令牌- Returns:
- 设置结果
-
setUserLevelPersistentMenu
public boolean setUserLevelPersistentMenu(String pageId, String psid, List<Map<String, Object>> persistentMenu, String pageAccessToken) 设置用户级固定菜单- Parameters:
pageId- 页面IDpsid- 用户PSIDpersistentMenu- 固定菜单配置pageAccessToken- 页面访问令牌- Returns:
- 设置结果
-
setGetStartedButton
设置"开始"按钮 这是使用固定菜单的必需条件- Parameters:
pageId- 页面IDpageAccessToken- 页面访问令牌- Returns:
- 设置结果
-