-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
579 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 5 additions & 7 deletions
12
...MossbauerLab.Watcher/Config/MailConfig.cs → ...n/Data/Notification/MailSendRequisites.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
....Watcher/Wissance.MossbauerLab.Watcher.Common/Data/Notification/TelegramSendRequisites.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Wissance.MossbauerLab.Watcher.Common.Data.Notification | ||
{ | ||
public class TelegramSendRequisites | ||
{ | ||
public TelegramSendRequisites() | ||
{ | ||
} | ||
|
||
public TelegramSendRequisites(string group, string botKey, string templateFilePath) | ||
{ | ||
Group = group; | ||
BotKey = botKey; | ||
TemplateFilePath = templateFilePath; | ||
} | ||
|
||
public string Group { get; set; } | ||
public string BotKey { get; set; } | ||
public string TemplateFilePath { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
....Watcher/Wissance.MossbauerLab.Watcher.Common/Wissance.MossbauerLab.Watcher.Common.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="Config\**" /> | ||
<EmbeddedResource Remove="Config\**" /> | ||
<None Remove="Config\**" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...tcher/Wissance.MossbauerLab.Watcher.Services/Notification/NotificationMessageFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using Wissance.MossbauerLab.Watcher.Common.Data; | ||
|
||
namespace Wissance.MossbauerLab.Watcher.Services.Notification | ||
{ | ||
internal static class NotificationMessageFormatter | ||
{ | ||
|
||
public static string FormatMailMessage(string template, IList<SpectrumReadyData> spectra) | ||
{ | ||
string mailMessage = template.Replace(CurrentSatePlaceholder, DateTime.Now.ToString("yyyy-MM-dd:HH-mm-ss")); | ||
IList<string> lines = spectra.Select(s => string.Format(SavedSpectrumDescriptionTemplate, s.Name, s.Channel, s.RawInfo.LastWriteTime.ToString("yyyy-MM-dd:HH-mm-ss"))).ToList(); | ||
string linesStr = string.Join(Environment.NewLine, lines); | ||
mailMessage = mailMessage.Replace(AutosavedSpectraPlaceholder, linesStr); | ||
|
||
return mailMessage; | ||
} | ||
public static string FormatTelegramMessage(string template, IList<SpectrumReadyData> spectra) | ||
{ | ||
string mailMessage = template.Replace(CurrentSatePlaceholder, DateTime.Now.ToString("yyyy-MM-dd:HH-mm-ss")); | ||
IList<string> lines = spectra.Select(s => string.Format(SavedSpectrumTelegramDescriptionTemplate, s.Name, s.Channel, s.RawInfo.LastWriteTime.ToString("yyyy-MM-dd:HH-mm-ss"))).ToList(); | ||
string linesStr = string.Join(Environment.NewLine, lines); | ||
mailMessage = mailMessage.Replace(AutosavedSpectraPlaceholder, linesStr); | ||
|
||
return mailMessage; | ||
} | ||
|
||
private const string AutosavedSpectraPlaceholder = "{savedSpectra}"; | ||
// <!--<li>Спектр {msSpName} по каналу {msChNumber} сохранен {msSaveDate}</li>--> | ||
private const string SavedSpectrumDescriptionTemplate = "<li>Спектр {0} по каналу {1} сохранен {2}</li>"; | ||
private const string CurrentSatePlaceholder = "{currDate}"; | ||
private const string SavedSpectrumTelegramDescriptionTemplate = "- Спектр {0} по каналу {1} сохранен {2}"; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...sbauerLab.Watcher/Wissance.MossbauerLab.Watcher.Services/Notification/TelegramNotifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using Telegram.Bot; | ||
using Telegram.Bot.Polling; | ||
using Telegram.Bot.Requests; | ||
using Telegram.Bot.Types; | ||
|
||
using Wissance.MossbauerLab.Watcher.Common.Data; | ||
using Wissance.MossbauerLab.Watcher.Common.Data.Notification; | ||
|
||
namespace Wissance.MossbauerLab.Watcher.Services.Notification | ||
{ | ||
public class TelegramNotifier : ISpectrumReadyNotifier | ||
{ | ||
|
||
public TelegramNotifier(TelegramSendRequisites tgRequisites, ILoggerFactory loggerFactory) | ||
{ | ||
_tgRequisites = tgRequisites; | ||
_logger = loggerFactory.CreateLogger<TelegramNotifier>(); | ||
} | ||
public async Task<bool> NotifySpectrumSavedAsync(IList<SpectrumReadyData> spectra) | ||
{ | ||
ITelegramBotClient client = new TelegramBotClient(_tgRequisites.BotKey); | ||
|
||
string targetGroupName = _tgRequisites.Group; | ||
ChatId targetChatId = new ChatId(targetGroupName); | ||
|
||
Message msg = new Message(); | ||
string template = !string.IsNullOrEmpty(_tgRequisites.TemplateFilePath) ? _tgRequisites.TemplateFilePath : DefaultSpectrumAutoSaveMailTemplate; | ||
string mailTemplate = System.IO.File.ReadAllText(template); | ||
msg.Text = NotificationMessageFormatter.FormatTelegramMessage(mailTemplate, spectra); | ||
|
||
try | ||
{ | ||
await client.SendTextMessageAsync(targetChatId, msg.Text); | ||
} | ||
catch (Exception e ) | ||
{ | ||
_logger.LogError($"An error occurred during sending message to telegram group {targetGroupName}: {e.Message}"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private const string DefaultSpectrumAutoSaveMailTemplate = @"Notification/Templates/tgAutosaveDone.txt"; | ||
private readonly TelegramSendRequisites _tgRequisites; | ||
private readonly ILogger<TelegramNotifier> _logger; | ||
|
||
} | ||
} |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
....Watcher/Wissance.MossbauerLab.Watcher.Services/Notification/Templates/tgAutosaveDone.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Уведомление | ||
На {currDate} были получены следующие наиболее актуальные спектры, файлы этих спектров отправлены на почту | ||
{savedSpectra} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...cher/Wissance.MossbauerLab.Watcher.Services/Wissance.MossbauerLab.Watcher.Services.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Quartz" Version="3.4.0" /> | ||
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.4.0" /> | ||
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.4.0" /> | ||
<PackageReference Include="SMBLibrary" Version="1.4.8" /> | ||
<PackageReference Include="Telegram.Bot" Version="18.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Wissance.MossbauerLab.Watcher.Common\Wissance.MossbauerLab.Watcher.Common.csproj" /> | ||
<ProjectReference Include="..\Wissance.MossbauerLab.Watcher.Data\Wissance.MossbauerLab.Watcher.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="Notification/Templates/**" CopyToOutputDirectory="PreserveNewest" LinkBase="Templates/"/> | ||
</ItemGroup> | ||
|
||
</Project> |
42 changes: 42 additions & 0 deletions
42
...ce.MossbauerLab.Watcher/Wissance.MossbauerLab.Watcher.Tests/TelegramNotificationsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
using System; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
|
||
using Wissance.MossbauerLab.Watcher.Common.Data; | ||
using Wissance.MossbauerLab.Watcher.Services.Notification; | ||
using Microsoft.QualityTools.Testing.Fakes; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Wissance.MossbauerLab.Watcher.Common.Data.Notification; | ||
|
||
namespace Wissance.MossbauerLab.Watcher.Services.Tests | ||
{ | ||
[TestClass] | ||
public class TelegramNotificationsTests | ||
{ | ||
[TestMethod] | ||
public async Task SendNotificationTest() | ||
{ | ||
byte[] bin = Convert.FromBase64String("NjI1MzUyNzMxNjpBQUYzWGZuSnE2azlTMnFldTc2bmd6SHhEU29id3BMcm50SQ=="); | ||
string key = Encoding.UTF8.GetString(bin); | ||
|
||
TelegramSendRequisites tgRequisites = new TelegramSendRequisites("@WissanceBotTest", key, "Templates\\testTelegramMessageTemplate.txt"); | ||
|
||
SpectrumReadyData spectra = new SpectrumReadyData | ||
{ | ||
Spectrum = new byte[] { 4, 5, 6 }, | ||
Name = "Test spectra", | ||
Channel = 1, | ||
Updated = DateTime.Now, | ||
RawInfo = new System.IO.FileInfo("textFileForFileInfo.txt") | ||
}; | ||
|
||
TelegramNotifier telegramNotifier = new TelegramNotifier(tgRequisites, new LoggerFactory()); | ||
bool result = await telegramNotifier.NotifySpectrumSavedAsync(new List<SpectrumReadyData> { spectra, spectra }); | ||
Assert.IsTrue(result); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.