创建
Channel
:ChannelClient
通过频道ID来检索我的频道(也希望也希望也希望消息):
private static IChannelClient ChannelClient
{
get
{
var config = ServiceLocator.GetService<IConfigUtility>();
var clientFactory = new StreamClientFactory(config.ApiKey, config.ApiSecret);
return clientFactory.GetChannelClient();
}
}
默认值,
ChannelClient
不包括频道状态(例如消息)。您需要通过在ChannelgetRequest中设置“状态= true”来明确要求它。
public async Task<ChatMessageDto[]> GetChannelMessages(string channelId)
{
try
{
var channel = await ChannelClient.GetOrCreateAsync("messaging", //channel id is: 4a20bbb1-63f9-46f7-80d3-08dd51a1dfad
channelId,
new ChannelGetRequest
{
MessagesPagination = new MessagePaginationParams { Limit = 50, Offset = 0 },
MembersPagination = new PaginationParams { Limit = 10, Offset = 0 },
WatchersPagination = new PaginationParams { Limit = 20, Offset = 0 }
});
return channel.Messages.Select(x => new ChatMessageDto //channel.Messages is always empty here
{
StreamMessageId = x.Id,
CreatedBy = x.User.Id,
CreatedDate = x.CreatedAt,
MessageText = x.Text,
ChannelId = channel.Channel.Id
}).ToArray();
}
catch (Exception ex)
{
Logger.LogError(ex, $"Failed to get messages for channel id: {channelId} at {DateTimeOffset.UtcNow}");
return [];
}
}