Skip to content

Commit

Permalink
feat 新增读取通知功能
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Aug 15, 2024
1 parent dadeac7 commit 18b0287
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 83 deletions.
10 changes: 9 additions & 1 deletion ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ public Task OnLoaded()
Cart.Command.ResponsePurchaseSelf(bot),

//Community
"NOTIFICATION" or
"N" when access >= EAccess.Operator =>
Community.Command.ResponseGetNotifications(bot),

"CLEARNOTIFICATION" or
"CN" when access >= EAccess.Operator =>
Community.Command.ResponseClearNotification(bot),
Expand Down Expand Up @@ -693,6 +697,10 @@ public Task OnLoaded()
Cart.Command.ResponsePurchaseSelf(Utilities.GetArgsAsText(args, 1, ",")),

//Community
"NOTIFICATION" or
"N" when access >= EAccess.Operator =>
Community.Command.ResponseGetNotifications(Utilities.GetArgsAsText(args, 1, ",")),

"CLEARNOTIFICATION" or
"CN" when access >= EAccess.Operator =>
Community.Command.ResponseClearNotification(Utilities.GetArgsAsText(args, 1, ",")),
Expand Down Expand Up @@ -1169,7 +1177,7 @@ public Task OnLoaded()

if (!string.IsNullOrEmpty(Config.ApiKey))
{
cfg = cfg.Replace(Config.ApiKey, "null");
cfg = cfg.Replace(Config.ApiKey, "**hidden**");
}

