fix: some build & linter warnings

This commit is contained in:
ZavaruKitsu
2022-10-08 19:15:51 +03:00
parent 3f0d109fe2
commit a731e2a8d0
159 changed files with 2738 additions and 3742 deletions
-1
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TelegramBotBase.Base
+15 -32
View File
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
{
@@ -11,17 +8,11 @@ namespace TelegramBotBase.Base
/// </summary>
public class ControlBase
{
public Sessions.DeviceSession Device { get; set; }
public DeviceSession Device { get; set; }
public int ID { get; set; }
public int Id { get; set; }
public String ControlID
{
get
{
return "#c" + this.ID.ToString();
}
}
public string ControlId => "#c" + Id;
/// <summary>
/// Defines if the control should be rendered and invoked with actions
@@ -37,43 +28,35 @@ namespace TelegramBotBase.Base
}
public virtual async Task Load(MessageResult result)
public virtual Task Load(MessageResult result)
{
return Task.CompletedTask;
}
public virtual async Task Action(MessageResult result, String value = null)
public virtual Task Action(MessageResult result, string value = null)
{
return Task.CompletedTask;
}
public virtual async Task Render(MessageResult result)
public virtual Task Render(MessageResult result)
{
return Task.CompletedTask;
}
public virtual async Task Hidden(bool FormClose)
public virtual Task Hidden(bool formClose)
{
return Task.CompletedTask;
}
/// <summary>
/// Will be called on a cleanup.
/// </summary>
/// <returns></returns>
public virtual async Task Cleanup()
public virtual Task Cleanup()
{
return Task.CompletedTask;
}
}
+46 -100
View File
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
namespace TelegramBotBase.Base
@@ -21,98 +20,45 @@ namespace TelegramBotBase.Base
public UpdateResult UpdateData { get; set; }
public Contact Contact
{
get
{
return this.Message.Contact;
}
}
public Contact Contact => Message.Contact;
public Location Location
{
get
{
return this.Message.Location;
}
}
public Location Location => Message.Location;
public Document Document
{
get
{
return this.Message.Document;
}
}
public Document Document => Message.Document;
public Audio Audio
{
get
{
return this.Message.Audio;
}
}
public Audio Audio => Message.Audio;
public Video Video
{
get
{
return this.Message.Video;
}
}
public Video Video => Message.Video;
public PhotoSize[] Photos
{
get
{
return this.Message.Photo;
}
}
public PhotoSize[] Photos => Message.Photo;
public Telegram.Bot.Types.Enums.MessageType Type
{
get
{
return this.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
}
}
public MessageType Type => Message?.Type ?? MessageType.Unknown;
public override Message Message
{
get
{
return this.UpdateData?.Message;
}
}
public override Message Message => UpdateData?.Message;
/// <summary>
/// Returns the FileId of the first reachable element.
/// </summary>
public String FileId
{
get
{
return (this.Document?.FileId ??
this.Audio?.FileId ??
this.Video?.FileId ??
this.Photos.FirstOrDefault()?.FileId);
}
}
public string FileId =>
(Document?.FileId ??
Audio?.FileId ??
Video?.FileId ??
Photos.FirstOrDefault()?.FileId);
public DataResult(UpdateResult update)
{
this.UpdateData = update;
UpdateData = update;
}
public async Task<InputOnlineFile> DownloadDocument()
{
var encryptedContent = new System.IO.MemoryStream();
encryptedContent.SetLength(this.Document.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent);
var encryptedContent = new MemoryStream();
encryptedContent.SetLength(Document.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, this.Document.FileName);
return new InputOnlineFile(encryptedContent, Document.FileName);
}
@@ -121,10 +67,10 @@ namespace TelegramBotBase.Base
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public async Task DownloadDocument(String path)
public async Task DownloadDocument(string path)
{
var file = await Device.Client.TelegramClient.GetFileAsync(this.Document.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
var file = await Device.Client.TelegramClient.GetFileAsync(Document.FileId);
var fs = new FileStream(path, FileMode.Create);
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
@@ -136,8 +82,8 @@ namespace TelegramBotBase.Base
/// <returns></returns>
public async Task<byte[]> DownloadRawDocument()
{
MemoryStream ms = new MemoryStream();
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
var ms = new MemoryStream();
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId, ms);
return ms.ToArray();
}
@@ -145,7 +91,7 @@ namespace TelegramBotBase.Base
/// Downloads a document and returns it as string. (txt,csv,etc) Default encoding ist UTF8.
/// </summary>
/// <returns></returns>
public async Task<String> DownloadRawTextDocument()
public async Task<string> DownloadRawTextDocument()
{
return await DownloadRawTextDocument(Encoding.UTF8);
}
@@ -154,10 +100,10 @@ namespace TelegramBotBase.Base
/// Downloads a document and returns it as string. (txt,csv,etc)
/// </summary>
/// <returns></returns>
public async Task<String> DownloadRawTextDocument(Encoding encoding)
public async Task<string> DownloadRawTextDocument(Encoding encoding)
{
MemoryStream ms = new MemoryStream();
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
var ms = new MemoryStream();
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId, ms);
ms.Position = 0;
@@ -168,17 +114,17 @@ namespace TelegramBotBase.Base
public async Task<InputOnlineFile> DownloadVideo()
{
var encryptedContent = new System.IO.MemoryStream();
encryptedContent.SetLength(this.Video.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent);
var encryptedContent = new MemoryStream();
encryptedContent.SetLength(Video.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Video.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadVideo(String path)
public async Task DownloadVideo(string path)
{
var file = await Device.Client.TelegramClient.GetFileAsync(this.Video.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
var file = await Device.Client.TelegramClient.GetFileAsync(Video.FileId);
var fs = new FileStream(path, FileMode.Create);
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
@@ -186,17 +132,17 @@ namespace TelegramBotBase.Base
public async Task<InputOnlineFile> DownloadAudio()
{
var encryptedContent = new System.IO.MemoryStream();
encryptedContent.SetLength(this.Audio.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Audio.FileId, encryptedContent);
var encryptedContent = new MemoryStream();
encryptedContent.SetLength(Audio.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Audio.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadAudio(String path)
public async Task DownloadAudio(string path)
{
var file = await Device.Client.TelegramClient.GetFileAsync(this.Audio.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
var file = await Device.Client.TelegramClient.GetFileAsync(Audio.FileId);
var fs = new FileStream(path, FileMode.Create);
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
@@ -204,19 +150,19 @@ namespace TelegramBotBase.Base
public async Task<InputOnlineFile> DownloadPhoto(int index)
{
var photo = this.Photos[index];
var encryptedContent = new System.IO.MemoryStream();
var photo = Photos[index];
var encryptedContent = new MemoryStream();
encryptedContent.SetLength(photo.FileSize.Value);
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadPhoto(int index, String path)
public async Task DownloadPhoto(int index, string path)
{
var photo = this.Photos[index];
var photo = Photos[index];
var file = await Device.Client.TelegramClient.GetFileAsync(photo.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
var fs = new FileStream(path, FileMode.Create);
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
+66 -82
View File
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
@@ -27,7 +26,7 @@ namespace TelegramBotBase.Form
/// <summary>
/// has this formular already been disposed ?
/// </summary>
public bool IsDisposed { get; set; } = false;
public bool IsDisposed { get; set; }
public List<ControlBase> Controls { get; set; }
@@ -35,33 +34,33 @@ namespace TelegramBotBase.Form
public EventHandlerList Events = new EventHandlerList();
private static object __evInit = new object();
private static readonly object EvInit = new object();
private static object __evOpened = new object();
private static readonly object EvOpened = new object();
private static object __evClosed = new object();
private static readonly object EvClosed = new object();
public FormBase()
{
this.Controls = new List<Base.ControlBase>();
Controls = new List<ControlBase>();
}
public FormBase(MessageClient Client) : this()
public FormBase(MessageClient client) : this()
{
this.Client = Client;
this.Client = client;
}
public async Task OnInit(InitEventArgs e)
{
var handler = this.Events[__evInit]?.GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
var handler = Events[EvInit]?.GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<InitEventArgs>(h, this, e);
await h.InvokeAllAsync(this, e);
}
}
@@ -70,27 +69,21 @@ namespace TelegramBotBase.Form
///// </summary>
public event AsyncEventHandler<InitEventArgs> Init
{
add
{
this.Events.AddHandler(__evInit, value);
}
remove
{
this.Events.RemoveHandler(__evInit, value);
}
add => Events.AddHandler(EvInit, value);
remove => Events.RemoveHandler(EvInit, value);
}
public async Task OnOpened(EventArgs e)
{
var handler = this.Events[__evOpened]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
var handler = Events[EvOpened]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<EventArgs>(h, this, e);
await h.InvokeAllAsync(this, e);
}
}
@@ -100,27 +93,21 @@ namespace TelegramBotBase.Form
/// <returns></returns>
public event AsyncEventHandler<EventArgs> Opened
{
add
{
this.Events.AddHandler(__evOpened, value);
}
remove
{
this.Events.RemoveHandler(__evOpened, value);
}
add => Events.AddHandler(EvOpened, value);
remove => Events.RemoveHandler(EvOpened, value);
}
public async Task OnClosed(EventArgs e)
{
var handler = this.Events[__evClosed]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
var handler = Events[EvClosed]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<EventArgs>(h, this, e);
await h.InvokeAllAsync(this, e);
}
}
@@ -131,14 +118,8 @@ namespace TelegramBotBase.Form
/// <returns></returns>
public event AsyncEventHandler<EventArgs> Closed
{
add
{
this.Events.AddHandler(__evClosed, value);
}
remove
{
this.Events.RemoveHandler(__evClosed, value);
}
add => Events.AddHandler(EvClosed, value);
remove => Events.RemoveHandler(EvClosed, value);
}
/// <summary>
@@ -146,9 +127,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task ReturnFromModal(ModalDialog modal)
public virtual Task ReturnFromModal(ModalDialog modal)
{
return Task.CompletedTask;
}
@@ -156,17 +137,19 @@ namespace TelegramBotBase.Form
/// Pre to form close, cleanup all controls
/// </summary>
/// <returns></returns>
public async Task CloseControls()
public Task CloseControls()
{
foreach (var b in this.Controls)
foreach (var b in Controls)
{
b.Cleanup().Wait();
}
return Task.CompletedTask;
}
public virtual async Task PreLoad(MessageResult message)
public virtual Task PreLoad(MessageResult message)
{
return Task.CompletedTask;
}
/// <summary>
@@ -179,7 +162,7 @@ namespace TelegramBotBase.Form
//Looking for the control by id, if not listened, raise event for all
if (message.RawData?.StartsWith("#c") ?? false)
{
var c = this.Controls.FirstOrDefault(a => a.ControlID == message.RawData.Split('_')[0]);
var c = Controls.FirstOrDefault(a => a.ControlId == message.RawData.Split('_')[0]);
if (c != null)
{
await c.Load(message);
@@ -187,7 +170,7 @@ namespace TelegramBotBase.Form
}
}
foreach (var b in this.Controls)
foreach (var b in Controls)
{
if (!b.Enabled)
continue;
@@ -201,9 +184,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Load(MessageResult message)
public virtual Task Load(MessageResult message)
{
return Task.CompletedTask;
}
/// <summary>
@@ -211,9 +194,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Edited(MessageResult message)
public virtual Task Edited(MessageResult message)
{
return Task.CompletedTask;
}
@@ -227,7 +210,7 @@ namespace TelegramBotBase.Form
//Looking for the control by id, if not listened, raise event for all
if (message.RawData.StartsWith("#c"))
{
var c = this.Controls.FirstOrDefault(a => a.ControlID == message.RawData.Split('_')[0]);
var c = Controls.FirstOrDefault(a => a.ControlId == message.RawData.Split('_')[0]);
if (c != null)
{
await c.Action(message, message.RawData.Split('_')[1]);
@@ -235,7 +218,7 @@ namespace TelegramBotBase.Form
}
}
foreach (var b in this.Controls)
foreach (var b in Controls)
{
if (!b.Enabled)
continue;
@@ -252,9 +235,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Action(MessageResult message)
public virtual Task Action(MessageResult message)
{
return Task.CompletedTask;
}
/// <summary>
@@ -262,9 +245,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task SentData(DataResult message)
public virtual Task SentData(DataResult message)
{
return Task.CompletedTask;
}
/// <summary>
@@ -274,7 +257,7 @@ namespace TelegramBotBase.Form
/// <returns></returns>
public virtual async Task RenderControls(MessageResult message)
{
foreach (var b in this.Controls)
foreach (var b in Controls)
{
if (!b.Enabled)
continue;
@@ -288,9 +271,9 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Render(MessageResult message)
public virtual Task Render(MessageResult message)
{
return Task.CompletedTask;
}
@@ -302,7 +285,7 @@ namespace TelegramBotBase.Form
/// <returns></returns>
public virtual async Task NavigateTo(FormBase newForm, params object[] args)
{
DeviceSession ds = this.Device;
var ds = Device;
if (ds == null)
return;
@@ -311,11 +294,11 @@ namespace TelegramBotBase.Form
ds.PreviousForm = ds.ActiveForm;
ds.ActiveForm = newForm;
newForm.Client = this.Client;
newForm.Client = Client;
newForm.Device = ds;
//Notify prior to close
foreach (var b in this.Controls)
foreach (var b in Controls)
{
if (!b.Enabled)
continue;
@@ -323,13 +306,13 @@ namespace TelegramBotBase.Form
await b.Hidden(true);
}
this.CloseControls().Wait();
CloseControls().Wait();
await this.OnClosed(new EventArgs());
await OnClosed(EventArgs.Empty);
await newForm.OnInit(new InitEventArgs(args));
await newForm.OnOpened(new EventArgs());
await newForm.OnOpened(EventArgs.Empty);
}
/// <summary>
@@ -339,7 +322,7 @@ namespace TelegramBotBase.Form
/// <returns></returns>
public virtual async Task OpenModal(ModalDialog newForm, params object[] args)
{
DeviceSession ds = this.Device;
var ds = Device;
if (ds == null)
return;
@@ -359,7 +342,7 @@ namespace TelegramBotBase.Form
await CloseModal(newForm, parentForm);
};
foreach (var b in this.Controls)
foreach (var b in Controls)
{
if (!b.Enabled)
continue;
@@ -369,14 +352,14 @@ namespace TelegramBotBase.Form
await newForm.OnInit(new InitEventArgs(args));
await newForm.OnOpened(new EventArgs());
await newForm.OnOpened(EventArgs.Empty);
}
public async Task CloseModal(ModalDialog modalForm, FormBase oldForm)
public Task CloseModal(ModalDialog modalForm, FormBase oldForm)
{
DeviceSession ds = this.Device;
var ds = Device;
if (ds == null)
return;
return Task.CompletedTask;
if (modalForm == null)
throw new Exception("No modal form");
@@ -386,6 +369,7 @@ namespace TelegramBotBase.Form
ds.PreviousForm = ds.ActiveForm;
ds.ActiveForm = oldForm;
return Task.CompletedTask;
}
/// <summary>
@@ -395,12 +379,12 @@ namespace TelegramBotBase.Form
public void AddControl(ControlBase control)
{
//Duplicate check
if (this.Controls.Contains(control))
if (Controls.Contains(control))
throw new ArgumentException("Control has been already added.");
control.ID = this.Controls.Count + 1;
control.Device = this.Device;
this.Controls.Add(control);
control.Id = Controls.Count + 1;
control.Device = Device;
Controls.Add(control);
control.Init();
}
@@ -411,12 +395,12 @@ namespace TelegramBotBase.Form
/// <param name="control"></param>
public void RemoveControl(ControlBase control)
{
if (!this.Controls.Contains(control))
if (!Controls.Contains(control))
return;
control.Cleanup().Wait();
this.Controls.Remove(control);
Controls.Remove(control);
}
/// <summary>
@@ -424,11 +408,11 @@ namespace TelegramBotBase.Form
/// </summary>
public void RemoveAllControls()
{
foreach(var c in this.Controls)
foreach(var c in Controls)
{
c.Cleanup().Wait();
this.Controls.Remove(c);
Controls.Remove(c);
}
}
@@ -437,9 +421,9 @@ namespace TelegramBotBase.Form
/// </summary>
public void Dispose()
{
this.Client = null;
this.Device = null;
this.IsDisposed = true;
Client = null;
Device = null;
IsDisposed = true;
}
}
}
+38 -50
View File
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Exceptions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
namespace TelegramBotBase.Base
{
@@ -22,13 +19,13 @@ namespace TelegramBotBase.Base
{
public String APIKey { get; set; }
public string ApiKey { get; set; }
public ITelegramBotClient TelegramClient { get; set; }
private EventHandlerList __Events { get; set; } = new EventHandlerList();
private EventHandlerList Events { get; set; } = new EventHandlerList();
private static object __evOnMessageLoop = new object();
private static readonly object EvOnMessageLoop = new object();
private static object __evOnMessage = new object();
@@ -36,21 +33,21 @@ namespace TelegramBotBase.Base
private static object __evCallbackQuery = new object();
CancellationTokenSource __cancellationTokenSource;
private CancellationTokenSource _cancellationTokenSource;
public MessageClient(String APIKey)
public MessageClient(string apiKey)
{
this.APIKey = APIKey;
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey);
this.ApiKey = apiKey;
TelegramClient = new TelegramBotClient(apiKey);
Prepare();
}
public MessageClient(String APIKey, HttpClient proxy)
public MessageClient(string apiKey, HttpClient proxy)
{
this.APIKey = APIKey;
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
this.ApiKey = apiKey;
TelegramClient = new TelegramBotClient(apiKey, proxy);
Prepare();
@@ -58,9 +55,9 @@ namespace TelegramBotBase.Base
public MessageClient(String APIKey, Uri proxyUrl, NetworkCredential credential = null)
public MessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential = null)
{
this.APIKey = APIKey;
this.ApiKey = apiKey;
var proxy = new WebProxy(proxyUrl)
{
@@ -71,7 +68,7 @@ namespace TelegramBotBase.Base
new HttpClientHandler { Proxy = proxy, UseProxy = true }
);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
TelegramClient = new TelegramBotClient(apiKey, httpClient);
Prepare();
}
@@ -79,12 +76,12 @@ namespace TelegramBotBase.Base
/// <summary>
/// Initializes the client with a proxy
/// </summary>
/// <param name="APIKey"></param>
/// <param name="apiKey"></param>
/// <param name="proxyHost">i.e. 127.0.0.1</param>
/// <param name="proxyPort">i.e. 10000</param>
public MessageClient(String APIKey, String proxyHost, int proxyPort)
public MessageClient(string apiKey, string proxyHost, int proxyPort)
{
this.APIKey = APIKey;
this.ApiKey = apiKey;
var proxy = new WebProxy(proxyHost, proxyPort);
@@ -92,17 +89,17 @@ namespace TelegramBotBase.Base
new HttpClientHandler { Proxy = proxy, UseProxy = true }
);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
TelegramClient = new TelegramBotClient(apiKey, httpClient);
Prepare();
}
public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client)
public MessageClient(string apiKey, TelegramBotClient client)
{
this.APIKey = APIKey;
this.TelegramClient = Client;
this.ApiKey = apiKey;
TelegramClient = client;
Prepare();
}
@@ -110,7 +107,7 @@ namespace TelegramBotBase.Base
public void Prepare()
{
this.TelegramClient.Timeout = new TimeSpan(0, 0, 30);
TelegramClient.Timeout = new TimeSpan(0, 0, 30);
}
@@ -118,19 +115,16 @@ namespace TelegramBotBase.Base
public void StartReceiving()
{
__cancellationTokenSource = new CancellationTokenSource();
_cancellationTokenSource = new CancellationTokenSource();
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
var receiverOptions = new ReceiverOptions();
this.TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, __cancellationTokenSource.Token);
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token);
}
public void StopReceiving()
{
__cancellationTokenSource.Cancel();
_cancellationTokenSource.Cancel();
}
@@ -143,9 +137,9 @@ namespace TelegramBotBase.Base
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
if (exception is ApiRequestException exAPI)
if (exception is ApiRequestException exApi)
{
Console.WriteLine($"Telegram API Error:\n[{exAPI.ErrorCode}]\n{exAPI.Message}");
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
}
else
{
@@ -159,9 +153,9 @@ namespace TelegramBotBase.Base
/// This will return the current list of bot commands.
/// </summary>
/// <returns></returns>
public async Task<BotCommand[]> GetBotCommands(BotCommandScope scope = null, String languageCode = null)
public async Task<BotCommand[]> GetBotCommands(BotCommandScope scope = null, string languageCode = null)
{
return await this.TelegramClient.GetMyCommandsAsync(scope, languageCode);
return await TelegramClient.GetMyCommandsAsync(scope, languageCode);
}
@@ -170,18 +164,18 @@ namespace TelegramBotBase.Base
/// </summary>
/// <param name="botcommands"></param>
/// <returns></returns>
public async Task SetBotCommands(List<BotCommand> botcommands, BotCommandScope scope = null, String languageCode = null)
public async Task SetBotCommands(List<BotCommand> botcommands, BotCommandScope scope = null, string languageCode = null)
{
await this.TelegramClient.SetMyCommandsAsync(botcommands, scope, languageCode);
await TelegramClient.SetMyCommandsAsync(botcommands, scope, languageCode);
}
/// <summary>
/// This will delete the current list of bot commands.
/// </summary>
/// <returns></returns>
public async Task DeleteBotCommands(BotCommandScope scope = null, String languageCode = null)
public async Task DeleteBotCommands(BotCommandScope scope = null, string languageCode = null)
{
await this.TelegramClient.DeleteMyCommandsAsync(scope, languageCode);
await TelegramClient.DeleteMyCommandsAsync(scope, languageCode);
}
@@ -191,19 +185,13 @@ namespace TelegramBotBase.Base
public event Async.AsyncEventHandler<UpdateResult> MessageLoop
{
add
{
this.__Events.AddHandler(__evOnMessageLoop, value);
}
remove
{
this.__Events.RemoveHandler(__evOnMessageLoop, value);
}
add => Events.AddHandler(EvOnMessageLoop, value);
remove => Events.RemoveHandler(EvOnMessageLoop, value);
}
public void OnMessageLoop(UpdateResult update)
{
(this.__Events[__evOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
(Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
}
+31 -91
View File
@@ -1,110 +1,56 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
using Telegram.Bot.Types.Enums;
namespace TelegramBotBase.Base
{
public class MessageResult : ResultBase
{
public Telegram.Bot.Types.Update UpdateData { get; set; }
public Update UpdateData { get; set; }
/// <summary>
/// Returns the Device/ChatId
/// </summary>
public override long DeviceId
{
get
{
return this.UpdateData?.Message?.Chat?.Id
?? this.UpdateData?.EditedMessage?.Chat.Id
?? this.UpdateData?.CallbackQuery.Message?.Chat.Id
?? Device?.DeviceId
?? 0;
}
}
public override long DeviceId =>
UpdateData?.Message?.Chat?.Id
?? UpdateData?.EditedMessage?.Chat.Id
?? UpdateData?.CallbackQuery.Message?.Chat.Id
?? Device?.DeviceId
?? 0;
/// <summary>
/// The message id
/// </summary>
public new int MessageId
{
get
{
return this.UpdateData?.Message?.MessageId
?? this.Message?.MessageId
?? this.UpdateData?.CallbackQuery?.Message?.MessageId
?? 0;
}
}
public new int MessageId =>
UpdateData?.Message?.MessageId
?? Message?.MessageId
?? UpdateData?.CallbackQuery?.Message?.MessageId
?? 0;
public String Command
{
get
{
return this.UpdateData?.Message?.Text ?? "";
}
}
public string Command => UpdateData?.Message?.Text ?? "";
public String MessageText
{
get
{
return this.UpdateData?.Message?.Text ?? "";
}
}
public string MessageText => UpdateData?.Message?.Text ?? "";
public Telegram.Bot.Types.Enums.MessageType MessageType
{
get
{
return Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
}
}
public Message Message
{
get
{
return this.UpdateData?.Message
?? this.UpdateData?.EditedMessage
?? this.UpdateData?.ChannelPost
?? this.UpdateData?.EditedChannelPost
?? this.UpdateData?.CallbackQuery?.Message;
}
}
public MessageType MessageType => Message?.Type ?? MessageType.Unknown;
/// <summary>
/// Is this an action ? (i.e. button click)
/// </summary>
public bool IsAction
{
get
{
return (this.UpdateData.CallbackQuery != null);
}
}
public bool IsAction => (UpdateData.CallbackQuery != null);
/// <summary>
/// Is this a command ? Starts with a slash '/' and a command
/// </summary>
public bool IsBotCommand
{
get
{
return (this.MessageText.StartsWith("/"));
}
}
public bool IsBotCommand => (MessageText.StartsWith("/"));
/// <summary>
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789)
/// </summary>
public List<String> BotCommandParameters
public List<string> BotCommandParameters
{
get
{
@@ -112,21 +58,21 @@ namespace TelegramBotBase.Base
return new List<string>();
//Split by empty space and skip first entry (command itself), return as list
return this.MessageText.Split(' ').Skip(1).ToList();
return MessageText.Split(' ').Skip(1).ToList();
}
}
/// <summary>
/// Returns just the command (i.e. /start 1 2 3 => /start)
/// </summary>
public String BotCommand
public string BotCommand
{
get
{
if (!IsBotCommand)
return null;
return this.MessageText.Split(' ')[0];
return MessageText.Split(' ')[0];
}
}
@@ -137,13 +83,7 @@ namespace TelegramBotBase.Base
public bool Handled { get; set; } = false;
public String RawData
{
get
{
return this.UpdateData?.CallbackQuery?.Data;
}
}
public string RawData => UpdateData?.CallbackQuery?.Data;
public T GetData<T>()
where T : class
@@ -151,7 +91,7 @@ namespace TelegramBotBase.Base
T cd = null;
try
{
cd = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(this.RawData);
cd = JsonConvert.DeserializeObject<T>(RawData);
return cd;
}
@@ -168,16 +108,16 @@ namespace TelegramBotBase.Base
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ConfirmAction(String message = "", bool showAlert = false, String urlToOpen = null)
public async Task ConfirmAction(string message = "", bool showAlert = false, string urlToOpen = null)
{
await this.Device.ConfirmAction(this.UpdateData.CallbackQuery.Id, message, showAlert, urlToOpen);
await Device.ConfirmAction(UpdateData.CallbackQuery.Id, message, showAlert, urlToOpen);
}
public override async Task DeleteMessage()
{
try
{
await base.DeleteMessage(this.MessageId);
await base.DeleteMessage(MessageId);
}
catch
{
@@ -190,9 +130,9 @@ namespace TelegramBotBase.Base
}
public MessageResult(Telegram.Bot.Types.Update update)
public MessageResult(Update update)
{
this.UpdateData = update;
UpdateData = update;
}
+5 -13
View File
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
@@ -18,15 +16,9 @@ namespace TelegramBotBase.Base
public virtual long DeviceId { get; set; }
public int MessageId
{
get
{
return this.Message.MessageId;
}
}
public int MessageId => Message.MessageId;
public virtual Telegram.Bot.Types.Message Message { get; set; }
public virtual Message Message { get; set; }
/// <summary>
/// Deletes the current message
@@ -35,7 +27,7 @@ namespace TelegramBotBase.Base
/// <returns></returns>
public virtual async Task DeleteMessage()
{
await DeleteMessage(this.MessageId);
await DeleteMessage(MessageId);
}
/// <summary>
@@ -47,7 +39,7 @@ namespace TelegramBotBase.Base
{
try
{
await Device.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, (messageId == -1 ? this.MessageId : messageId));
await Device.Client.TelegramClient.DeleteMessageAsync(DeviceId, (messageId == -1 ? MessageId : messageId));
}
catch
{
+3 -5
View File
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TelegramBotBase.Base
{
public partial class StateContainer
public class StateContainer
{
public List<StateEntry> States { get; set; }
@@ -27,7 +25,7 @@ namespace TelegramBotBase.Base
public StateContainer()
{
this.States = new List<StateEntry>();
States = new List<StateEntry>();
}
}
+6 -9
View File
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
namespace TelegramBotBase.Base
{
@@ -18,26 +15,26 @@ namespace TelegramBotBase.Base
/// <summary>
/// Contains the Username (on privat chats) or Group title on groups/channels.
/// </summary>
public String ChatTitle { get; set; }
public string ChatTitle { get; set; }
/// <summary>
/// Contains additional values to save.
/// </summary>
public Dictionary<String, object> Values { get; set; }
public Dictionary<string, object> Values { get; set; }
/// <summary>
/// Contains the full qualified namespace of the form to used for reload it via reflection.
/// </summary>
public String FormUri {get;set;}
public string FormUri {get;set;}
/// <summary>
/// Contains the assembly, where to find that form.
/// </summary>
public String QualifiedName { get; set; }
public string QualifiedName { get; set; }
public StateEntry()
{
this.Values = new Dictionary<string, object>();
Values = new Dictionary<string, object>();
}
}
+13 -37
View File
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telegram.Bot.Types;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
@@ -14,46 +10,26 @@ namespace TelegramBotBase.Base
RawData = rawData;
Device = device;
}
/// <summary>
/// Returns the Device/ChatId
/// </summary>
public override long DeviceId
{
get
{
return this.RawData?.Message?.Chat?.Id
?? this.RawData?.CallbackQuery?.Message?.Chat?.Id
?? Device?.DeviceId
?? 0;
}
}
public override long DeviceId =>
RawData?.Message?.Chat?.Id
?? RawData?.CallbackQuery?.Message?.Chat?.Id
?? Device?.DeviceId
?? 0;
public Update RawData { get; set; }
public override Message Message
{
get
{
return RawData?.Message
?? RawData?.EditedMessage
?? RawData?.ChannelPost
?? RawData?.EditedChannelPost
?? RawData?.CallbackQuery?.Message;
}
}
public DeviceSession Device
{
get;
set;
}
public override Message Message =>
RawData?.Message
?? RawData?.EditedMessage
?? RawData?.ChannelPost
?? RawData?.EditedChannelPost
?? RawData?.CallbackQuery?.Message;
}
}