Skip to content

Commit

Permalink
fix(plugins/tgbotstatplugin): 0.1.4 : /export
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Aug 5, 2023
1 parent 5f723fd commit a6ef9cb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
8 changes: 4 additions & 4 deletions plugins/TgBotStatPlugin/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ public async static Task<IEnumerable<Message>> QueryByGroupId(string groupId, Pa
string sql = @"SELECT * FROM Message
WHERE GroupId = @GroupId
ORDER BY Id
OFFSET @Offset ROWS
FETCH NEXT @Next ROWS ONLY;";
LIMIT @PageSize OFFSET (@PageIndex * @PageSize);";

return await con.QueryAsync<Message>(sql, new
{
GroupId = groupId,
Offset = pager.Offset,
Next = pager.Next
PageSize = pager.PageSize,
// PageIndex 从 0 开始
PageIndex = pager.Page - 1
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/TgBotStatPlugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.TgBotStatPlugin-v0.1.3.README" style="border:0" alt="" />
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.TgBotStatPlugin-v0.1.4.README" style="border:0" alt="" />
<!-- End Matomo -->
36 changes: 19 additions & 17 deletions plugins/TgBotStatPlugin/TgBotStatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async void HandleUpdateAsync(ITelegramBotClient botClient, Update update,

var chatId = message.Chat.Id;

if (message.Chat.Type == Telegram.Bot.Types.Enums.ChatType.Group)
if (message.Chat.Type != Telegram.Bot.Types.Enums.ChatType.Private)
{
// 只在群聊有效

Expand All @@ -65,24 +65,26 @@ public async void HandleUpdateAsync(ITelegramBotClient botClient, Update update,

#region /export
// 导出当前群聊记录 (分片导出: 多个文件)

long count = await DbContext.CountByGroupId(groupId: chatId.ToString());
for (int i = 1; i <= Math.Floor((decimal)count / (decimal)50); i++)
if (messageText.Trim().StartsWith("/export"))
{
List<Models.Message> tempList = (await DbContext.QueryByGroupId(groupId: chatId.ToString(), new Pager(page: i, pageSize: 50))).ToList();
string tempJsonStr = Utils.JsonUtil.Obj2JsonStr(tempList);

try
long count = await DbContext.CountByGroupId(groupId: chatId.ToString());
for (int i = 1; i <= (int)Math.Ceiling((decimal)count / (decimal)50); i++)
{
Stream fileStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(tempJsonStr));
Message newMessage = await botClient.SendDocumentAsync(
chatId: chatId,
document: new Telegram.Bot.Types.InputFiles.InputOnlineFile(content: fileStream, fileName: $"export-{i}.json"),
caption: $"export-{i}.json");
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.ToString());
List<Models.Message> tempList = (await DbContext.QueryByGroupId(groupId: chatId.ToString(), new Pager(page: i, pageSize: 50))).ToList();
string tempJsonStr = Utils.JsonUtil.Obj2JsonStr(tempList);

try
{
Stream fileStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(tempJsonStr));
Message newMessage = await botClient.SendDocumentAsync(
chatId: chatId,
document: new Telegram.Bot.Types.InputFiles.InputOnlineFile(content: fileStream, fileName: $"export-{i}.json"),
caption: $"export-{i}.json");
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
}
}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions plugins/TgBotStatPlugin/TgBotStatPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<PropertyGroup>
<PackageId>PluginCore.TgBotStatPlugin</PackageId>
<Version>0.1.3</Version>
<FileVersion>0.1.3.0</FileVersion>
<Version>0.1.4</Version>
<FileVersion>0.1.4.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Company>yiyun</Company>
<Authors>yiyun</Authors>
Expand Down
7 changes: 6 additions & 1 deletion plugins/TgBotStatPlugin/Utils/JsonUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace TgBotStatPlugin.Utils
Expand All @@ -20,7 +21,11 @@ public static string Obj2JsonStr(object jsonObj)
{
//return JsonConvert.SerializeObject(jsonObj);
return JsonSerializer.Serialize(jsonObj,
new JsonSerializerOptions { WriteIndented = true });
new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
}
#endregion

Expand Down
2 changes: 1 addition & 1 deletion plugins/TgBotStatPlugin/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"DisplayName": "TgBotStatPlugin",
"Description": "TgBotStatPlugin",
"Author": "yiyun",
"Version": "0.1.3",
"Version": "0.1.4",
"SupportedVersions": ["0.0.1"],
"DependPlugins": ["TelegramPlugin"]
}
2 changes: 1 addition & 1 deletion plugins/TgBotStatPlugin/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TgBotStatPlugin-v0.1.3</title>
<title>TgBotStatPlugin-v0.1.4</title>
<link
href="/plugins/TgBotStatPlugin/libs/mdui/css/mdui.min.css"
rel="stylesheet"
Expand Down

0 comments on commit a6ef9cb

Please sign in to comment.