跳转至

消息

Hive 聊天功能支持 频道消息发送,以向用户参与的频道发送消息,以及 1:1 消息发送,以直接向特定个人发送消息。

频道消息发送

要向用户已加入的频道发送消息,请创建一个 ChannelSendMessageParams 对象,并将其作为参数传递给 Chat 类的 sendMessage() 方法。

sendMessage() 方法返回传输结果和用于发送的 ChannelSendMessageParams,作为 retryParam。 如果传输失败,您可以尝试使用 retryParams 重新发送。

频道发送消息参数

字段名称 描述 类型 必填
channelId 发送消息的频道 ID 字符串
message 发送到频道的消息
(最多 200 个字符)
字符串
extraData 频道消息的附加信息
最多 256B 字节 (UTF-8)
字符串

这是一个示例代码,用于向用户参与的频道发送消息。

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()) {
        // 消息发送失败,尝试使用 `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 com.hive.Chat;

Chat.ChannelSendMessageParams params = new Chat.ChannelSendMessageParams(
    "CHANNEL_ID",
    "你好 Hive",
    "EXTRA_DATA"
);

Chat.sendMessage(params, (result, retryParams) -> {
    if(!result.isSuccess()) {
        // 消息发送失败,尝试使用 `retryParams` 重新发送
    }
});
import HIVEService

let params = HiveChatParams.ChannelSendMessageParams(
    channelId = "CHANNEL_ID",
    message = "Hello Hive",
    extraData = "EXTRA_DATA"
)
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";
sendMessageParams.extraData = "EXTRA_DATA";

[HiveChat sendMessageWithChannelSendMessageParams:sendMessageParams handler:^(HIVEResultAPI * result, HiveChatChannelSendMessageParams * retryParams) {
    if (!result.isSuccess) {
        // 消息发送失败。尝试使用 `retryParams` 重新发送
    }
}];

频道消息事件管理

发送的消息可以通过onChannelMessage事件处理程序接收,该处理程序属于addChannelListener事件。有关事件处理的更多详细信息,请参阅事件管理 > 通道事件文档。

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("你好 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 = "你好 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 com.hive.Chat;

Chat.DirectSendMessageParams params = new Chat.DirectSendMessageParams(
        12345678,
        "Hello Hive",
        "EXTRA_DATA"
);

Chat.sendMessage(params, (result, retryParams) -> {
    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 {
        // 消息发送失败,尝试使用 `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("你好", "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 是翻译语言消息
        } 
    }
})
import com.hive.Chat;

Chat.TranslateParams translateParams = new Chat.TranslateParams(
    "你好",
    "auto",
    ["ko", "ja", "ar"]
);

Chat.translate(translateParams, (result, data) -> {
    if(result.isSuccess() && data != null) {
        // data 是翻译语言消息
    }
});
import HIVEService

let translateParams = HiveChatParams.TranslateParams("hello", "auto", ["ko", "ja", "ar"])
ChatInterface.translate(params: translateParams) { result, data in
    if result.isSuccess(), let data {
        // data is translate language messages
    }
}
#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 是翻译语言消息
            }
          }];