Compare commits

..
10 Commits
Author SHA1 Message Date
FlorianDahn 52c01b3dfd Update 2019-06-22 15:16:51 +02:00
FlorianDahn fe1aafc9a5 - added IsDisposed to form
- removed FormSwitched from FormBase and added to DeviceSession (more logical message loop)
- added PreviousForm to DeviceSession, contains last opened form (at initial form is null)
-BotBase message loop update
- now navigation is possible from every form, not just the latest ones as before
2019-06-22 15:15:29 +02:00
FlorianDahn 022eee4b57 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-05-04 18:59:08 +02:00
FlorianDahn ea971979e2 - Added comments
- added Edit method
2019-05-04 18:59:05 +02:00
Florian Dahn 9585a231bd Update README.md 2019-05-04 18:43:54 +02:00
Florian Dahn c07aca96a4 Update README.md 2019-05-04 16:56:44 +02:00
Florian Dahn 01de9ec932 Update README.md 2019-05-04 16:56:25 +02:00
FlorianDahn 94fe263676 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-05-04 16:32:50 +02:00
FlorianDahn 4f1eae543d - 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
2019-05-04 16:32:48 +02:00
Florian Dahn 1491fc1795 Update README.md 2019-04-16 13:46:37 +02:00
12 changed files with 238 additions and 72 deletions
+12 -5
View File
@@ -2,13 +2,15 @@
# .Net Telegram Bot Framework - Context based addon # .Net Telegram Bot Framework - Context based addon
[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase/) [![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase/)
[![telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://t.me/tgbotbase) [![telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase)
[![license](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md) [![license](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
[![downloads](https://img.shields.io/nuget/dt/TelegramBotBase.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase) [![downloads](https://img.shields.io/nuget/dt/TelegramBotBase.svg?style=flat-square&label=Package%20Downloads)](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() 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 -2
View File
@@ -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");
} }
-10
View File
@@ -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)
{ {
-8
View File
@@ -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)
{ {
+6 -4
View File
@@ -20,9 +20,9 @@ namespace TelegramBotBase.Form
public bool CustomEventManagement { get; set; } = false; public bool CustomEventManagement { get; set; } = false;
/// <summary> /// <summary>
/// contains if the form has been switched (navigated) /// has this formular already been disposed ?
/// </summary> /// </summary>
public bool FormSwitched { get; set; } = false; public bool IsDisposed { get; set; } = false;
public List<ControlBase> Controls { get; set; } public List<ControlBase> Controls { get; set; }
@@ -117,12 +117,14 @@ namespace TelegramBotBase.Form
if (ds == null) if (ds == null)
return; return;
this.FormSwitched = true; ds.FormSwitched = true;
ds.ActiveForm = newForm; ds.ActiveForm = newForm;
newForm.Client = this.Client; newForm.Client = this.Client;
newForm.Device = ds; newForm.Device = ds;
ds.PreviousForm = this;
await newForm.Init(args); await newForm.Init(args);
await this.Closed(); await this.Closed();
@@ -137,7 +139,7 @@ namespace TelegramBotBase.Form
{ {
this.Client = null; this.Client = null;
this.Device = 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;
}
}
}
+4
View File
@@ -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>
+8 -8
View File
@@ -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;
} }
+25 -12
View File
@@ -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;
@@ -254,6 +258,9 @@ namespace TelegramBotBase
do do
{ {
i++; i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm; activeForm = ds.ActiveForm;
@@ -275,10 +282,10 @@ namespace TelegramBotBase
} }
//Render Event //Render Event
if (!activeForm.FormSwitched) if (!ds.FormSwitched)
await activeForm.Render(e); await activeForm.Render(e);
} while (activeForm.FormSwitched && i < NavigationMaximum); } while (ds.FormSwitched && i < NavigationMaximum);
} }
@@ -286,10 +293,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 +312,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;
} }
@@ -322,6 +332,9 @@ namespace TelegramBotBase
{ {
i++; i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm; activeForm = ds.ActiveForm;
//If the form manages the events by itselfs, skip here //If the form manages the events by itselfs, skip here
@@ -335,7 +348,7 @@ namespace TelegramBotBase
await activeForm.Load(e); await activeForm.Load(e);
//Action Event //Action Event
if (!activeForm.FormSwitched) if (!ds.FormSwitched)
{ {
await activeForm.Action(e); await activeForm.Action(e);
@@ -346,7 +359,7 @@ namespace TelegramBotBase
if (uhc.Handled) if (uhc.Handled)
{ {
if (activeForm.FormSwitched) if (ds.FormSwitched)
{ {
continue; continue;
} }
@@ -360,10 +373,10 @@ namespace TelegramBotBase
} }
//Render Event //Render Event
if (!activeForm.FormSwitched) if (!ds.FormSwitched)
await activeForm.Render(e); await activeForm.Render(e);
} while (activeForm.FormSwitched && i < NavigationMaximum); } while (ds.FormSwitched && i < NavigationMaximum);
} }
+63 -14
View File
@@ -17,17 +17,45 @@ namespace TelegramBotBase.Form
public eDeleteMode DeleteMode { get; set; } public eDeleteMode DeleteMode { get; set; }
public eSide DeleteSide { get; set; }
public enum eDeleteMode public enum eDeleteMode
{ {
/// <summary>
/// Don't delete any message.
/// </summary>
None = 0, None = 0,
/// <summary>
/// Delete messages on every callback/action.
/// </summary>
OnEveryCall = 1, OnEveryCall = 1,
/// <summary>
/// Delete on leaving this form.
/// </summary>
OnLeavingForm = 2 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() 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 +65,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 +91,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 +104,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 +117,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 +132,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()
{
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);
}
} }
} }
} }
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben: // übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.0.0")] [assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.4.0.0")] [assembly: AssemblyFileVersion("1.4.2.0")]
+82 -7
View File
@@ -37,6 +37,16 @@ namespace TelegramBotBase.Sessions
/// </summary> /// </summary>
public FormBase ActiveForm { get; set; } 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> /// <summary>
/// Returns the ID of the last received message. /// Returns the ID of the last received message.
/// </summary> /// </summary>
@@ -68,13 +78,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);
} }
} }
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 __evMessageSent = new object();
private static object __evMessageReceived = new object();
public DeviceSession() public DeviceSession()
{ {
@@ -126,6 +148,39 @@ namespace TelegramBotBase.Sessions
return null; 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> /// <summary>
/// Sends a simple text message /// Sends a simple text message
/// </summary> /// </summary>
@@ -151,7 +206,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 +239,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 +278,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)
{ {
@@ -318,8 +373,8 @@ 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 +469,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);
}
} }
} }