Merge pull request #65 from MajMcCloud/development

Migrating development branch into master
This commit is contained in:
Florian Zevedei 2024-06-29 18:30:34 +02:00 committed by GitHub
commit 261fdafd96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 384 additions and 292 deletions

View File

@ -8,12 +8,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9"/> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.9"/> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj"/> <ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -14,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="IronSoftware.System.Drawing" Version="2024.1.1" /> <PackageReference Include="IronSoftware.System.Drawing" Version="2024.5.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1"> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -16,7 +16,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.3" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1"> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -22,7 +22,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="TelegramBotBase" Version="6.0.0" /> <PackageReference Include="TelegramBotBase" Version="6.0.0" />
<PackageReference Include="Npgsql" Version="8.0.1" /> <PackageReference Include="Npgsql" Version="8.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -9,16 +10,33 @@ namespace TelegramBotBase.Base;
public class MessageResult : ResultBase public class MessageResult : ResultBase
{ {
internal MessageResult()
{
}
public MessageResult(Update update) public MessageResult(Update update)
{ {
UpdateData = update; UpdateData = update;
init();
} }
public Update UpdateData { get; set; } void init()
{
IsAction = UpdateData.CallbackQuery != null;
IsBotCommand = Message.Entities?.Any(a => a.Type == MessageEntityType.BotCommand) ?? false;
if (!IsBotCommand)
return;
BotCommand = MessageText.Split(' ')[0];
IsBotGroupCommand = BotCommand.Contains("@");
if (IsBotGroupCommand)
{
BotCommand = BotCommand.Substring(0, BotCommand.LastIndexOf('@'));
}
}
public Update UpdateData { get; private set; }
/// <summary> /// <summary>
/// Returns the Device/ChatId /// Returns the Device/ChatId
@ -55,12 +73,17 @@ public class MessageResult : ResultBase
/// <summary> /// <summary>
/// Is this an action ? (i.e. button click) /// Is this an action ? (i.e. button click)
/// </summary> /// </summary>
public bool IsAction => UpdateData.CallbackQuery != null; public bool IsAction { get; private set; }
/// <summary> /// <summary>
/// Is this a command ? Starts with a slash '/' and a command /// Is this a command ? Starts with a slash '/' and a command
/// </summary> /// </summary>
public bool IsBotCommand => MessageText.StartsWith("/"); public bool IsBotCommand { get; private set; }
/// <summary>
/// Is this a bot command sent from a group via @BotId ?
/// </summary>
public bool IsBotGroupCommand { get; private set; }
/// <summary> /// <summary>
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => /// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 =>
@ -83,18 +106,7 @@ public class MessageResult : ResultBase
/// <summary> /// <summary>
/// Returns just the command (i.e. /start 1 2 3 => /start) /// Returns just the command (i.e. /start 1 2 3 => /start)
/// </summary> /// </summary>
public string BotCommand public string BotCommand { get; private set; }
{
get
{
if (!IsBotCommand)
{
return null;
}
return MessageText.Split(' ')[0];
}
}
/// <summary> /// <summary>
/// Returns if this message will be used on the first form or not. /// Returns if this message will be used on the first form or not.

View File

@ -8,6 +8,7 @@ using Telegram.Bot.Types;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Enums; using TelegramBotBase.Enums;
using TelegramBotBase.Exceptions;
using TelegramBotBase.Interfaces; using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions; using TelegramBotBase.Sessions;
using Console = TelegramBotBase.Tools.Console; using Console = TelegramBotBase.Tools.Console;
@ -139,6 +140,13 @@ public sealed class BotBase
mr.IsFirstHandler = false; mr.IsFirstHandler = false;
} while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10)); } while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10));
} }
catch (InvalidServiceProviderConfiguration ex)
{
var ds = Sessions.GetSession(e.DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, e.DeviceId, ds, ex));
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
var ds = Sessions.GetSession(e.DeviceId); var ds = Sessions.GetSession(e.DeviceId);
@ -189,13 +197,10 @@ public sealed class BotBase
/// <param name="deviceId">Contains the device/chat id of the device to update.</param> /// <param name="deviceId">Contains the device/chat id of the device to update.</param>
public async Task InvokeMessageLoop(long deviceId) public async Task InvokeMessageLoop(long deviceId)
{ {
var mr = new MessageResult var mr = new MessageResult(new Update
{ {
UpdateData = new Update Message = new Message()
{ });
Message = new Message()
}
};
await InvokeMessageLoop(deviceId, mr); await InvokeMessageLoop(deviceId, mr);
} }
@ -260,7 +265,7 @@ public sealed class BotBase
{ {
foreach (var scope in BotCommandScopes) foreach (var scope in BotCommandScopes)
{ {
if (scope.Value.Any(a => "/" + a.Command == command)) if (scope.Value.Any(a => Constants.Telegram.BotCommandIndicator + a.Command == command))
{ {
return true; return true;
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Telegram.Bot.Types; using Telegram.Bot.Types;
@ -20,6 +21,16 @@ public static class Extensions
scope = BotCommandScope.Default(); scope = BotCommandScope.Default();
} }
if (string.IsNullOrEmpty(command))
{
throw new ArgumentNullException(nameof(command), $"{nameof(command)} parameter can not be null or empty");
}
if(command.StartsWith(Constants.Telegram.BotCommandIndicator))
{
throw new ArgumentException($"{nameof(command)} parameter does not have to start with a slash, please remove.", $"{nameof(command)}");
}
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type); var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
if (item.Value != null) if (item.Value != null)

View File

@ -16,4 +16,15 @@ public static class Telegram
public const int MaxReplyKeyboardCols = 12; public const int MaxReplyKeyboardCols = 12;
public const int MessageDeletionsPerSecond = 30; public const int MessageDeletionsPerSecond = 30;
/// <summary>
/// The maximum length of callback data. Will raise an exception of it exceeds it.
/// </summary>
public const int MaxCallBackDataBytes = 64;
/// <summary>
/// The slash constant which indicates a bot command.
/// </summary>
public const string BotCommandIndicator = "/";
} }

View File

@ -51,7 +51,24 @@ public class ButtonGrid : ControlBase
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } = ""; public string ConfirmationText { get; set; } = "";
@ -340,14 +357,14 @@ public class ButtonGrid : ControlBase
} }
//var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText) //var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText)
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText); // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText);
// bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText) // bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText)
//var index = bf.FindRowByButton(button); //var index = bf.FindRowByButton(button);
check: check:
//Remove button click message //Remove button click message
if (DeleteReplyMessage) if (DeleteReplyMessage)
@ -449,15 +466,15 @@ public class ButtonGrid : ControlBase
} }
//var bf = DataSource.ButtonForm; //var bf = DataSource.ButtonForm;
//var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) //var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
// ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData); // ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData);
//var index = bf.FindRowByButton(button); //var index = bf.FindRowByButton(button);
check: check:
if (match != null) if (match != null)
{ {
await result.ConfirmAction(ConfirmationText ?? ""); await result.ConfirmAction(ConfirmationText ?? "");
@ -506,13 +523,13 @@ public class ButtonGrid : ControlBase
if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !EnablePaging) if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !EnablePaging)
{ {
throw new MaximumRowsReachedException throw new MaximumRowsReachedException
{ Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows }; { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows };
} }
if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols) if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols)
{ {
throw new MaximumColsException throw new MaximumColsException
{ Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols }; { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols };
} }
break; break;
@ -522,13 +539,13 @@ public class ButtonGrid : ControlBase
if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !EnablePaging) if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !EnablePaging)
{ {
throw new MaximumRowsReachedException throw new MaximumRowsReachedException
{ Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows }; { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows };
} }
if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols) if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols)
{ {
throw new MaximumColsException throw new MaximumColsException
{ Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols }; { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols };
} }
break; break;
@ -693,7 +710,7 @@ public class ButtonGrid : ControlBase
Updated(); Updated();
} }
else else
//Remove event handler //Remove event handler
{ {
Device.MessageDeleted -= Device_MessageDeleted; Device.MessageDeleted -= Device_MessageDeleted;
} }

