- adding Delete Options for Both sides to AutoCleanForm
- general Performance improvements - Adding MessageReceived Event to DeviceSession, will be called before "message loop" - AutoCleanForm optimized and clean process added to public function MessageCleanup (so you could call on your own) - IsGroup for DeviceSession fixed to correct test - IsChannel added to DeviceSession Examples: - SimpleForm (#1) derived now from AutoCleanForm to show deletion mode for both sides
This commit is contained in:
parent
3a78a98b9e
commit
4f1eae543d
@ -8,12 +8,18 @@ using TelegramBotBase.Form;
|
|||||||
|
|
||||||
namespace TelegramBaseTest.Tests
|
namespace TelegramBaseTest.Tests
|
||||||
{
|
{
|
||||||
public class SimpleForm : FormBase
|
public class SimpleForm : AutoCleanForm
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public SimpleForm()
|
||||||
|
{
|
||||||
|
this.DeleteSide = eSide.Both;
|
||||||
|
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
||||||
|
}
|
||||||
|
|
||||||
public override async Task Opened()
|
public override async Task Opened()
|
||||||
{
|
{
|
||||||
await this.Device.Send("Hello world! (send 'back' to get back to Start)");
|
await this.Device.Send("Hello world! (send 'back' to get back to Start)\r\nOr\r\nhi, hello, maybe, bye and ciao");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,10 +15,6 @@ namespace TelegramBaseTest.Tests
|
|||||||
|
|
||||||
String LastMessage { get; set; }
|
String LastMessage { get; set; }
|
||||||
|
|
||||||
public override async Task Init(params object[] param)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task Opened()
|
public override async Task Opened()
|
||||||
{
|
{
|
||||||
@ -30,12 +26,6 @@ namespace TelegramBaseTest.Tests
|
|||||||
await this.Device.Send("Ciao from Form 1");
|
await this.Device.Send("Ciao from Form 1");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task Load(MessageResult message)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public override async Task Action(MessageResult message)
|
public override async Task Action(MessageResult message)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -15,10 +15,6 @@ namespace TelegramBaseTest.Tests
|
|||||||
public class TestForm2 : FormBase
|
public class TestForm2 : FormBase
|
||||||
{
|
{
|
||||||
|
|
||||||
public override async Task Init(params object[] param)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task Opened()
|
public override async Task Opened()
|
||||||
{
|
{
|
||||||
@ -30,10 +26,6 @@ namespace TelegramBaseTest.Tests
|
|||||||
await this.Device.Send("Ciao from Form 2");
|
await this.Device.Send("Ciao from Form 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task Load(MessageResult message)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task Action(MessageResult message)
|
public override async Task Action(MessageResult message)
|
||||||
{
|
{
|
||||||
|
|||||||
28
TelegramBotBase/Base/MessageReceivedEventArgs.cs
Normal file
28
TelegramBotBase/Base/MessageReceivedEventArgs.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Base
|
||||||
|
{
|
||||||
|
public class MessageReceivedEventArgs
|
||||||
|
{
|
||||||
|
public int MessageId
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return this.Message.MessageId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message Message { get; set; }
|
||||||
|
|
||||||
|
public MessageReceivedEventArgs(Message m)
|
||||||
|
{
|
||||||
|
this.Message = m;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base
|
namespace TelegramBotBase.Base
|
||||||
{
|
{
|
||||||
@ -23,6 +24,9 @@ namespace TelegramBotBase.Base
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DeviceSession Device
|
||||||
|
{ get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The message id
|
/// The message id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -9,18 +9,18 @@ namespace TelegramBotBase.Base
|
|||||||
{
|
{
|
||||||
public class MessageSentEventArgs
|
public class MessageSentEventArgs
|
||||||
{
|
{
|
||||||
public int MessageId { get; set; }
|
public int MessageId
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return this.Message.MessageId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Message Message { get; set; }
|
public Message Message { get; set; }
|
||||||
|
|
||||||
public MessageSentEventArgs()
|
public MessageSentEventArgs(Message message)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessageSentEventArgs(int MessageId, Message message)
|
|
||||||
{
|
|
||||||
this.MessageId = MessageId;
|
|
||||||
this.Message = message;
|
this.Message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -201,12 +201,15 @@ namespace TelegramBotBase
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||||
|
e.Device = ds;
|
||||||
|
|
||||||
if (LogAllMessages)
|
if (LogAllMessages)
|
||||||
{
|
{
|
||||||
DeviceSession ds2 = this.Sessions.GetSession(e.DeviceId);
|
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
|
||||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds2, e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
|
||||||
|
|
||||||
Client_TryMessage(sender, e);
|
Client_TryMessage(sender, e);
|
||||||
}
|
}
|
||||||
@ -223,10 +226,11 @@ namespace TelegramBotBase
|
|||||||
|
|
||||||
private async void Client_TryMessage(object sender, MessageResult e)
|
private async void Client_TryMessage(object sender, MessageResult e)
|
||||||
{
|
{
|
||||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
DeviceSession ds = e.Device;
|
||||||
if (ds == null)
|
if (ds == null)
|
||||||
{
|
{
|
||||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||||
|
e.Device = ds;
|
||||||
|
|
||||||
ds.LastMessage = e.Message;
|
ds.LastMessage = e.Message;
|
||||||
|
|
||||||
@ -286,10 +290,12 @@ namespace TelegramBotBase
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||||
|
e.Device = ds;
|
||||||
|
|
||||||
if (LogAllMessages)
|
if (LogAllMessages)
|
||||||
{
|
{
|
||||||
DeviceSession ds2 = this.Sessions.GetSession(e.DeviceId);
|
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
|
||||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds2, e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Client_TryAction(sender, e);
|
Client_TryAction(sender, e);
|
||||||
@ -303,10 +309,11 @@ namespace TelegramBotBase
|
|||||||
|
|
||||||
private async void Client_TryAction(object sender, MessageResult e)
|
private async void Client_TryAction(object sender, MessageResult e)
|
||||||
{
|
{
|
||||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
DeviceSession ds = e.Device;
|
||||||
if (ds == null)
|
if (ds == null)
|
||||||
{
|
{
|
||||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||||
|
e.Device = ds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,8 @@ namespace TelegramBotBase.Form
|
|||||||
|
|
||||||
public eDeleteMode DeleteMode { get; set; }
|
public eDeleteMode DeleteMode { get; set; }
|
||||||
|
|
||||||
|
public eSide DeleteSide { get; set; }
|
||||||
|
|
||||||
public enum eDeleteMode
|
public enum eDeleteMode
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
@ -24,10 +26,18 @@ namespace TelegramBotBase.Form
|
|||||||
OnLeavingForm = 2
|
OnLeavingForm = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum eSide
|
||||||
|
{
|
||||||
|
BotOnly = 0,
|
||||||
|
UserOnly = 1,
|
||||||
|
Both = 2
|
||||||
|
}
|
||||||
|
|
||||||
public AutoCleanForm()
|
public AutoCleanForm()
|
||||||
{
|
{
|
||||||
this.OldMessages = new List<Message>();
|
this.OldMessages = new List<Message>();
|
||||||
this.DeleteMode = eDeleteMode.OnEveryCall;
|
this.DeleteMode = eDeleteMode.OnEveryCall;
|
||||||
|
this.DeleteSide = eSide.BotOnly;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,10 +47,24 @@ namespace TelegramBotBase.Form
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
this.Device.MessageSent += Device_MessageSent;
|
this.Device.MessageSent += Device_MessageSent;
|
||||||
|
|
||||||
|
this.Device.MessageReceived += Device_MessageReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Device_MessageReceived(object sender, MessageReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
if (this.DeleteSide == eSide.BotOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.OldMessages.Add(e.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Device_MessageSent(object sender, MessageSentEventArgs e)
|
private void Device_MessageSent(object sender, MessageSentEventArgs e)
|
||||||
{
|
{
|
||||||
|
if (this.DeleteSide == eSide.UserOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
this.OldMessages.Add(e.Message);
|
this.OldMessages.Add(e.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,19 +73,11 @@ namespace TelegramBotBase.Form
|
|||||||
if (this.DeleteMode != eDeleteMode.OnEveryCall)
|
if (this.DeleteMode != eDeleteMode.OnEveryCall)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
while (this.OldMessages.Count > 0)
|
await MessageCleanup();
|
||||||
{
|
|
||||||
if (!await this.Device.DeleteMessage(this.OldMessages[0].MessageId))
|
|
||||||
{
|
|
||||||
//Nachricht konnte nicht gelöscht werden, vermutlich existiert diese nicht mehr
|
|
||||||
if (this.OldMessages.Count > 0)
|
|
||||||
this.OldMessages.RemoveAt(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fügt eine Nachricht zu der Liste der löschenden hinzu.
|
/// Adds a message to this of removable ones
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="Id"></param>
|
/// <param name="Id"></param>
|
||||||
public void AddMessage(Message m)
|
public void AddMessage(Message m)
|
||||||
@ -70,7 +86,7 @@ namespace TelegramBotBase.Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Behält die Nachricht mit der angegebenen Id.
|
/// Keeps the message by removing it from the list
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="Id"></param>
|
/// <param name="Id"></param>
|
||||||
public void LeaveMessage(int Id)
|
public void LeaveMessage(int Id)
|
||||||
@ -83,7 +99,7 @@ namespace TelegramBotBase.Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Behält die zuletzt gesendete Nachricht.
|
/// Keeps the last sent message
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void LeaveLastMessage()
|
public void LeaveLastMessage()
|
||||||
{
|
{
|
||||||
@ -98,9 +114,24 @@ namespace TelegramBotBase.Form
|
|||||||
if (this.DeleteMode != eDeleteMode.OnLeavingForm)
|
if (this.DeleteMode != eDeleteMode.OnLeavingForm)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
foreach (var m in this.OldMessages)
|
await MessageCleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cleans up all remembered messages.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task MessageCleanup()
|
||||||
{
|
{
|
||||||
await this.Device.DeleteMessage(m.MessageId);
|
while (this.OldMessages.Count > 0)
|
||||||
|
{
|
||||||
|
if (!await this.Device.DeleteMessage(this.OldMessages[0].MessageId))
|
||||||
|
{
|
||||||
|
//Message can't be deleted cause it seems not to exist anymore
|
||||||
|
if (this.OldMessages.Count > 0)
|
||||||
|
this.OldMessages.RemoveAt(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,13 +68,25 @@ namespace TelegramBotBase.Sessions
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return this.LastMessage != null && this.LastMessage.Chat.Id != this.LastMessage.From.Id;
|
return this.LastMessage != null && (this.LastMessage.Chat.Type == ChatType.Group | this.LastMessage.Chat.Type == ChatType.Supergroup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns if the messages is posted within a channel.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsChannel
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return this.LastMessage != null && this.LastMessage.Chat.Type == ChatType.Channel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventHandlerList __Events = new EventHandlerList();
|
public EventHandlerList __Events = new EventHandlerList();
|
||||||
|
|
||||||
private static object __evMessageSent = new object();
|
private static object __evMessageSent = new object();
|
||||||
|
private static object __evMessageReceived = new object();
|
||||||
|
|
||||||
public DeviceSession()
|
public DeviceSession()
|
||||||
{
|
{
|
||||||
@ -151,7 +163,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
{
|
{
|
||||||
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
OnMessageSent(new MessageSentEventArgs( m));
|
||||||
}
|
}
|
||||||
catch (ApiRequestException ex)
|
catch (ApiRequestException ex)
|
||||||
{
|
{
|
||||||
@ -184,7 +196,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
{
|
{
|
||||||
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
OnMessageSent(new MessageSentEventArgs(m));
|
||||||
}
|
}
|
||||||
catch (ApiRequestException ex)
|
catch (ApiRequestException ex)
|
||||||
{
|
{
|
||||||
@ -223,7 +235,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
{
|
{
|
||||||
m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
|
m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
|
||||||
|
|
||||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
OnMessageSent(new MessageSentEventArgs(m));
|
||||||
}
|
}
|
||||||
catch (ApiRequestException ex)
|
catch (ApiRequestException ex)
|
||||||
{
|
{
|
||||||
@ -319,7 +331,7 @@ namespace TelegramBotBase.Sessions
|
|||||||
|
|
||||||
var m = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo);
|
var m = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo);
|
||||||
|
|
||||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
OnMessageSent(new MessageSentEventArgs(m));
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
@ -414,5 +426,25 @@ namespace TelegramBotBase.Sessions
|
|||||||
(this.__Events[__evMessageSent] as EventHandler<MessageSentEventArgs>)?.Invoke(this, e);
|
(this.__Events[__evMessageSent] as EventHandler<MessageSentEventArgs>)?.Invoke(this, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Eventhandler for received messages
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<MessageReceivedEventArgs> MessageReceived
|
||||||
|
{
|
||||||
|
add
|
||||||
|
{
|
||||||
|
this.__Events.AddHandler(__evMessageReceived, value);
|
||||||
|
}
|
||||||
|
remove
|
||||||
|
{
|
||||||
|
this.__Events.RemoveHandler(__evMessageReceived, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void OnMessageReceived(MessageReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
(this.__Events[__evMessageReceived] as EventHandler<MessageReceivedEventArgs>)?.Invoke(this, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user