From 72a1fa3c99f8f7d0f267c7294ba7630095ec6b57 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sun, 15 May 2022 15:41:24 +0200 Subject: [PATCH] Replacing Client in ResultBase and subclasses - replace Client with Device to allow more access - making Client in DeviceSession public to allow access from outside --- TelegramBotBase/Base/DataResult.cs | 28 +++++++++++------------ TelegramBotBase/Base/ResultBase.cs | 11 +-------- TelegramBotBase/Sessions/DeviceSession.cs | 2 +- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/TelegramBotBase/Base/DataResult.cs b/TelegramBotBase/Base/DataResult.cs index 8fc9e34..fab04bd 100644 --- a/TelegramBotBase/Base/DataResult.cs +++ b/TelegramBotBase/Base/DataResult.cs @@ -109,7 +109,7 @@ namespace TelegramBotBase.Base public async Task DownloadDocument() { var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize.Value); - var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent); + var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent); return new InputOnlineFile(encryptedContent, this.Document.FileName); } @@ -122,9 +122,9 @@ namespace TelegramBotBase.Base /// public async Task DownloadDocument(String path) { - var file = await this.Client.TelegramClient.GetFileAsync(this.Document.FileId); + var file = await Device.Client.TelegramClient.GetFileAsync(this.Document.FileId); FileStream fs = new FileStream(path, FileMode.Create); - await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); + await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); fs.Close(); fs.Dispose(); } @@ -136,7 +136,7 @@ namespace TelegramBotBase.Base public async Task DownloadRawDocument() { MemoryStream ms = new MemoryStream(); - await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms); + await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms); return ms.ToArray(); } @@ -156,7 +156,7 @@ namespace TelegramBotBase.Base public async Task DownloadRawTextDocument(Encoding encoding) { MemoryStream ms = new MemoryStream(); - await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms); + await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms); ms.Position = 0; @@ -168,16 +168,16 @@ namespace TelegramBotBase.Base public async Task DownloadVideo() { var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize.Value); - var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent); + var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent); return new InputOnlineFile(encryptedContent, ""); } public async Task DownloadVideo(String path) { - var file = await this.Client.TelegramClient.GetFileAsync(this.Video.FileId); + var file = await Device.Client.TelegramClient.GetFileAsync(this.Video.FileId); FileStream fs = new FileStream(path, FileMode.Create); - await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); + await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); fs.Close(); fs.Dispose(); } @@ -185,16 +185,16 @@ namespace TelegramBotBase.Base public async Task DownloadAudio() { var encryptedContent = new System.IO.MemoryStream(this.Audio.FileSize.Value); - var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Audio.FileId, encryptedContent); + var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Audio.FileId, encryptedContent); return new InputOnlineFile(encryptedContent, ""); } public async Task DownloadAudio(String path) { - var file = await this.Client.TelegramClient.GetFileAsync(this.Audio.FileId); + var file = await Device.Client.TelegramClient.GetFileAsync(this.Audio.FileId); FileStream fs = new FileStream(path, FileMode.Create); - await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); + await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); fs.Close(); fs.Dispose(); } @@ -203,7 +203,7 @@ namespace TelegramBotBase.Base { var photo = this.Photos[index]; var encryptedContent = new System.IO.MemoryStream(photo.FileSize.Value); - var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent); + var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent); return new InputOnlineFile(encryptedContent, ""); } @@ -211,9 +211,9 @@ namespace TelegramBotBase.Base public async Task DownloadPhoto(int index, String path) { var photo = this.Photos[index]; - var file = await this.Client.TelegramClient.GetFileAsync(photo.FileId); + var file = await Device.Client.TelegramClient.GetFileAsync(photo.FileId); FileStream fs = new FileStream(path, FileMode.Create); - await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); + await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs); fs.Close(); fs.Dispose(); } diff --git a/TelegramBotBase/Base/ResultBase.cs b/TelegramBotBase/Base/ResultBase.cs index 5cce472..adf8ecc 100644 --- a/TelegramBotBase/Base/ResultBase.cs +++ b/TelegramBotBase/Base/ResultBase.cs @@ -10,15 +10,6 @@ namespace TelegramBotBase.Base { public class ResultBase : EventArgs { - public MessageClient Client - { - get - { - return Device.ActiveForm.Client; - } - } - - public DeviceSession Device { get; @@ -56,7 +47,7 @@ namespace TelegramBotBase.Base { try { - await this.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, (messageId == -1 ? this.MessageId : messageId)); + await Device.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, (messageId == -1 ? this.MessageId : messageId)); } catch { diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 945abfd..bbe4e5a 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -84,7 +84,7 @@ namespace TelegramBotBase.Sessions /// public Message LastMessage { get; set; } - private MessageClient Client + public MessageClient Client { get {