var sb = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/ASFEnhance.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
Expand Down
18 changes: 9 additions & 9 deletions ASFEnhance/Account/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,31 +436,31 @@ decimal ParseMoneyString(string strMoney)
{
switch (option.NotificationType)
{
case NotificationType.ReceivedGift:
case ENotificationType.ReceivedGift:
result.ReceivedGift = option.NotificationTargets;
break;
case NotificationType.SubscribedDissionReplyed:
case ENotificationType.SubscribedDissionReplyed:
result.SubscribedDissionReplyed = option.NotificationTargets;
break;
case NotificationType.ReceivedNewItem:
case ENotificationType.ReceivedNewItem:
result.ReceivedNewItem = option.NotificationTargets;
break;
case NotificationType.ReceivedFriendInvitation:
case ENotificationType.ReceivedFriendInvitation:
result.ReceivedFriendInvitation = option.NotificationTargets;
break;
case NotificationType.MajorSaleStart:
case ENotificationType.MajorSaleStart:
result.MajorSaleStart = option.NotificationTargets;
break;
case NotificationType.ItemInWishlistOnSale:
case ENotificationType.ItemInWishlistOnSale:
result.ItemInWishlistOnSale = option.NotificationTargets;
break;
case NotificationType.ReceivedTradeOffer:
case ENotificationType.ReceivedTradeOffer:
result.ReceivedTradeOffer = option.NotificationTargets;
break;
case NotificationType.ReceivedSteamSupportReply:
case ENotificationType.ReceivedSteamSupportReply:
result.ReceivedSteamSupportReply = option.NotificationTargets;
break;
case NotificationType.SteamTurnNotification:
case ENotificationType.SteamTurnNotification:
result.SteamTurnNotification = option.NotificationTargets;
break;
}
Expand Down
16 changes: 8 additions & 8 deletions ASFEnhance/Account/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ internal static async Task<bool> RemoveLicense(Bot bot, uint subId)

var optionList = new List<NotificationPayload>
{
new(NotificationType.ReceivedGift, option.ReceivedGift),
new(NotificationType.SubscribedDissionReplyed,option.SubscribedDissionReplyed),
new(NotificationType.ReceivedNewItem,option.ReceivedNewItem),
new(NotificationType.MajorSaleStart,option.MajorSaleStart),
new(NotificationType.ItemInWishlistOnSale,option.ItemInWishlistOnSale),
new(NotificationType.ReceivedTradeOffer,option.ReceivedTradeOffer),
new(NotificationType.ReceivedSteamSupportReply,option.ReceivedSteamSupportReply),
new(NotificationType.SteamTurnNotification,option.SteamTurnNotification),
new(ENotificationType.ReceivedGift, option.ReceivedGift),
new(ENotificationType.SubscribedDissionReplyed,option.SubscribedDissionReplyed),
new(ENotificationType.ReceivedNewItem,option.ReceivedNewItem),
new(ENotificationType.MajorSaleStart,option.MajorSaleStart),
new(ENotificationType.ItemInWishlistOnSale,option.ItemInWishlistOnSale),
new(ENotificationType.ReceivedTradeOffer,option.ReceivedTradeOffer),
new(ENotificationType.ReceivedSteamSupportReply,option.ReceivedSteamSupportReply),
new(ENotificationType.SteamTurnNotification,option.SteamTurnNotification),
};

var json = optionList.ToJsonText();
Expand Down
109 changes: 108 additions & 1 deletion ASFEnhance/Community/Command.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,119 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using ASFEnhance.Data;
using System.Text;


namespace ASFEnhance.Community;

internal static class Command
{
/// <summary>
/// 清除通知小绿信
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseGetNotifications(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

var result = await WebRequest.GetSteamNotificationsResponse(bot).ConfigureAwait(false);
var response = result?.Response;

if (response?.Notifications == null)
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var sb = new StringBuilder();
sb.AppendLine(Langs.MultipleLineResult);

int index = 1;
foreach (var notification in response.Notifications)
{
var type = notification.NotificationType switch
{
(int)ENotificationType.ReceivedGift => Langs.MsgReceivedGift,
(int)ENotificationType.SubscribedDissionReplyed => Langs.MsgReceivedReplyed,
(int)ENotificationType.ReceivedNewItem => Langs.MsgReceivedNewItem,
(int)ENotificationType.ReceivedFriendInvitation => Langs.MsgFriendInviation,
(int)ENotificationType.MajorSaleStart => Langs.MsgMajorSale,
(int)ENotificationType.ItemInWishlistOnSale => Langs.MsgWishlistItemOnSale,
(int)ENotificationType.ReceivedTradeOffer => Langs.MsgTradeOffer,
(int)ENotificationType.ReceivedSteamSupportReply => Langs.MsgSteamSupport,
(int)ENotificationType.SteamTurnNotification => Langs.MsgSteamTurn,
(int)ENotificationType.SteamCommunityMessage => Langs.MsgCommunityMessage,
_ => notification.NotificationType.ToString(),
};

sb.AppendLineFormat("{0}{1}: {2} {3}", notification.Read ? "" : "*", index++, type, notification.BodyData);

if (index >= 15)
{
break;
}
}

if (index == 1)
{
sb.AppendLine(Langs.MsgNoMessage);
}

sb.AppendLine(Static.Line);
sb.AppendLineFormat(Langs.NocUnreadCount, response.UnreadCount);

if (response.ConfirmationCount > 0)
{
sb.AppendLineFormat(Langs.NocConfirmationCount, response.ConfirmationCount);
}
if (response.PendingGiftCount > 0)
{
sb.AppendLineFormat(Langs.NocPendingGiftCount, response.PendingGiftCount);
}
if (response.PendingFriendCount > 0)
{
sb.AppendLineFormat(Langs.NocPendingFriendCount, response.PendingFriendCount);
}
if (response.PendingFamilyInviteCount > 0)
{
sb.AppendLineFormat(Langs.NocPendingFamilyInviteCount, response.PendingFamilyInviteCount);
}

return bot.FormatBotResponse(sb.ToString());
}

/// <summary>
/// 清除通知小绿信 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseGetNotifications(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0))
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseGetNotifications(bot))).ConfigureAwait(false);

var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}


