Adding IDeviceSession and IDeviceSessionMethods Interface

This commit is contained in:
Florian Zevedei 2024-10-15 15:32:32 +02:00
parent bc1f1fc93d
commit db5dd7862c
17 changed files with 169 additions and 39 deletions

View File

@ -187,6 +187,7 @@ namespace TelegramBotBase
var sourceCode = $$""" var sourceCode = $$"""
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions; using TelegramBotBase.Sessions;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Extensions; using Telegram.Bot.Extensions;
@ -232,7 +233,7 @@ namespace TelegramBotBase
sb.AppendLine(xml_comments); sb.AppendLine(xml_comments);
sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this DeviceSession device, {parameters})"); sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this IDeviceSession device, {parameters})");
sb.AppendLine($" {{"); sb.AppendLine($" {{");

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Args; namespace TelegramBotBase.Args;
@ -15,7 +15,7 @@ public class BotCommandEventArgs : EventArgs
} }
public BotCommandEventArgs(string command, List<string> parameters, Message message, long deviceId, public BotCommandEventArgs(string command, List<string> parameters, Message message, long deviceId,
DeviceSession device) IDeviceSession device)
{ {
Command = command; Command = command;
Parameters = parameters; Parameters = parameters;
@ -30,7 +30,7 @@ public class BotCommandEventArgs : EventArgs
public long DeviceId { get; set; } public long DeviceId { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public bool Handled { get; set; } = false; public bool Handled { get; set; } = false;

View File

@ -1,11 +1,11 @@
using System; using System;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
public class MessageIncomeEventArgs : EventArgs public class MessageIncomeEventArgs : EventArgs
{ {
public MessageIncomeEventArgs(long deviceId, DeviceSession device, MessageResult message) public MessageIncomeEventArgs(long deviceId, IDeviceSession device, MessageResult message)
{ {
DeviceId = deviceId; DeviceId = deviceId;
Device = device; Device = device;
@ -14,7 +14,7 @@ public class MessageIncomeEventArgs : EventArgs
public long DeviceId { get; set; } public long DeviceId { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public MessageResult Message { get; set; } public MessageResult Message { get; set; }
} }

View File

@ -1,11 +1,11 @@
using System; using System;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
public class SessionBeginEventArgs : EventArgs public class SessionBeginEventArgs : EventArgs
{ {
public SessionBeginEventArgs(long deviceId, DeviceSession device) public SessionBeginEventArgs(long deviceId, IDeviceSession device)
{ {
DeviceId = deviceId; DeviceId = deviceId;
Device = device; Device = device;
@ -13,5 +13,5 @@ public class SessionBeginEventArgs : EventArgs
public long DeviceId { get; set; } public long DeviceId { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
} }

View File

@ -1,5 +1,5 @@
using System; using System;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Args; namespace TelegramBotBase.Args;
@ -9,7 +9,7 @@ public class SystemExceptionEventArgs : EventArgs
{ {
} }
public SystemExceptionEventArgs(string command, long deviceId, DeviceSession device, Exception error) public SystemExceptionEventArgs(string command, long deviceId, IDeviceSession device, Exception error)
{ {
Command = command; Command = command;
DeviceId = deviceId; DeviceId = deviceId;
@ -21,7 +21,7 @@ public class SystemExceptionEventArgs : EventArgs
public long DeviceId { get; set; } public long DeviceId { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public Exception Error { get; set; } public Exception Error { get; set; }
} }

View File

@ -1,6 +1,6 @@
using System; using System;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Args; namespace TelegramBotBase.Args;
@ -12,7 +12,7 @@ public class UnhandledCallEventArgs : EventArgs
} }
public UnhandledCallEventArgs(string command, string rawData, long deviceId, int messageId, Message message, public UnhandledCallEventArgs(string command, string rawData, long deviceId, int messageId, Message message,
DeviceSession device) : this() IDeviceSession device) : this()
{ {
Command = command; Command = command;
RawData = rawData; RawData = rawData;
@ -26,7 +26,7 @@ public class UnhandledCallEventArgs : EventArgs
public long DeviceId { get; set; } public long DeviceId { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public string RawData { get; set; } public string RawData { get; set; }

View File

@ -1,5 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
@ -8,7 +8,7 @@ namespace TelegramBotBase.Base;
/// </summary> /// </summary>
public class ControlBase public class ControlBase
{ {
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public int Id { get; set; } public int Id { get; set; }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Form.Navigation; using TelegramBotBase.Form.Navigation;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
using static TelegramBotBase.Base.Async; using static TelegramBotBase.Base.Async;
namespace TelegramBotBase.Form; namespace TelegramBotBase.Form;
@ -28,7 +28,7 @@ public class FormBase : IDisposable
public NavigationController NavigationController { get; set; } public NavigationController NavigationController { get; set; }
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public MessageClient Client { get; set; } public MessageClient Client { get; set; }

View File

@ -2,13 +2,13 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
public class ResultBase : EventArgs public class ResultBase : EventArgs
{ {
public DeviceSession Device { get; set; } public IDeviceSession Device { get; set; }
public virtual long DeviceId { get; set; } public virtual long DeviceId { get; set; }

View File

@ -1,11 +1,11 @@
using Telegram.Bot.Types; using Telegram.Bot.Types;
using TelegramBotBase.Sessions; using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Base; namespace TelegramBotBase.Base;
public class UpdateResult : ResultBase public class UpdateResult : ResultBase
{ {
public UpdateResult(Update rawData, DeviceSession device) public UpdateResult(Update rawData, IDeviceSession device)
{ {
RawData = rawData; RawData = rawData;
Device = device; Device = device;

View File

@ -1,10 +1,24 @@
using System; using System;
using System.Threading.Tasks;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
using TelegramBotBase.Form; using TelegramBotBase.Form;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using Telegram.Bot;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Interfaces; namespace TelegramBotBase.Interfaces;
internal interface IDeviceSession public interface IDeviceSession : IDeviceSessionMethods
{ {
MessageClient Client => ActiveForm.Client;
int LastMessageId => LastMessage?.MessageId ?? -1;
Message LastMessage { get; set; }
/// <summary> /// <summary>
/// Device or chat id /// Device or chat id
/// </summary> /// </summary>
@ -35,4 +49,6 @@ internal interface IDeviceSession
/// contains if the form has been switched (navigated) /// contains if the form has been switched (navigated)
/// </summary> /// </summary>
bool FormSwitched { get; set; } bool FormSwitched { get; set; }
} }

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using Telegram.Bot.Types;
using TelegramBotBase.Form;
using Telegram.Bot;
using TelegramBotBase.Base;
using TelegramBotBase.Args;
namespace TelegramBotBase.Interfaces
{
public interface IDeviceSessionMethods
{
string GetChatTitle();
Task BanUser(long userId, DateTime until = default);
Task UnbanUser(long userId);
Task ChangeChatPermissions(ChatPermissions permissions);
Task RestrictUser(long userId, ChatPermissions permissions, bool? useIndependentGroupPermission = null, DateTime until = default);
Task ConfirmAction(string callbackQueryId, string message = "", bool showAlert = false,
string urlToOpen = null);
Task<bool> DeleteMessage(int messageId = -1);
Task<bool> DeleteMessage(Message message);
Task<Message> HideReplyKeyboard(string closedMsg = "Closed", bool autoDeleteResponse = true);
Task<Message> Send(string text, ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
bool markdownV2AutoEscape = true);
Task<Message> Send(string text, IReplyMarkup markup, int replyTo = 0,
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
bool markdownV2AutoEscape = true);
Task<Message> Send(string text, InlineKeyboardMarkup markup, int replyTo = 0,
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
bool markdownV2AutoEscape = true);
Task SetAction(ChatAction action);
Task<Message> SendTextFile(string filename, string textcontent, Encoding encoding = null,
string caption = "", ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false);
Task<Message> SendDocument(InputFile document, string caption = "",
ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false);
Task<Message> SendPhoto(InputFile file, string caption = null, ButtonForm buttons = null,
int replyTo = 0, bool disableNotification = false,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> SendVideo(InputFile file, string caption = null, ButtonForm buttons = null,
int replyTo = 0, bool disableNotification = false,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> SendVideo(string url, ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown);
Task<Message> SendVideo(string filename, byte[] video, ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown);
Task<Message> SendLocalVideo(string filepath, ButtonForm buttons = null, int replyTo = 0,
bool disableNotification = false,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> Edit(int messageId, string text, ButtonForm buttons = null,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> Edit(int messageId, string text, InlineKeyboardMarkup markup,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> Edit(Message message, ButtonForm buttons = null,
ParseMode parseMode = ParseMode.Markdown);
Task<Message> EditReplyMarkup(int messageId, ButtonForm bf);
Task<Message> RequestContact(string buttonText = "Send your contact",
string requestMessage = "Give me your phone number!",
bool oneTimeOnly = true);
Task<Message> RequestLocation(string buttonText = "Send your location",
string requestMessage = "Give me your location!",
bool oneTimeOnly = true);
Task<ChatMember> GetChatUser(long userId);
event Async.AsyncEventHandler<MessageSentEventArgs> MessageSent;
event EventHandler<MessageReceivedEventArgs> MessageReceived;
event EventHandler<MessageDeletedEventArgs> MessageDeleted;
Task Api(Func<ITelegramBotClient, Task> call);
Task<T> Api<T>(Func<ITelegramBotClient, Task<T>> call);
T Raw<T>(Func<ITelegramBotClient, T> call);
}
}

View File

@ -2,13 +2,12 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Interfaces; namespace TelegramBotBase.Interfaces;
public interface IMessageLoopFactory public interface IMessageLoopFactory
{ {
Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult e); Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult e);
event EventHandler<UnhandledCallEventArgs> UnhandledCall; event EventHandler<UnhandledCallEventArgs> UnhandledCall;
} }

View File

@ -5,7 +5,6 @@ using Telegram.Bot.Types.Enums;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Interfaces; using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.MessageLoops; namespace TelegramBotBase.MessageLoops;
@ -18,7 +17,7 @@ public class FormBaseMessageLoop : IMessageLoopFactory
private readonly EventHandlerList _events = new(); private readonly EventHandlerList _events = new();
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr) public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
{ {
var update = ur.RawData; var update = ur.RawData;

View File

@ -5,7 +5,6 @@ using Telegram.Bot.Types.Enums;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Interfaces; using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.MessageLoops; namespace TelegramBotBase.MessageLoops;
@ -18,7 +17,7 @@ public class FullMessageLoop : IMessageLoopFactory
private readonly EventHandlerList _events = new(); private readonly EventHandlerList _events = new();
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr) public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
{ {
var update = ur.RawData; var update = ur.RawData;

View File

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Base; using TelegramBotBase.Base;
using TelegramBotBase.Interfaces; using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.MessageLoops; namespace TelegramBotBase.MessageLoops;
@ -17,7 +16,7 @@ public class MinimalMessageLoop : IMessageLoopFactory
private readonly EventHandlerList _events = new(); private readonly EventHandlerList _events = new();
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr) public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
{ {
var update = ur.RawData; var update = ur.RawData;

View File

@ -21,7 +21,7 @@ public class SessionManager
public SessionManager(BotBase botBase) public SessionManager(BotBase botBase)
{ {
BotBase = botBase; BotBase = botBase;
SessionList = new Dictionary<long, DeviceSession>(); SessionList = new Dictionary<long, IDeviceSession>();
} }
/// <summary> /// <summary>
@ -32,7 +32,7 @@ public class SessionManager
/// <summary> /// <summary>
/// A list of all active sessions. /// A list of all active sessions.
/// </summary> /// </summary>
public Dictionary<long, DeviceSession> SessionList { get; set; } public Dictionary<long, IDeviceSession> SessionList { get; set; }
/// <summary> /// <summary>
@ -45,7 +45,7 @@ public class SessionManager
/// </summary> /// </summary>
/// <param name="deviceId"></param> /// <param name="deviceId"></param>
/// <returns></returns> /// <returns></returns>
public DeviceSession GetSession(long deviceId) public IDeviceSession GetSession(long deviceId)
{ {
var ds = SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null; var ds = SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null;
return ds; return ds;
@ -57,7 +57,7 @@ public class SessionManager
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="deviceId"></param> /// <param name="deviceId"></param>
/// <returns></returns> /// <returns></returns>
public async Task<DeviceSession> StartSession(long deviceId) public async Task<IDeviceSession> StartSession(long deviceId)
{ {
var start = BotBase.StartFormFactory.CreateForm(); var start = BotBase.StartFormFactory.CreateForm();
@ -91,7 +91,7 @@ public class SessionManager
/// Returns all active User Sessions. /// Returns all active User Sessions.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public List<DeviceSession> GetUserSessions() public List<IDeviceSession> GetUserSessions()
{ {
return SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList(); return SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList();
} }
@ -100,7 +100,7 @@ public class SessionManager
/// Returns all active Group Sessions. /// Returns all active Group Sessions.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public List<DeviceSession> GetGroupSessions() public List<IDeviceSession> GetGroupSessions()
{ {
return SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList(); return SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList();
} }