Skip to content

Commit

Permalink
feat(plugins/tgbotstatplugin): 0.1.5 : 接收消息
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Aug 12, 2023
1 parent a442939 commit 0b70831
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
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.4.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.5.README" style="border:0" alt="" />
<!-- End Matomo -->
73 changes: 70 additions & 3 deletions plugins/TgBotStatPlugin/TgBotStatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
using System.Linq;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Polling;
using Telegram.Bot.Types.Enums;

namespace TgBotStatPlugin
{
public class TgBotStatPlugin : BasePlugin, ITelegramBotPlugin
public class TgBotStatPlugin : BasePlugin, ITimeJobPlugin
{
private static bool _isInit = false;

public override (bool IsSuccess, string Message) AfterEnable()
{
Console.WriteLine($"{nameof(TgBotStatPlugin)}: {nameof(AfterEnable)}");
Expand All @@ -26,12 +30,75 @@ public override (bool IsSuccess, string Message) BeforeDisable()
return base.BeforeDisable();
}

public void HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken, string botToken)
public override void AppStart()
{

}

public override List<string> AppStartOrderDependPlugins
{
get
{
return new List<string>(){
"TelegramPlugin",
};
}
}

public long SecondsPeriod => 180;

public async Task ExecuteAsync()
{
try
{
#region 初始化
if (!_isInit)
{
if (TelegramPlugin.TelegramBotStore.Bots != null && TelegramPlugin.TelegramBotStore.Bots.Count >= 1)
{
foreach (var item in TelegramPlugin.TelegramBotStore.Bots)
{
if (item.TelegramBotClient != null)
{
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
ReceiverOptions receiverOptions = new()
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types
};

item.TelegramBotClient.StartReceiving(
updateHandler: async (ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) =>
{
await HandleUpdateAsync(botClient, update, cancellationToken, "");
},
pollingErrorHandler: async (ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) =>
{
await HandlePollingErrorAsync(botClient, exception, cancellationToken, "");
},
receiverOptions: receiverOptions
);

}
}
}
_isInit = true;
}
#endregion
}
catch (Exception ex)
{
Console.WriteLine($"执行定时任务失败: {ex.ToString()}");
}

await Task.CompletedTask;
}

public async Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken, string botToken)
{

}

public async void HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken, string botToken)
public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken, string botToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
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.4</Version>
<FileVersion>0.1.4.0</FileVersion>
<Version>0.1.5</Version>
<FileVersion>0.1.5.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Company>yiyun</Company>
<Authors>yiyun</Authors>
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.4",
"Version": "0.1.5",
"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.4</title>
<title>TgBotStatPlugin-v0.1.5</title>
<link
href="/plugins/TgBotStatPlugin/libs/mdui/css/mdui.min.css"
rel="stylesheet"
Expand Down

0 comments on commit 0b70831

Please sign in to comment.