/// <summary>
/// 清除通知小绿信
/// </summary>
Expand All @@ -19,7 +126,7 @@ internal static class Command
return bot.FormatBotResponse(Strings.BotNotConnected);
}

var result = await WebRequest.PureCommentNotifications(bot).ConfigureAwait(false);
var result = await WebRequest.MarkNotificationsRead(bot).ConfigureAwait(false);

return bot.FormatBotResponse(result ? Langs.Success : Langs.Failure);
}
Expand Down
26 changes: 16 additions & 10 deletions ASFEnhance/Community/WebRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ArchiSteamFarm.Steam;
using ASFEnhance.Data.ISteamNotificationService;

namespace ASFEnhance.Community;

Expand All @@ -9,23 +10,28 @@ internal static class WebRequest
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<bool> PureCommentNotifications(Bot bot)
internal static async Task<bool> MarkNotificationsRead(Bot bot)
{
var token = bot.AccessToken;
if (token == null)
{
return false;
}

var request = new Uri(SteamApiURL, "/ISteamNotificationService/MarkNotificationsRead/v1/");

var data = new Dictionary<string, string>(2) {
{ "access_token", token },
{ "access_token", bot.AccessToken ?? throw new AccessTokenNullException() },
{ "timestamp", "0" },
{ "mark_all_read", "true" },
};

await bot.ArchiWebHandler.UrlPostWithSession(request, data: data, referer: SteamCommunityURL).ConfigureAwait(false);
await bot.ArchiWebHandler.UrlPost(request, data: data, referer: SteamCommunityURL).ConfigureAwait(false);

return true;
}

internal static async Task<GetSteamNotificationsResponse?> GetSteamNotificationsResponse(Bot bot)
{
var token = bot.AccessToken ?? throw new AccessTokenNullException();

var request = new Uri(SteamApiURL, $"/ISteamNotificationService/GetSteamNotifications/v1/?access_token={token}&include_hidden=true&language={Langs.Language}&include_confirmation_count=true&include_pinned_counts=true&include_read=true");

var response = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession<GetSteamNotificationsResponse>(request, referer: SteamCommunityURL).ConfigureAwait(false);

return response?.Content;
}
}
8 changes: 4 additions & 4 deletions ASFEnhance/Data/CuratorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public sealed record CuratorItem
/// 名称
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; } = "";
public string? Name { get; set; }

/// <summary>
/// 描述
/// </summary>
[JsonPropertyName("curator_description")]
public string Description { get; set; } = "";
public string? Description { get; set; }

/// <summary>
/// ID
/// </summary>
[JsonPropertyName("clanId")]
public string ClanId { get; set; } = "";
[JsonPropertyName("clanID")]
public string? ClanId { get; set; }

/// <summary>
/// 关注人数
Expand Down
48 changes: 48 additions & 0 deletions ASFEnhance/Data/ENotificationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace ASFEnhance.Data;

/// <summary>
/// 通知类型
/// </summary>
internal enum ENotificationType : byte
{
/// <summary>
/// 收到礼物
/// </summary>
ReceivedGift = 2,
/// <summary>
/// 订阅的讨论回复
/// </summary>
SubscribedDissionReplyed,
/// <summary>
/// 有新物品
/// </summary>
ReceivedNewItem,
/// <summary>
/// 收到好友邀请
/// </summary>
ReceivedFriendInvitation,
/// <summary>
/// 大促开始
/// </summary>
MajorSaleStart,
/// <summary>
/// 愿望单物品打折
/// </summary>
ItemInWishlistOnSale = 8,
/// <summary>
/// 收到交易报价
/// </summary>
ReceivedTradeOffer,
/// <summary>
/// 收到客服回复
/// </summary>
ReceivedSteamSupportReply = 11,
/// <summary>
/// Steam回合通知
/// </summary>
SteamTurnNotification,
/// <summary>
/// Steam消息
/// </summary>
SteamCommunityMessage = 14,
}
Loading

0 comments on commit 18b0287

Please sign in to comment.