Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
52c01b3dfd | ||
|
|
fe1aafc9a5 | ||
|
|
022eee4b57 | ||
|
|
ea971979e2 | ||
|
|
9585a231bd | ||
|
|
c07aca96a4 | ||
|
|
01de9ec932 | ||
|
|
94fe263676 | ||
|
|
4f1eae543d | ||
|
|
1491fc1795 |
@@ -2,13 +2,15 @@
|
||||
# .Net Telegram Bot Framework - Context based addon
|
||||
|
||||
[](https://www.nuget.org/packages/TelegramBotBase/)
|
||||
[](https://t.me/tgbotbase)
|
||||
[](https://www.t.me/tgbotbase)
|
||||
|
||||
|
||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
||||
[](https://www.nuget.org/packages/TelegramBotBase)
|
||||
|
||||
Test the Testproject: [@TGBaseBot](https://t.me/TGBaseBot)
|
||||
Test the Testproject: [@TGBaseBot](https://www.t.me/TGBaseBot)
|
||||
|
||||
Join the Telegram Group: [https://www.t.me/tgbotbase](https://www.t.me/tgbotbase)
|
||||
|
||||
---
|
||||
|
||||
@@ -220,12 +222,17 @@ On every input the user is sending back to the bot the Action event gets raised.
|
||||
|
||||
|
||||
```
|
||||
public class SimpleForm : FormBase
|
||||
public class SimpleForm : AutoCleanForm
|
||||
{
|
||||
|
||||
public SimpleForm()
|
||||
{
|
||||
this.DeleteSide = eSide.Both;
|
||||
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
||||
}
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
await this.Device.Send("Hello world!");
|
||||
await this.Device.Send("Hello world! (send 'back' to get back to Start)\r\nOr\r\nhi, hello, maybe, bye and ciao");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,12 +8,18 @@ using TelegramBotBase.Form;
|
||||
|
||||
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()
|
||||
{
|
||||
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; }
|
||||
|
||||
public override async Task Init(params object[] param)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
@@ -30,12 +26,6 @@ namespace TelegramBaseTest.Tests
|
||||
await this.Device.Send("Ciao from Form 1");
|
||||
}
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
|
||||
@@ -15,10 +15,6 @@ namespace TelegramBaseTest.Tests
|
||||
public class TestForm2 : FormBase
|
||||
{
|
||||
|
||||
public override async Task Init(params object[] param)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
@@ -30,10 +26,6 @@ namespace TelegramBaseTest.Tests
|
||||
await this.Device.Send("Ciao from Form 2");
|
||||
}
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
|
||||
@@ -20,9 +20,9 @@ namespace TelegramBotBase.Form
|
||||
public bool CustomEventManagement { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// contains if the form has been switched (navigated)
|
||||
/// has this formular already been disposed ?
|
||||
/// </summary>
|
||||
public bool FormSwitched { get; set; } = false;
|
||||
public bool IsDisposed { get; set; } = false;
|
||||
|
||||
public List<ControlBase> Controls { get; set; }
|
||||
|
||||
@@ -117,12 +117,14 @@ namespace TelegramBotBase.Form
|
||||
if (ds == null)
|
||||
return;
|
||||
|
||||
this.FormSwitched = true;
|
||||
ds.FormSwitched = true;
|
||||
|
||||
ds.ActiveForm = newForm;
|
||||
newForm.Client = this.Client;
|
||||
newForm.Device = ds;
|
||||
|
||||
ds.PreviousForm = this;
|
||||
|
||||
await newForm.Init(args);
|
||||
|
||||
await this.Closed();
|
||||
@@ -137,7 +139,7 @@ namespace TelegramBotBase.Form
|
||||
{
|
||||
this.Client = null;
|
||||
this.Device = null;
|
||||
this.FormSwitched = false;
|
||||
this.IsDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
@@ -23,6 +24,9 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
}
|
||||
|
||||
public DeviceSession Device
|
||||
{ get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The message id
|
||||
/// </summary>
|
||||
|
||||
@@ -9,18 +9,18 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
public class MessageSentEventArgs
|
||||
{
|
||||
public int MessageId { get; set; }
|
||||
public int MessageId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Message.MessageId;
|
||||
}
|
||||
}
|
||||
|
||||
public Message Message { get; set; }
|
||||
|
||||
public MessageSentEventArgs()
|
||||
public MessageSentEventArgs(Message message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MessageSentEventArgs(int MessageId, Message message)
|
||||
{
|
||||
this.MessageId = MessageId;
|
||||
this.Message = message;
|
||||
}
|
||||
|
||||
|
||||
+25
-12
@@ -201,12 +201,15 @@ namespace TelegramBotBase
|
||||
|
||||
try
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
if (LogAllMessages)
|
||||
{
|
||||
DeviceSession ds2 = this.Sessions.GetSession(e.DeviceId);
|
||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds2, e));
|
||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
|
||||
}
|
||||
|
||||
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
|
||||
|
||||
Client_TryMessage(sender, e);
|
||||
}
|
||||
@@ -223,10 +226,11 @@ namespace TelegramBotBase
|
||||
|
||||
private async void Client_TryMessage(object sender, MessageResult e)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
DeviceSession ds = e.Device;
|
||||
if (ds == null)
|
||||
{
|
||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
ds.LastMessage = e.Message;
|
||||
|
||||
@@ -254,6 +258,9 @@ namespace TelegramBotBase
|
||||
do
|
||||
{
|
||||
i++;
|
||||
|
||||
//Reset navigation
|
||||
ds.FormSwitched = false;
|
||||
|
||||
activeForm = ds.ActiveForm;
|
||||
|
||||
@@ -275,10 +282,10 @@ namespace TelegramBotBase
|
||||
}
|
||||
|
||||
//Render Event
|
||||
if (!activeForm.FormSwitched)
|
||||
if (!ds.FormSwitched)
|
||||
await activeForm.Render(e);
|
||||
|
||||
} while (activeForm.FormSwitched && i < NavigationMaximum);
|
||||
} while (ds.FormSwitched && i < NavigationMaximum);
|
||||
|
||||
}
|
||||
|
||||
@@ -286,10 +293,12 @@ namespace TelegramBotBase
|
||||
{
|
||||
try
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
if (LogAllMessages)
|
||||
{
|
||||
DeviceSession ds2 = this.Sessions.GetSession(e.DeviceId);
|
||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds2, e));
|
||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
|
||||
}
|
||||
|
||||
Client_TryAction(sender, e);
|
||||
@@ -303,10 +312,11 @@ namespace TelegramBotBase
|
||||
|
||||
private async void Client_TryAction(object sender, MessageResult e)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
DeviceSession ds = e.Device;
|
||||
if (ds == null)
|
||||
{
|
||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||
e.Device = ds;
|
||||
}
|
||||
|
||||
|
||||
@@ -322,6 +332,9 @@ namespace TelegramBotBase
|
||||
{
|
||||
i++;
|
||||
|
||||
//Reset navigation
|
||||
ds.FormSwitched = false;
|
||||
|
||||
activeForm = ds.ActiveForm;
|
||||
|
||||
//If the form manages the events by itselfs, skip here
|
||||
@@ -335,7 +348,7 @@ namespace TelegramBotBase
|
||||
await activeForm.Load(e);
|
||||
|
||||
//Action Event
|
||||
if (!activeForm.FormSwitched)
|
||||
if (!ds.FormSwitched)
|
||||
{
|
||||
await activeForm.Action(e);
|
||||
|
||||
@@ -346,7 +359,7 @@ namespace TelegramBotBase
|
||||
|
||||
if (uhc.Handled)
|
||||
{
|
||||
if (activeForm.FormSwitched)
|
||||
if (ds.FormSwitched)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -360,10 +373,10 @@ namespace TelegramBotBase
|
||||
}
|
||||
|
||||
//Render Event
|
||||
if (!activeForm.FormSwitched)
|
||||
if (!ds.FormSwitched)
|
||||
await activeForm.Render(e);
|
||||
|
||||
} while (activeForm.FormSwitched && i < NavigationMaximum);
|
||||
} while (ds.FormSwitched && i < NavigationMaximum);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,17 +17,45 @@ namespace TelegramBotBase.Form
|
||||
|
||||
public eDeleteMode DeleteMode { get; set; }
|
||||
|
||||
public eSide DeleteSide { get; set; }
|
||||
|
||||
public enum eDeleteMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Don't delete any message.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// Delete messages on every callback/action.
|
||||
/// </summary>
|
||||
OnEveryCall = 1,
|
||||
/// <summary>
|
||||
/// Delete on leaving this form.
|
||||
/// </summary>
|
||||
OnLeavingForm = 2
|
||||
}
|
||||
|
||||
public enum eSide
|
||||
{
|
||||
/// <summary>
|
||||
/// Delete only messages from this bot.
|
||||
/// </summary>
|
||||
BotOnly = 0,
|
||||
/// <summary>
|
||||
/// Delete only user messages.
|
||||
/// </summary>
|
||||
UserOnly = 1,
|
||||
/// <summary>
|
||||
/// Delete all messages in this context.
|
||||
/// </summary>
|
||||
Both = 2
|
||||
}
|
||||
|
||||
public AutoCleanForm()
|
||||
{
|
||||
this.OldMessages = new List<Message>();
|
||||
this.DeleteMode = eDeleteMode.OnEveryCall;
|
||||
this.DeleteSide = eSide.BotOnly;
|
||||
|
||||
}
|
||||
|
||||
@@ -37,10 +65,24 @@ namespace TelegramBotBase.Form
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
if (this.DeleteSide == eSide.UserOnly)
|
||||
return;
|
||||
|
||||
this.OldMessages.Add(e.Message);
|
||||
}
|
||||
|
||||
@@ -49,19 +91,11 @@ namespace TelegramBotBase.Form
|
||||
if (this.DeleteMode != eDeleteMode.OnEveryCall)
|
||||
return;
|
||||
|
||||
while (this.OldMessages.Count > 0)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
await MessageCleanup();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fügt eine Nachricht zu der Liste der löschenden hinzu.
|
||||
/// Adds a message to this of removable ones
|
||||
/// </summary>
|
||||
/// <param name="Id"></param>
|
||||
public void AddMessage(Message m)
|
||||
@@ -70,7 +104,7 @@ namespace TelegramBotBase.Form
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Behält die Nachricht mit der angegebenen Id.
|
||||
/// Keeps the message by removing it from the list
|
||||
/// </summary>
|
||||
/// <param name="Id"></param>
|
||||
public void LeaveMessage(int Id)
|
||||
@@ -83,7 +117,7 @@ namespace TelegramBotBase.Form
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Behält die zuletzt gesendete Nachricht.
|
||||
/// Keeps the last sent message
|
||||
/// </summary>
|
||||
public void LeaveLastMessage()
|
||||
{
|
||||
@@ -98,9 +132,24 @@ namespace TelegramBotBase.Form
|
||||
if (this.DeleteMode != eDeleteMode.OnLeavingForm)
|
||||
return;
|
||||
|
||||
foreach (var m in this.OldMessages)
|
||||
await MessageCleanup();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up all remembered messages.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task MessageCleanup()
|
||||
{
|
||||
while (this.OldMessages.Count > 0)
|
||||
{
|
||||
await this.Device.DeleteMessage(m.MessageId);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
||||
// übernehmen, indem Sie "*" eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.4.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.4.0.0")]
|
||||
[assembly: AssemblyVersion("1.4.2.0")]
|
||||
[assembly: AssemblyFileVersion("1.4.2.0")]
|
||||
|
||||
@@ -37,6 +37,16 @@ namespace TelegramBotBase.Sessions
|
||||
/// </summary>
|
||||
public FormBase ActiveForm { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the previous shown form
|
||||
/// </summary>
|
||||
public FormBase PreviousForm { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// contains if the form has been switched (navigated)
|
||||
/// </summary>
|
||||
public bool FormSwitched { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the ID of the last received message.
|
||||
/// </summary>
|
||||
@@ -68,13 +78,25 @@ namespace TelegramBotBase.Sessions
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public EventHandlerList __Events = new EventHandlerList();
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
|
||||
private EventHandlerList __Events = new EventHandlerList();
|
||||
|
||||
private static object __evMessageSent = new object();
|
||||
private static object __evMessageReceived = new object();
|
||||
|
||||
public DeviceSession()
|
||||
{
|
||||
@@ -126,6 +148,39 @@ namespace TelegramBotBase.Sessions
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Edits the text message
|
||||
/// </summary>
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="buttons"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Message> Edit(Message message, ButtonForm buttons = null)
|
||||
{
|
||||
if (this.ActiveForm == null)
|
||||
return null;
|
||||
|
||||
InlineKeyboardMarkup markup = null;
|
||||
if (buttons != null)
|
||||
{
|
||||
markup = buttons;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, message.MessageId, message.Text, replyMarkup: markup);
|
||||
|
||||
return m;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a simple text message
|
||||
/// </summary>
|
||||
@@ -151,7 +206,7 @@ namespace TelegramBotBase.Sessions
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -184,7 +239,7 @@ namespace TelegramBotBase.Sessions
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -223,7 +278,7 @@ namespace TelegramBotBase.Sessions
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -318,8 +373,8 @@ namespace TelegramBotBase.Sessions
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -414,5 +469,25 @@ namespace TelegramBotBase.Sessions
|
||||
(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user