Lớp công cụ
RecommendationService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Phương Pháp
GenerateItemListAsync(generateRecommendationItemListRequest: Dictionary):RecommendationPages |
GetRecommendationItemAsync(itemId: string):Dictionary |
LogActionEvent(actionType: Enum.RecommendationActionType,itemId: string,tracingId: string,actionEventDetails: Dictionary):() |
LogImpressionEvent(impressionType: Enum.RecommendationImpressionType,itemId: string,tracingId: string,impressionEventDetails: Dictionary):() |
LogPreferenceEvent(preferenceType: Enum.RecommendationPreferenceType,targetType: Enum.RecommendationPreferenceTargetType,targetId: string,tracingId: string,itemId: string):() |
RegisterItemAsync(player: Player,registerRecommendationItemsRequest: Dictionary):Dictionary |
RemoveItemAsync(itemId: string):() |
UpdateItemAsync(updateRecommendationItemRequest: Dictionary):() |
Tài Liệu Tham Khảo API
Phương Pháp
GenerateItemListAsync
RecommendationService:GenerateItemListAsync(generateRecommendationItemListRequest:Dictionary):RecommendationPages
Tham Số
Lợi Nhuận
Mẫu mã
Tạo danh sách những món đồ được đề xuất
local RecommendationService = game:GetService("RecommendationService")
-- Định nghĩa yêu cầu để tạo danh sách đề xuất
local request: GenerateRecommendationItemListRequest = {
ConfigName = "MaximizeEngagement",
LocationId = "Lobby",
PageSize = 10
-- LƯU Ý: Bỏ comment và đặt CustomContexts.UserId cho script Server.
-- Không cần đặt cho script Local.
-- CustomContexts = {
-- ["UserId"] = tostring(player.UserId),
-- }
}
-- Gọi GenerateItemListAsync để lấy các trang đề xuất
local success, recommendationPages = pcall(function()
return RecommendationService:GenerateItemListAsync(request)
end)GetRecommendationItemAsync
Tham Số
Lợi Nhuận
Mẫu mã
Lấy một mục đề xuất đơn
local RecommendationService = game:GetService("RecommendationService")
local function getItem(itemId)
local success, item = pcall(function()
return RecommendationService:GetRecommendationItemAsync(itemId)
end)
if success and item then
print("Lấy mục thành công: " .. item.ItemId)
print(" ReferenceId: " .. item.ReferenceId)
-- TracingId sẽ trống khi lấy một mục đơn trực tiếp
if item.Creator then
print(" CreatorId: " .. tostring(item.Creator.CreatorId))
end
if item.CustomTags then
print(" CustomTags: ")
for _, customTag in ipairs(item.CustomTags) do
print(" " .. customTag)
end
end
else
warn("Lấy mục thất bại: " .. itemId .. ". Lỗi: " .. tostring(item))
end
end
-- Ví dụ sử dụng (cần một itemId hợp lệ)
-- getItem("some-item-id")LogActionEvent
RecommendationService:LogActionEvent(
actionType:Enum.RecommendationActionType, itemId:string, tracingId:string, actionEventDetails:Dictionary
):()
Tham Số
| Giá Trị Mặc Định: "nil" |
Lợi Nhuận
()
Mẫu mã
Ghi lại hành động của người dùng
local RecommendationService = game:GetService("RecommendationService")
local function logLike(itemId, tracingId)
local detail: RecommendationActionEventDetails = {
ReactionType = "Thích",
Weight = 1.0
}
RecommendationService:LogActionEvent(Enum.RecommendationActionType.AddReaction, itemId, tracingId, detail)
print("Đã ghi lại hành động 'Thích' cho mục: " .. itemId)
end
-- Ví dụ sử dụng (cần một itemId và tracingId hợp lệ từ GenerateItemListAsync)
-- logLike("item-id", "tracing-id")LogImpressionEvent
RecommendationService:LogImpressionEvent(
impressionType:Enum.RecommendationImpressionType, itemId:string, tracingId:string, impressionEventDetails:Dictionary
):()
Tham Số
| Giá Trị Mặc Định: "nil" |
Lợi Nhuận
()
Mẫu mã
Ghi lại ấn tượng của người dùng
local RecommendationService = game:GetService("RecommendationService")
local function logView(itemId, tracingId)
local detail: RecommendationImpressionEventDetails = {
Duration = 5, -- Người dùng đã xem trong 5 giây
ItemPosition = 1,
DepartureIntent = Enum.RecommendationDepartureIntent.Neutral
}
RecommendationService:LogImpressionEvent(Enum.RecommendationImpressionType.View, itemId, tracingId, detail)
print("Đã ghi lại ấn tượng 'Xem' cho mục: " .. itemId)
end
-- Ví dụ sử dụng (cần có itemId và tracingId hợp lệ từ GenerateItemListAsync)
-- logView("some-item-id", "some-tracing-id")LogPreferenceEvent
RecommendationService:LogPreferenceEvent(
preferenceType:Enum.RecommendationPreferenceType, targetType:Enum.RecommendationPreferenceTargetType, targetId:string, tracingId:string, itemId:string
):()
Tham Số
Lợi Nhuận
()
RegisterItemAsync
RecommendationService:RegisterItemAsync(
Tham Số
Lợi Nhuận
Mẫu mã
Đăng ký một mục mới
local RecommendationService = game:GetService("RecommendationService")
local storage = game:GetService("ReplicatedStorage")
-- Giả sử một RemoteFunction có tên 'RegisterItemRemote' tồn tại trong ReplicatedStorage
local registerItemRemote = storage.RegisterItemRemote
registerItemRemote.OnServerInvoke = function(player, refId)
local request: RegisterRecommendationItemRequest = {
ContentType = Enum.RecommendationItemContentType.Dynamic,
ReferenceId = refId,
Duration = 60,
Visibility = Enum.RecommendationItemVisibility.Public,
CustomTags = {"locale:en-us", "seasonal:summer"},
Attributes = {
{
AssetId = 123,
Description = "video thử nghiệm",
TrimStartTime = 0,
TrimEndTime = 10.5
}
}
}
local success, response = pcall(function()
return RecommendationService:RegisterItemAsync(player, request)
end)
if success and response then
print("Đã đăng ký mục thành công. ItemId: " .. response.ItemId)
return response.ItemId
else
warn("Đăng ký mục thất bại. Lỗi: " .. tostring(response))
return nil
end
endRemoveItemAsync
Tham Số
Lợi Nhuận
()
Mẫu mã
Xóa một mục
local RecommendationService = game:GetService("RecommendationService")
local function removeItem(itemId)
local success, err = pcall(function()
return RecommendationService:RemoveItemAsync(itemId)
end)
if success then
print("Đã xóa mục thành công: " .. itemId)
else
warn("Không thể xóa mục: " .. itemId .. ". Lỗi: " .. tostring(err))
end
endUpdateItemAsync
Tham Số
Lợi Nhuận
()
Mẫu mã
Cập nhật một mục
local RecommendationService = game:GetService("RecommendationService")
local function updateItem(itemId)
-- Chỉ bao gồm các trường bạn muốn cập nhật
local request: UpdateRecommendationItemRequest = {
ItemId = itemId,
Visibility = Enum.RecommendationItemVisibility.Private,
-- CHÚ Ý: toàn bộ danh sách CustomTags sẽ được thay thế bằng danh sách mới này
CustomTags = {"updated-tag"}
}
local success, err = pcall(function()
return RecommendationService:UpdateItemAsync(request)
end)
if success then
print("Cập nhật mục thành công: " .. itemId)
else
warn("Cập nhật mục thất bại: " .. itemId .. ". Lỗi: " .. tostring(err))
end
end
-- Ví dụ sử dụng
-- updateItem("some-item-id-to-update")