訊息¶
Hive 聊天功能支持 頻道消息發送,以向用戶參與的頻道發送消息,以及 1:1 消息發送,以直接向特定個人發送消息。
頻道消息發送¶
要向用户已加入的频道发送消息,请创建一个 ChannelSendMessageParams
对象,并将其作为参数传递给 Chat 类的 sendMessage()
方法。
sendMessage()
方法返回傳輸結果以及用於發送的 ChannelSendMessageParams
作為 retryParam
。 如果傳輸失敗,您可以嘗試使用 retryParams
重新發送。
Channelsendmessageparams¶
字段名稱 | 描述 | 類型 | 必填 |
---|---|---|---|
channelId | 發送消息的頻道ID | 字串 | 是 |
message | 要發送到頻道的消息 (最多200個字符) | 字串 | 是 |
extraData | 頻道消息的附加信息 最多256B字節(UTF-8) | 字串 | 否 |
replyMessageId | 正在回覆的消息ID | 字串 | 否 |
mentionedPlayerIds | 要提及的玩家ID | 列表 <long> | 否 |
這是一個示例代碼,用於向用戶參與的頻道發送消息。
using hive;
ChannelSendMessageParams channelSendMessageParams = new ChannelSendMessageParams();
channelSendMessageParams.channelId = "CHANNEL_ID";
channelSendMessageParams.message = "你好 Hive";
channelSendMessageParams.extraData = "EXTRA_DATA";
Chat.sendMessage(channelSendMessageParams, (ResultAPI result, ChannelSendMessageParams retryParams) => {
if (!result.isSuccess()) {
// 發送消息失敗,嘗試使用 `retryParams` 重新發送
}
});
#include "HiveChat.h"
FHiveChatChannelSendMessageParams ChannelSendMessageParams;
ChannelSendMessageParams.ChannelId = TEXT("CHANNEL_ID");
ChannelSendMessageParams.Message = TEXT("你好 Hive");
ChannelSendMessageParams.ExtraData = TEXT("EXTRA_DATA");
FHiveChat::SendMessageWithChannelSendMessageParams(ChannelSendMessageParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChatChannelSendMessageParams& RetryParams) {
if (!Result.IsSuccess) {
// 消息发送失败,尝试使用`RetryParams`重新发送
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
ChannelSendMessageParams params;
params.channelId = "CHANNEL_ID";
params.message = "你好 Hive";
params.extraData = "EXTRA_DATA";
Chat::sendMessageWithChannelSendMessageParams(params, [=](ResultAPI const & result, ChannelSendMessageParams const & retryParams) {
if (!result.isSuccess()) {
// Message Send Failed, Try resnding using `retryParams`
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val params = Chat.ChannelSendMessageParams(
channelId = "CHANNEL_ID",
message = "你好 Hive",
extraData = "EXTRA_DATA"
)
Chat.sendMessage(params, object: Chat.SendMessageListener {
override fun onResult(
result: ResultAPI,
retryParams: Chat.ChannelSendMessageParams?
) {
if (!result.isSuccess) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
}
})
#import "HIVEService.h"
HiveChatChannelSendMessageParams* sendMessageParams = [[HiveChatChannelSendMessageParams alloc] init];
sendMessageParams.channelId = "CHANNEL_ID";
sendMessageParams.message = "你好 Hive";
sendMessageParams.extraData = "EXTRA_DATA";
[HiveChat sendMessageWithChannelSendMessageParams:sendMessageParams handler:^(HIVEResultAPI * result, HiveChatChannelSendMessageParams * retryParams) {
if (!result.isSuccess) {
// 消息发送失败。尝试使用 `retryParams` 重新发送
}
}];
以下是一段示例代码,用于向用户已加入的频道中的特定目标消息发送回复。
using hive;
ChannelSendMessageParams channelSendMessageParams = new ChannelSendMessageParams();
channelSendMessageParams.channelId = "CHANNEL_ID";
channelSendMessageParams.message = "你好 Hive";
channelSendMessageParams.replyMessageId = "TARGET_MESSAGE_ID";
Chat.sendMessage(channelSendMessageParams, (ResultAPI result, ChannelSendMessageParams retryParams) => {
if (!result.isSuccess()) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
});
#include "HiveChat.h"
FHiveChatChannelSendMessageParams ChannelSendMessageParams;
ChannelSendMessageParams.ChannelId = TEXT("CHANNEL_ID");
ChannelSendMessageParams.Message = TEXT("你好 Hive");
ChannelSendMessageParams.ReplyMessageId = TEXT("TARGET_MESSAGE_ID");
FHiveChat::SendMessageWithChannelSendMessageParams(ChannelSendMessageParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChatChannelSendMessageParams& RetryParams) {
if (!Result.IsSuccess) {
// 消息发送失败,尝试使用 `RetryParams` 重新发送
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
ChannelSendMessageParams params;
params.channelId = "CHANNEL_ID";
params.message = "你好 Hive";
params.replyMessageId = "TARGET_MESSAGE_ID";
Chat::sendMessageWithChannelSendMessageParams(params, [=](ResultAPI const & result, ChannelSendMessageParams const & retryParams) {
if (!result.isSuccess()) {
// Message Send Failed, Try resnding using `retryParams`
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val params = Chat.ChannelSendMessageParams(
channelId = "CHANNEL_ID",
message = "你好 Hive",
replyMessageId = "TARGET_MESSAGE_ID"
)
Chat.sendMessage(params, object: Chat.SendMessageListener {
override fun onResult(
result: ResultAPI,
retryParams: Chat.ChannelSendMessageParams?
) {
if (!result.isSuccess) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
}
})
import com.hive.Chat;
Chat.ChannelSendMessageParams params = new Chat.ChannelSendMessageParams(
"CHANNEL_ID",
"Hello Hive",
"EXTRA_DATA",
"TARGET_MESSAGE_ID"
);
Chat.sendMessage(params, (result, retryParams) -> {
if(!result.isSuccess()) {
// Message Send Failed, Try resending using `retryParams`
}
});
#import "HIVEService.h"
HiveChatChannelSendMessageParams* sendMessageParams = [[HiveChatChannelSendMessageParams alloc] init];
sendMessageParams.channelId = "CHANNEL_ID";
sendMessageParams.message = "你好 Hive";
sendMessageParams.replyMessageId = "TARGET_MESSAGE_ID";
[HiveChat sendMessageWithChannelSendMessageParams:sendMessageParams handler:^(HIVEResultAPI * result, HiveChatChannelSendMessageParams * retryParams) {
if (!result.isSuccess) {
// 消息发送失败。尝试使用 `retryParams` 重新发送
}
}];
以下是一个示例代码,用于在用户已加入的频道中发送提及特定用户的消息。
using hive;
ChannelSendMessageParams channelSendMessageParams = new ChannelSendMessageParams();
channelSendMessageParams.channelId = "CHANNEL_ID";
channelSendMessageParams.message = "你好 Hive";
List<long> playerIds = new List<long>();
playerIds.Add(1234); // User 1 PlayerId
playerIds.Add(5678); // User 2 PlayerId
channelSendMessageParams.mentionedPlayerIds = playerIds;
Chat.sendMessage(channelSendMessageParams, (ResultAPI result, ChannelSendMessageParams retryParams) => {
if (!result.isSuccess()) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
});
#include "HiveChat.h"
FHiveChatChannelSendMessageParams ChannelSendMessageParams;
ChannelSendMessageParams.ChannelId = TEXT("CHANNEL_ID");
ChannelSendMessageParams.Message = TEXT("你好 Hive");
TArray<int64> PlayerIds;
PlayerIds.Add(1234); // User 1 PlayerId
PlayerIds.Add(5678); // User 2 PlayerId
ChannelSendMessageParams.MentionedPlayerIds = PlayerIds;
FHiveChat::SendMessageWithChannelSendMessageParams(ChannelSendMessageParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChatChannelSendMessageParams& RetryParams) {
if (!Result.IsSuccess) {
// 消息发送失败,尝试使用 `RetryParams` 重新发送
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
ChannelSendMessageParams params;
params.channelId = "CHANNEL_ID";
params.message = "你好 Hive";
std::vector<int64_t> playerIds;
playerIds.push_back(1234); // User 1 PlayerId
playerIds.push_back(5678); // User 2 PlayerId
params.mentionedPlayerIds = playerIds;
Chat::sendMessageWithChannelSendMessageParams(params, [=](ResultAPI const & result, ChannelSendMessageParams const & retryParams) {
if (!result.isSuccess()) {
// Message Send Failed, Try resnding using `retryParams`
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val playerIds = listOf(1234L, 5678L) // User 1, 2的 PlayerId
val params = Chat.ChannelSendMessageParams(
channelId = "CHANNEL_ID",
message = "Hello Hive",
replyMessageId = "TARGET_MESSAGE_ID",
mentionedPlayerIds = playerIds
)
Chat.sendMessage(params, object: Chat.SendMessageListener {
override fun onResult(
result: ResultAPI,
retryParams: Chat.ChannelSendMessageParams?
) {
if (!result.isSuccess) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
}
})
import com.hive.Chat;
List<Long> mentionedPlayerIds = new ArrayList<>();
mentionedPlayerIds.add(1234L); // 用户 1 玩家ID
mentionedPlayerIds.add(5678L); // 用户 2 玩家ID
Chat.ChannelSendMessageParams params = new Chat.ChannelSendMessageParams(
"CHANNEL_ID",
"Hello Hive",
"EXTRA_DATA"
);
params.mentionedPlayerIds = mentionedPlayerIds;
Chat.sendMessage(params, (result, retryParams) -> {
if(!result.isSuccess()) {
// Message Send Failed, Try resending using `retryParams`
}
});
import HIVEService
var playerIds: [Int64] = [1234, 5678] // User 1, 2的 PlayerId
let params = HiveChatParams.ChannelSendMessageParams(
channelId = "CHANNEL_ID",
message = "Hello Hive",
mentionedPlayerIds = playerIds
)
ChatInterface.sendMessage(params) { result, retryParams in
if !result.isSuccess {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
}
#import "HIVEService.h"
HiveChatChannelSendMessageParams* sendMessageParams = [[HiveChatChannelSendMessageParams alloc] init];
sendMessageParams.channelId = @"CHANNEL_ID";
sendMessageParams.message = @"你好 Hive";
NSMutableArray<NSNumber *> *playerIds = [NSMutableArray array];
[playerIds addObject:@(1234)]; // 用户 1 PlayerId
[playerIds addObject:@(5678)]; // 用户 2 PlayerId
sendMessageParams.mentionedPlayerIds = playerIds;
[HiveChat sendMessageWithChannelSendMessageParams:sendMessageParams handler:^(HIVEResultAPI * result, HiveChatChannelSendMessageParams * retryParams) {
if (!result.isSuccess) {
// 訊息發送失敗。嘗試使用 `retryParams` 重新發送
}
}];
添加或移除頻道消息的反應¶
用戶可以對他們已加入的頻道中特定用戶的消息添加或移除反應。消息反應功能使用戶能夠輕鬆表達他們的感受並提供即時的快速反饋,從而促進更有效的遊戲內溝通。特別是,它有助於使頻道中的對話流程更加生動和直觀。
Note
目前僅支援「喜歡」反應類型。未來將添加更多反應類型。
以下是示範代碼,用於向用戶已加入的頻道中的消息添加或移除反應。
using hive;
String messageId = "TARGET_MESSAGE_ID";
ReactionType type = ReactionType.Like;
channel.addReaction(messageId, type, (ResultAPI result, ReactionType reactionType) => {
if (result.isSuccess()) {
// API Call Success
}
});
channel.removeReaction(messageId, type, (ResultAPI result, ReactionType reactionType) => {
if (result.isSuccess()) {
// API Call Success
}
});
#include "HiveChat.h"
FString MessageId = TEXT("TARGET_MESSAGE_ID");
EReactionType Type = EReactionType::Like;
channel.AddReaction(MessageId, Type, FHiveChatOnReactionDelegate::CreateLambda([this](const FHiveResultAPI& Result, EReactionType Type) {
if (Result.IsSuccess()) {
// API Call Success
}
}));
channel.RemoveReaction(MessageId, Type, FHiveChatOnReactionDelegate::CreateLambda([this](const FHiveResultAPI& Result, EReactionType Type) {
if (Result.IsSuccess()) {
// API Call Success
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
std::string messageId = "TARGET_MESSAGE_ID";
ReactionType type = ReactionType::Like;
channel.addReaction(messageId, type, [=](ResultAPI const & result, ReactionType type) {
if (result.isSuccess()) {
// API Call Success
}
});
channel.removeReaction(messageId, type, [=](ResultAPI const & result, ReactionType type) {
if (result.isSuccess()) {
// API Call Success
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val messageId = "TARGET_MESSAGE_ID"
val type = Chat.ReactionType.Like;
channel.addReaction(messageId, type, object: Chat.ReactionListener {
override fun onResult(result: ResultAPI, type: Chat.ReactionType) {
if (result.isSuccess()) {
// API Call Success
}
}
})
channel.removeReaction(messageId, type, object: Chat.ReactionListener {
override fun onResult(result: ResultAPI, type: Chat.ReactionType) {
if (result.isSuccess()) {
// API Call Success
}
}
})
import com.hive.Chat;
String messageId = "TARGET_MESSAGE_ID";
Chat.ReactionType type = Chat.ReactionType.Like;
Chat.addReaction(messageId, type, new Chat.ReactionListener() {
@Override
public void onResult(ResultAPI result, ReactionType type) {
if (result.isSuccess()) {
// API Call Success
}
}
});
Chat.removeReaction(messageId, type, new Chat.ReactionListener() {
@Override
public void onResult(ResultAPI result, ReactionType type) {
if (result.isSuccess()) {
// API Call Success
}
}
});
import HIVEService
let messageId = "TARGET_MESSAGE_ID"
let reactionType: ReactionType = .like
ChatInterface.addReaction(messageId: messageId, type: reactionType) { result, type in
if result.isSuccess {
// API Call Success
}
}
ChatInterface.removeReaction(messageId: messageId, type: reactionType) { result, type in
if result.isSuccess {
// API Call Success
}
}
#import "HIVEService.h"
NSString *messageId = @"TARGET_MESSAGE_ID";
HiveChatReactionType reactionType = HiveChatReactionTypeLike;
[channel addReactionWithMessageId:messageId type:reactionType handler:^(ResultAPI *result, HiveChatReactionType type) {
if (result.isSuccess) {
// API Call Success
}
}];
[channel removeReactionWithMessageId:messageId type:reactionType handler:^(ResultAPI *result, HiveChatReactionType type) {
if (result.isSuccess) {
// API Call Success
}
}];
頻道訊息事件管理¶
發送的消息可以被addChannelListener
事件的onChannelMessage
事件處理程序接收。 有關事件處理的更多詳細信息,請參閱事件管理 > 通道事件文檔。
1:1 訊息發送¶
要向特定收件人發送直接消息,使用者創建一個 DirectSendMessageParams
物件,然後將其作為參數傳遞給 Chat 類的 sendMessage()
方法。
sendMessage()
方法返回傳輸結果以及在傳輸過程中使用的 DirectSendMessageParams
,名稱為 retryParam
。 如果傳輸失敗,您可以嘗試使用 retryParams
重新發送。
直接發送消息參數¶
欄位名稱 | 描述 | 類型 | 是否必填 |
---|---|---|---|
toPlayerId | Hive 目標用戶的玩家 ID | long | Y |
message | 要發送給目標用戶的消息 (最多 200 個字符) | string | Y |
extraData | 1:1 消息的附加信息 最多 256B 字節 (UTF-8 標準) | string | N |
以下是用戶向特定收件人發送直接消息的示例代碼。
using hive;
DirectSendMessageParams directSendMessageParams = new DirectSendMessageParams();
directSendMessageParams.toPlayerId = 12345678;
directSendMessageParams.message = "你好 Hive";
directSendMessageParams.extraData = "EXTRA_DATA";
Chat.sendMessage(directSendMessageParams, (ResultAPI result, DirectSendMessageParams retryParams) => {
if (!result.isSuccess()) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
});
#include "HiveChat.h"
FHiveChatDirectSendMessageParams DirectSendMessageParams = FHiveChatDirectSendMessageParams();
DirectSendMessageParams.ToPlayerId = 12345678l;
DirectSendMessageParams.Message = TEXT("Hello Hive");
DirectSendMessageParams.ExtraData = TEXT("EXTRA_DATA");
FHiveChat::SendMessageWithDirectSendMessageParams(DirectSendMessageParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChatDirectSendMessageParams& RetryParams) {
if (!Result.IsSuccess) {
// 訊息發送失敗,嘗試使用 `RetryParams` 重新發送
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
DirectSendMessageParams params;
params.toPlayerId = 12345678;
params.message = "Hello Hive";
params.extraData = "EXTRA_DATA";
Chat::sendMessageWithDirectSendMessageParams(params, [=](ResultAPI const & result, DirectSendMessageParams const & retryParams) {
if (!result.isSuccess()) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val params = Chat.DirectSendMessageParams(
toPlayerId = 12345678,
message = "你好 Hive",
extraData = "EXTRA_DATA"
)
Chat.sendMessage(params, object: Chat.SendMessageListener {
override fun onResult(
result: ResultAPI,
retryParams: Chat.DirectSendMessageParams?
) {
if (!result.isSuccess) {
// 訊息發送失敗,嘗試使用 `retryParams` 重新發送
}
}
})
import HIVEService
let params = HiveChatParams.DirectSendMessageParams(toPlayerId: Int64(12345678), message: "Hello Hive", extraData: "EXTRA_DATA")
ChatInterface.sendMessage(sendMessageParams: params) { result, retryParams in
if !result.isSuccess {
// Message Send Failed, Try resending using `retryParams`
}
}
#import "HIVEService.h"
HiveChatDirectSendMessageParams* sendMessageParams = [[HiveChatDirectSendMessageParams alloc] init];
sendMessageParams.toPlayerId = 12345678;
sendMessageParams.message = "你好 Hive";
sendMessageParams.extraData = "EXTRA_DATA";
[HiveChat sendDirectMessageWithSendMessageParams:sendMessageParams handler:^(HIVEResultAPI * result, HiveChatDirectSendMessageParams * retryParams) {
if (!result.isSuccess) {
// 消息发送失败。尝试使用 `retryParams` 重新发送
}
}];
1:1 訊息事件管理¶
透過 1:1 訊息傳輸發送的訊息可以通過 addUserListener
事件的 onDirectMessage
事件處理程序接收。 有關事件處理的更多詳細資訊,請參閱 事件管理 > 使用者事件 文件。
訊息翻譯請求¶
嘗試請求該消息的翻譯。 語言代碼基於 ISO 639 alpha-2
標準。
翻譯參數¶
欄位名稱 | 描述 | 類型 | 必填 |
---|---|---|---|
message | 訊息 | 字串 | 是 |
sourceLanguage | 訊息的語言代碼。如果省略,則推斷為 auto 。 | 字串 | 否 |
targetLanguages | 翻譯請求的語言代碼陣列 | 字串陣列 | 是 |
using hive;
List<String> targetLanguages = new List<String>();
targetLanguages.Add("ko");
targetLanguages.Add("ja");
targetLanguages.Add("ar");
TranslateParams translateParams = new TranslateParams("hello", "auto", targetLanguages);
Chat.translate(translateParams, (ResultAPI result, TranslationData data) => {
if (result.isSuccess() && data != null) {
// data is translate languages
}
});
#include "HiveChat.h"
TArray<FString> TargetLanguages;
TargetLanguages.Add(TEXT("ko"));
TargetLanguages.Add(TEXT("ja"));
TargetLanguages.Add(TEXT("ar"));
FHiveChatTranslateParams TranslateParams = FHiveChatTranslateParams(TEXT("你好"), TEXT("auto"), TargetLanguages);
FHiveChat::Translate(TranslateParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChatTranslationData& data) {
if (Result.ISSuccess && data != null) {
// data 是翻譯的語言消息
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
std::vector<std::string> targetLanguages = { "ko", "ja", "ar" };
Chat::TranslateParams translateParams = new Chat::TranslateParams("hello", "auto", targetLanguages);
Chat::translate(translateParams, [=](ResultAPI const & result, Chat::TranslationData const & data) {
if (!result.isSuccess() && data != null ) {
// data is translate language messages
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
var translateParams = Chat.TranslateParams(
message = "hello",
sourceLanguage = "auto",
targetLanguages = ["ko", "ja", "ar"]
)
Chat.translate(translateParams, object: Chat.TranslateParamsListener {
override fun onResult(
result: ResultAPI,
data: Chat.TranslationData?
) {
if (!result.isSuccess && data != null) {
// data is translate language maessages
}
}
})
#import "HIVEService.h"
NSArray<NSString *> *targetLanguages = @[@"ko", @"ja", @"ar"];
HiveChatTranslateParams translateParams = [[HiveChatTranslateParams alloc] message: @"hello"
sourceLanguage: @"auto"
targetLanguages: targetLanguages];
[HiveChat translateWithParams: translateParams
handler:^(HIVEResultAPI * result, HiveChatTranslationData * data) {
if (result.isSuccess && data) {
// data is translate language messages
}
}];