View File

@ -13,6 +13,7 @@ using TelegramBotBase.Enums;
using TelegramBotBase.Exceptions; using TelegramBotBase.Exceptions;
using TelegramBotBase.Form; using TelegramBotBase.Form;
using TelegramBotBase.Localizations; using TelegramBotBase.Localizations;
using static System.Net.Mime.MediaTypeNames;
using static TelegramBotBase.Base.Async; using static TelegramBotBase.Base.Async;
namespace TelegramBotBase.Controls.Hybrid; namespace TelegramBotBase.Controls.Hybrid;
@ -51,7 +52,24 @@ public class CheckedButtonList : ControlBase
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } = ""; public string ConfirmationText { get; set; } = "";

View File

@ -67,7 +67,24 @@ public class TaggedButtonGrid : MultiView
DataSource = new ButtonFormDataSource(form); DataSource = new ButtonFormDataSource(form);
} }
public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; string m_Title = Default.Language["ButtonGrid_Title"];
public string Title
{
get
{
return m_Title;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
m_Title = value;
}
}
public string ConfirmationText { get; set; } public string ConfirmationText { get; set; }

View File

@ -16,7 +16,7 @@ public class Label : ControlBase
{ {
private bool _renderNecessary = true; private bool _renderNecessary = true;
private string _text = string.Empty; private string _text = Default.Language["Label_Text"];
public String Text public String Text
{ {
@ -29,6 +29,10 @@ public class Label : ControlBase
if (_text == value) if (_text == value)
return; return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Text)}", $"{nameof(Text)} property must have been a value unequal to null/empty");
}
_text = value; _text = value;
_renderNecessary = true; _renderNecessary = true;
@ -36,6 +40,8 @@ public class Label : ControlBase
} }
} }
private ParseMode _parseMode = ParseMode.Markdown; private ParseMode _parseMode = ParseMode.Markdown;
public ParseMode ParseMode public ParseMode ParseMode

