Class ButtonContent
java.lang.Object
com.bytedesk.core.base.BaseContent
com.bytedesk.core.message.content.ButtonContent
- All Implemented Interfaces:
Serializable
按钮内容类
用于构建Messenger平台的各种按钮类型
支持的按钮类型:
- web_url: 网页按钮
- postback: 回发按钮
- phone_number: 电话按钮
- game_play: 游戏开始按钮
- account_link: 登录按钮
- account_unlink: 注销按钮
- extension: 扩展按钮
使用示例:
1. 创建网页按钮:
ButtonContent webButton = ButtonContent.createWebUrlButton("访问网站", "https://example.com", "full");
2. 创建回发按钮:
ButtonContent postbackButton = ButtonContent.createPostbackButton("开始", "GET_STARTED_PAYLOAD");
3. 创建电话按钮:
ButtonContent phoneButton = ButtonContent.createPhoneButton("联系我们", "+1234567890");
4. 创建游戏按钮:
ButtonContent gameButton = ButtonContent.createGamePlayButtonWithPlayer("开始游戏", "{\"game_id\":\"123\"}", "player_456");
5. 验证按钮配置:
List<ButtonContent> buttons = Arrays.asList(webButton, postbackButton, phoneButton);
ButtonContent.ValidationResult result = ButtonContent.validateButtons(buttons);
if (result.isValid()) {
// 按钮配置有效
} else {
// 处理错误
System.out.println(result.getErrorMessage());
}
6. 转换为Map格式(用于API调用):
List<Map<String, Object>> buttonMaps = ButtonContent.toMapList(buttons);
metaMessageService.sendButtonTemplateMessage(pageId, recipientId, text, buttonMaps, token);
7. 使用Builder模式创建复杂按钮:
ButtonContent complexButton = ButtonContent.builder()
.type("web_url")
.title("复杂按钮")
.url("https://example.com")
.webviewHeightRatio("tall")
.messengerExtensions(true)
.fallbackUrl("https://fallback.com")
.build();
优势:
- 类型安全:编译时检查按钮类型
- 配置验证:自动验证必填字段
- 代码可读性:清晰的API设计
- 维护性:集中管理按钮配置
- 扩展性:易于添加新的按钮类型
- 兼容性:自动转换为Map格式
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
游戏元数据类static class
验证结果类 -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Boolean
是否启用分享按钮 用于extension类型private String
备用URL 用于web_url、extension类型private ButtonContent.GameMetadata
游戏元数据 用于game_play类型private Boolean
是否启用Messenger扩展 用于web_url类型private String
按钮载荷 用于postback、phone_number、game_play类型private String
时区 用于account_link类型private String
按钮标题 必填字段,最多20个字符private String
按钮类型 必填字段private String
URL地址 用于web_url、account_link、extension类型private String
视图样式 用于extension类型 可选值:full, compact, tallprivate String
WebView高度比例 用于web_url、extension类型 可选值:compact, tall, fullprivate String
WebView分享按钮设置 用于web_url类型 可选值:hide -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ButtonContent
createExtensionButton
(String title, String url, String viewStyle) 创建扩展按钮static ButtonContent
createExtensionButtonFull
(String title, String url, String viewStyle, String fallbackUrl, Boolean enableShareButton) 创建扩展按钮(完整配置)static ButtonContent
createGamePlayButton
(String title, String payload) 创建游戏开始按钮static ButtonContent
createGamePlayButtonWithContext
(String title, String payload, String contextId) 创建游戏开始按钮(带上下文ID)static ButtonContent
createGamePlayButtonWithPlayer
(String title, String payload, String playerId) 创建游戏开始按钮(带玩家ID)static ButtonContent
createLogInButton
(String url) 创建登录按钮static ButtonContent
创建注销按钮static ButtonContent
createPhoneButton
(String title, String phoneNumber) 创建电话按钮static ButtonContent
createPostbackButton
(String title, String payload) 创建回发按钮static ButtonContent
createWebUrlButton
(String title, String url, String webviewHeightRatio) 创建网页按钮static ButtonContent
createWebUrlButtonWithExtensions
(String title, String url, String webviewHeightRatio, Boolean messengerExtensions, String fallbackUrl) 创建网页按钮(带Messenger扩展)static ButtonContent
从JSON字符串反序列化为ButtonContent对象获取按钮类型描述boolean
isValid()
验证按钮配置是否有效toMap()
转换为Map结构 用于与现有API兼容toMapList
(List<ButtonContent> buttons) 将ButtonContent列表转换为Map列表 用于与现有API兼容validateButtons
(List<ButtonContent> buttons) 验证ButtonContent列表中的所有按钮Methods inherited from class com.bytedesk.core.base.BaseContent
fromJson, toJson
-
Field Details
-
type
按钮类型 必填字段 -
title
按钮标题 必填字段,最多20个字符 -
payload
按钮载荷 用于postback、phone_number、game_play类型 -
url
URL地址 用于web_url、account_link、extension类型 -
webviewHeightRatio
WebView高度比例 用于web_url、extension类型 可选值:compact, tall, full -
messengerExtensions
是否启用Messenger扩展 用于web_url类型 -
fallbackUrl
备用URL 用于web_url、extension类型 -
viewStyle
视图样式 用于extension类型 可选值:full, compact, tall -
gameMetadata
游戏元数据 用于game_play类型 -
timezone
时区 用于account_link类型
-
-
Constructor Details
-
ButtonContent
public ButtonContent()
-
-
Method Details
-
createWebUrlButton
创建网页按钮- Parameters:
title
- 按钮标题url
- URL地址webviewHeightRatio
- WebView高度比例- Returns:
- ButtonContent实例
-
createWebUrlButtonWithExtensions
public static ButtonContent createWebUrlButtonWithExtensions(String title, String url, String webviewHeightRatio, Boolean messengerExtensions, String fallbackUrl) 创建网页按钮(带Messenger扩展)- Parameters:
title
- 按钮标题url
- URL地址webviewHeightRatio
- WebView高度比例messengerExtensions
- 是否启用Messenger扩展fallbackUrl
- 备用URL- Returns:
- ButtonContent实例
-
createPostbackButton
创建回发按钮- Parameters:
title
- 按钮标题payload
- 载荷- Returns:
- ButtonContent实例
-
createPhoneButton
创建电话按钮- Parameters:
title
- 按钮标题phoneNumber
- 电话号码- Returns:
- ButtonContent实例
-
createGamePlayButton
创建游戏开始按钮- Parameters:
title
- 按钮标题payload
- 载荷- Returns:
- ButtonContent实例
-
createGamePlayButtonWithPlayer
public static ButtonContent createGamePlayButtonWithPlayer(String title, String payload, String playerId) 创建游戏开始按钮(带玩家ID)- Parameters:
title
- 按钮标题payload
- 载荷playerId
- 玩家ID- Returns:
- ButtonContent实例
-
createGamePlayButtonWithContext
public static ButtonContent createGamePlayButtonWithContext(String title, String payload, String contextId) 创建游戏开始按钮(带上下文ID)- Parameters:
title
- 按钮标题payload
- 载荷contextId
- 上下文ID- Returns:
- ButtonContent实例
-
createLogInButton
创建登录按钮- Parameters:
url
- 授权URL- Returns:
- ButtonContent实例
-
createLogOutButton
创建注销按钮- Returns:
- ButtonContent实例
-
createExtensionButton
创建扩展按钮- Parameters:
title
- 按钮标题url
- URL地址viewStyle
- 视图样式- Returns:
- ButtonContent实例
-
createExtensionButtonFull
public static ButtonContent createExtensionButtonFull(String title, String url, String viewStyle, String fallbackUrl, Boolean enableShareButton) 创建扩展按钮(完整配置)- Parameters:
title
- 按钮标题url
- URL地址viewStyle
- 视图样式fallbackUrl
- 备用URLenableShareButton
- 是否启用分享按钮- Returns:
- ButtonContent实例
-
toMap
转换为Map结构 用于与现有API兼容- Returns:
- Map结构
-
isValid
public boolean isValid()验证按钮配置是否有效- Returns:
- 验证结果
-
getTypeDescription
获取按钮类型描述- Returns:
- 类型描述
-
toMapList
将ButtonContent列表转换为Map列表 用于与现有API兼容- Parameters:
buttons
- ButtonContent列表- Returns:
- Map列表
-
validateButtons
验证ButtonContent列表中的所有按钮- Parameters:
buttons
- ButtonContent列表- Returns:
- 验证结果,包含无效按钮的信息
-
fromJson
从JSON字符串反序列化为ButtonContent对象- Parameters:
json
- JSON字符串- Returns:
- ButtonContent对象,如果解析失败返回null
-