View File

@ -32,10 +32,32 @@ public class MultiToggleButton : ControlBase
/// </summary> /// </summary>
public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"]; public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"];
private string _title = Default.Language["MultiToggleButton_Title"];
/// <summary> /// <summary>
/// This holds the title of the control. /// This holds the title of the control.
/// </summary> /// </summary>
public string Title { get; set; } = Default.Language["MultiToggleButton_Title"]; public String Title
{
get
{
return _title;
}
set
{
if (_title == value)
return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} must have been a value unequal to null/empty");
}
_title = value;
_renderNecessary = true;
}
}
public int? MessageId { get; set; } public int? MessageId { get; set; }

View File

@ -36,7 +36,31 @@ public class ToggleButton : ControlBase
public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"]; public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"];
public string Title { get; set; } = Default.Language["ToggleButton_Title"]; private string _title = Default.Language["ToggleButton_Title"];
public String Title
{
get
{
return _title;
}
set
{
if (_title == value)
return;
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
}
_title = value;
_renderNecessary = true;
}
}
public int? MessageId { get; set; } public int? MessageId { get; set; }

View File

@ -0,0 +1,35 @@
using System;
using Telegram.Bot.Exceptions;
namespace TelegramBotBase.Exceptions;
public sealed class CallbackDataTooLongException : Exception
{
static ApiRequestException _innerException = new Telegram.Bot.Exceptions.ApiRequestException("Bad Request: BUTTON_DATA_INVALID", 400);
static String _message = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
static String _message_with_bytes = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data with @current_callback_bytes@ bytes.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
public CallbackDataTooLongException() : base(_message, _innerException)
{
}
/// <summary>
///
/// </summary>
/// <param name="current_callback_bytes">The amount of callback bytes generated.</param>
public CallbackDataTooLongException(int current_callback_bytes) : base(getMessage(current_callback_bytes), _innerException)
{
}
static String getMessage(int current_callback_bytes = -1)
{
if (current_callback_bytes == -1)
return _message;
return _message_with_bytes.Replace("@current_callback_bytes@", current_callback_bytes.ToString());
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Exceptions
{
public sealed class InvalidServiceProviderConfiguration : Exception
{
public InvalidServiceProviderConfiguration(string message, Exception innerException) : base(message, innerException) { }
}
}

View File

@ -1,6 +1,7 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using TelegramBotBase.DependencyInjection; using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Exceptions;
using TelegramBotBase.Form; using TelegramBotBase.Form;
using TelegramBotBase.Interfaces; using TelegramBotBase.Interfaces;
@ -24,7 +25,16 @@ public class ServiceProviderStartFormFactory : IStartFormFactory
public FormBase CreateForm() public FormBase CreateForm()
{ {
var fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass); FormBase fb = null;
try
{
fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
}
catch(InvalidOperationException ex)
{
throw new InvalidServiceProviderConfiguration(ex.Message, ex);
}
//Sets an internal field for future ServiceProvider navigation //Sets an internal field for future ServiceProvider navigation
fb.SetServiceProvider(_serviceProvider); fb.SetServiceProvider(_serviceProvider);

View File

@ -1,4 +1,6 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Text;
using TelegramBotBase.Exceptions;
namespace TelegramBotBase.Form; namespace TelegramBotBase.Form;
@ -23,22 +25,24 @@ public class CallbackData
public static string Create(string method, string value) public static string Create(string method, string value)
{ {
return new CallbackData(method, value).Serialize(); return new CallbackData(method, value).Serialize(true);
} }
/// <summary> /// <summary>
/// Serializes data to json string /// Serializes data to json string
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public string Serialize() public string Serialize(bool throwExceptionOnOverflow = false)
{ {
var s = ""; var s = string.Empty;
try
{ s = JsonConvert.SerializeObject(this);
s = JsonConvert.SerializeObject(this);
} //Is data over 64 bytes ?
catch int byte_count = Encoding.UTF8.GetByteCount(s);
if (throwExceptionOnOverflow && byte_count > Constants.Telegram.MaxCallBackDataBytes)
{ {
throw new CallbackDataTooLongException(byte_count);
} }
return s; return s;
@ -51,19 +55,8 @@ public class CallbackData
/// <returns></returns> /// <returns></returns>
public static CallbackData Deserialize(string data) public static CallbackData Deserialize(string data)
{ {
CallbackData cd = null; return JsonConvert.DeserializeObject<CallbackData>(data);
try
{
cd = JsonConvert.DeserializeObject<CallbackData>(data);
return cd;
}
catch
{
}
return null;
} }
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(); public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(true);
} }

View File

@ -34,5 +34,6 @@ public sealed class English : Localization
Values["ToggleButton_Changed"] = "Setting changed"; Values["ToggleButton_Changed"] = "Setting changed";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Default label text";
} }
} }

View File

@ -34,5 +34,6 @@ public sealed class German : Localization
Values["ToggleButton_Changed"] = "Einstellung geändert"; Values["ToggleButton_Changed"] = "Einstellung geändert";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Standard Label Text";
} }
} }

View File

@ -34,6 +34,7 @@ public sealed class Persian : Localization
Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد"; Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "متن برچسب پیش‌فرض";
} }
} }

View File

@ -34,5 +34,6 @@ public sealed class Russian : Localization
Values["ToggleButton_Changed"] = "Настройки изменены"; Values["ToggleButton_Changed"] = "Настройки изменены";
Values["ButtonGrid_SearchIcon"] = "🔍"; Values["ButtonGrid_SearchIcon"] = "🔍";
Values["TaggedButtonGrid_TagIcon"] = "📁"; Values["TaggedButtonGrid_TagIcon"] = "📁";
Values["Label_Text"] = "Текст метки по умолчанию";
} }
} }

View File

@ -79,13 +79,16 @@ public class FormBaseMessageLoop : IMessageLoopFactory
} }
//Action Event //Action Event
if (!session.FormSwitched && mr.IsAction) if (!session.FormSwitched && mr.IsAction && !mr.Handled)
{ {
//Send Action event to controls //Send Action event to controls
await activeForm.ActionControls(mr); await activeForm.ActionControls(mr);
//Send Action event to form itself if (!mr.Handled)
await activeForm.Action(mr); {
//Send Action event to form itself, if not already handled by a control
await activeForm.Action(mr);
}
if (!mr.Handled) if (!mr.Handled)
{ {

View File

@ -72,13 +72,16 @@ public class FullMessageLoop : IMessageLoopFactory
} }
//Action Event //Action Event
if (!session.FormSwitched && mr.IsAction) if (!session.FormSwitched && mr.IsAction && !mr.Handled)
{ {
//Send Action event to controls //Send Action event to controls
await activeForm.ActionControls(mr); await activeForm.ActionControls(mr);
//Send Action event to form itself if (!mr.Handled)
await activeForm.Action(mr); {
//Send Action event to form itself, if not already handled by a control
await activeForm.Action(mr);
}
if (!mr.Handled) if (!mr.Handled)
{ {

View File

@ -131,13 +131,9 @@ public class DeviceSession : IDeviceSession
public async Task ConfirmAction(string callbackQueryId, string message = "", bool showAlert = false, public async Task ConfirmAction(string callbackQueryId, string message = "", bool showAlert = false,
string urlToOpen = null) string urlToOpen = null)
{ {
try
{ await Client.TelegramClient.AnswerCallbackQueryAsync(callbackQueryId, message, showAlert, urlToOpen);
await Client.TelegramClient.AnswerCallbackQueryAsync(callbackQueryId, message, showAlert, urlToOpen);
}
catch
{
}
} }
/// <summary> /// <summary>
@ -157,17 +153,8 @@ public class DeviceSession : IDeviceSession
throw new MessageTooLongException(text.Length); throw new MessageTooLongException(text.Length);
} }
try
{
return await Api(a =>
a.EditMessageTextAsync(DeviceId, messageId, text, parseMode, replyMarkup: markup));
}
catch
{
}
return await Api(a => a.EditMessageTextAsync(DeviceId, messageId, text, parseMode, replyMarkup: markup));
return null;
} }
/// <summary> /// <summary>
@ -185,17 +172,9 @@ public class DeviceSession : IDeviceSession
throw new MessageTooLongException(text.Length); throw new MessageTooLongException(text.Length);
} }
try
{
return await Api(a =>
a.EditMessageTextAsync(DeviceId, messageId, text, parseMode, replyMarkup: markup));
}
catch
{
}
return await Api(a => a.EditMessageTextAsync(DeviceId, messageId, text, parseMode, replyMarkup: markup));
return null;
} }
/// <summary> /// <summary>
@ -215,18 +194,8 @@ public class DeviceSession : IDeviceSession
throw new MessageTooLongException(message.Text.Length); throw new MessageTooLongException(message.Text.Length);
} }
try return await Api(a => a.EditMessageTextAsync(DeviceId, message.MessageId, message.Text, parseMode,
{ replyMarkup: markup));
return await Api(a =>
a.EditMessageTextAsync(DeviceId, message.MessageId, message.Text, parseMode,
replyMarkup: markup));
}
catch
{
}
return null;
} }
/// <summary> /// <summary>
@ -237,15 +206,8 @@ public class DeviceSession : IDeviceSession
/// <returns></returns> /// <returns></returns>
public async Task<Message> EditReplyMarkup(int messageId, ButtonForm bf) public async Task<Message> EditReplyMarkup(int messageId, ButtonForm bf)
{ {
try
{
return await Api(a => a.EditMessageReplyMarkupAsync(DeviceId, messageId, bf));
}
catch
{
}
return null; return await Api(a => a.EditMessageReplyMarkupAsync(DeviceId, messageId, bf));
} }
/// <summary> /// <summary>
@ -277,21 +239,16 @@ public class DeviceSession : IDeviceSession
text = text.MarkdownV2Escape(); text = text.MarkdownV2Escape();
} }
try
{
var t = Api(a => a.SendTextMessageAsync(deviceId, text, null, parseMode, replyToMessageId: replyTo,
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendTextMessageAsync(deviceId, text, null, parseMode, replyToMessageId: replyTo,
replyMarkup: markup, disableNotification: disableNotification));
await OnMessageSent(new MessageSentEventArgs(await t, o)); var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -336,20 +293,15 @@ public class DeviceSession : IDeviceSession
text = text.MarkdownV2Escape(); text = text.MarkdownV2Escape();
} }
try
{
var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -379,20 +331,15 @@ public class DeviceSession : IDeviceSession
text = text.MarkdownV2Escape(); text = text.MarkdownV2Escape();
} }
try
{
var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -414,20 +361,15 @@ public class DeviceSession : IDeviceSession
InlineKeyboardMarkup markup = buttons; InlineKeyboardMarkup markup = buttons;
try
{
var t = Api(a => a.SendPhotoAsync(DeviceId, file, null, caption, parseMode, replyToMessageId: replyTo,
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendPhotoAsync(DeviceId, file, null, caption, parseMode, replyToMessageId: replyTo,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -449,21 +391,16 @@ public class DeviceSession : IDeviceSession
InlineKeyboardMarkup markup = buttons; InlineKeyboardMarkup markup = buttons;
try
{
var t = Api(a => a.SendVideoAsync(DeviceId, file, caption: caption, parseMode: parseMode,
replyToMessageId: replyTo, replyMarkup: markup,
disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendVideoAsync(DeviceId, file, caption: caption, parseMode: parseMode,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyToMessageId: replyTo, replyMarkup: markup,
disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -484,21 +421,16 @@ public class DeviceSession : IDeviceSession
InlineKeyboardMarkup markup = buttons; InlineKeyboardMarkup markup = buttons;
try
{
var t = Api(a => a.SendVideoAsync(DeviceId, InputFile.FromUri(url), parseMode: parseMode,
replyToMessageId: replyTo, replyMarkup: markup,
disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendVideoAsync(DeviceId, InputFile.FromUri(url), parseMode: parseMode,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyToMessageId: replyTo, replyMarkup: markup,
disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -520,24 +452,19 @@ public class DeviceSession : IDeviceSession
InlineKeyboardMarkup markup = buttons; InlineKeyboardMarkup markup = buttons;
try
{
var ms = new MemoryStream(video);
var fts = InputFile.FromStream(ms, filename); var ms = new MemoryStream(video);
var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo, var fts = InputFile.FromStream(ms, filename);
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -560,26 +487,21 @@ public class DeviceSession : IDeviceSession
InlineKeyboardMarkup markup = buttons; InlineKeyboardMarkup markup = buttons;
try
{
var fs = new FileStream(filepath, FileMode.Open);
var filename = Path.GetFileName(filepath); var fs = new FileStream(filepath, FileMode.Open);
var fts = InputFile.FromStream(fs, filename); var filename = Path.GetFileName(filepath);
var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo, var fts = InputFile.FromStream(fs, filename);
replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo,
await OnMessageSent(new MessageSentEventArgs(await t, o)); replyMarkup: markup, disableNotification: disableNotification));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -650,20 +572,15 @@ public class DeviceSession : IDeviceSession
markup = buttons; markup = buttons;
} }
try
{
var t = Api(a => a.SendDocumentAsync(DeviceId, document, null, null, caption, replyMarkup: markup,
disableNotification: disableNotification, replyToMessageId: replyTo));
var o = GetOrigin(new StackTrace()); var t = Api(a => a.SendDocumentAsync(DeviceId, document, null, null, caption, replyMarkup: markup,
await OnMessageSent(new MessageSentEventArgs(await t, o)); disableNotification: disableNotification, replyToMessageId: replyTo));
var o = GetOrigin(new StackTrace());
await OnMessageSent(new MessageSentEventArgs(await t, o));
return await t;
return await t;
}
catch
{
return null;
}
} }
/// <summary> /// <summary>
@ -714,22 +631,16 @@ public class DeviceSession : IDeviceSession
public async Task<Message> HideReplyKeyboard(string closedMsg = "Closed", bool autoDeleteResponse = true) public async Task<Message> HideReplyKeyboard(string closedMsg = "Closed", bool autoDeleteResponse = true)
{ {
try
{
var m = await Send(closedMsg, new ReplyKeyboardRemove());
if (autoDeleteResponse && m != null) var m = await Send(closedMsg, new ReplyKeyboardRemove());
{
await DeleteMessage(m);
}
return m; if (autoDeleteResponse && m != null)
}
catch
{ {
await DeleteMessage(m);
} }
return null; return m;
} }
/// <summary> /// <summary>
@ -759,13 +670,9 @@ public class DeviceSession : IDeviceSession
public virtual async Task ChangeChatPermissions(ChatPermissions permissions) public virtual async Task ChangeChatPermissions(ChatPermissions permissions)
{ {
try
{ await Api(a => a.SetChatPermissionsAsync(DeviceId, permissions));
await Api(a => a.SetChatPermissionsAsync(DeviceId, permissions));
}
catch
{
}
} }
private Type GetOrigin(StackTrace stackTrace) private Type GetOrigin(StackTrace stackTrace)
@ -878,49 +785,31 @@ public class DeviceSession : IDeviceSession
public virtual async Task<ChatMember> GetChatUser(long userId) public virtual async Task<ChatMember> GetChatUser(long userId)
{ {
try
{
return await Api(a => a.GetChatMemberAsync(DeviceId, userId));
}
catch
{
}
return null; return await Api(a => a.GetChatMemberAsync(DeviceId, userId));
} }
[Obsolete("User BanUser instead.")] [Obsolete("User BanUser instead.")]
public virtual async Task KickUser(long userId, DateTime until = default) public virtual async Task KickUser(long userId, DateTime until = default)
{ {
try
{ await Api(a => a.BanChatMemberAsync(DeviceId, userId, until));
await Api(a => a.BanChatMemberAsync(DeviceId, userId, until));
}
catch
{
}
} }
public virtual async Task BanUser(long userId, DateTime until = default) public virtual async Task BanUser(long userId, DateTime until = default)
{ {
try
{ await Api(a => a.BanChatMemberAsync(DeviceId, userId, until));
await Api(a => a.BanChatMemberAsync(DeviceId, userId, until));
}
catch
{
}
} }
public virtual async Task UnbanUser(long userId) public virtual async Task UnbanUser(long userId)
{ {
try
{ await Api(a => a.UnbanChatMemberAsync(DeviceId, userId));
await Api(a => a.UnbanChatMemberAsync(DeviceId, userId));
}
catch
{
}
} }
#endregion #endregion