Compare commits

...
32 Commits
Author SHA1 Message Date
FlorianDahn 94d153e141 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-06-26 12:48:37 +02:00
FlorianDahn e13cb6d4ef - Updating NavigateTo to set PreviousForm now correctly 2019-06-26 12:48:25 +02:00
Florian Dahn c0e285d39e Update README.md 2019-06-24 13:50:55 +02:00
Florian Dahn 8338c5c066 Update README.md 2019-06-24 13:49:37 +02:00
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
Florian Dahn b2db44fe4c Update README.md 2019-03-27 14:10:43 +07:00
FlorianDahn 3a78a98b9e Update Readme.md 2019-03-27 14:09:53 +07:00
FlorianDahn b14fa2dc87 - adding new SentData Event in FormBase for User uploads (for Photo, Audio, Video, Contact, Location, Document)
- adding RequestUserLocation and RequestUserContact to Device class
- Update to Device class
- updating examples
- adding example for data upload
- small documentary Updates (Readme.md)
2019-03-27 14:01:48 +07:00
FlorianDahn 5cd54fc746 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 22:24:42 +07:00
FlorianDahn fb68da6ab1 - added request contact to DeviceSession
- added request location to DeviceSession
2019-03-20 22:24:00 +07:00
FlorianDahn 2f7ded8ce5 - added constructor for adding custom config of TelegramBotClient
- added Log all messages to Action events
2019-03-20 22:21:10 +07:00
Florian Dahn 906177a5f9 Update README.md 2019-03-20 17:54:35 +07:00
FlorianDahn 873c91a165 Update Readme 2019-03-20 17:53:06 +07:00
FlorianDahn 46c1acbf99 Update Readme.md 2019-03-20 17:51:40 +07:00
FlorianDahn acd06a6948 Update Testproject/Readme 2019-03-20 17:50:11 +07:00
Florian Dahn a97305fa35 Update README.md 2019-03-20 17:48:18 +07:00
FlorianDahn 16baef0c45 Update Readme 2019-03-20 17:43:50 +07:00
FlorianDahn 211bc55aff - Cleanup of unecessary init calls
- added form1/form2 option to start menu in test project
- added example for different forms with and without event handlers
- added form examples to Readme.md
2019-03-20 17:42:33 +07:00
FlorianDahn 19f3c9c195 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 16:57:15 +07:00
FlorianDahn c6666f72bc - Improvements to system calls
- system calls now async with await
- added start command to example
- added "handled" property to system call event args
2019-03-20 16:57:10 +07:00
Florian Dahn bf7f6f851a Update README.md 2019-03-20 14:00:11 +07:00
FlorianDahn 816bf4cbbe Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 13:44:48 +07:00
FlorianDahn da80af86e3 Small fix in Systemcall management 2019-03-20 13:44:44 +07:00
22 changed files with 1030 additions and 353 deletions
+352 -246
View File
@@ -2,25 +2,22 @@
# .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/)
[![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)
[![downloads](https://img.shields.io/nuget/dt/TelegramBotBase.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase)
Hey guys,
Test the Testproject: [@TGBaseBot](https://www.t.me/TGBaseBot)
here we are. After some time and thoughts i give my TelegramBot framework to public.
It is based on C#.
Join the Telegram Group: [https://www.t.me/tgbotbase](https://www.t.me/tgbotbase)
It is a module which is based on the original [TelegramBotLibrary](https://github.com/TelegramBots/Telegram.Bot) you will find in nuget.
It gives you features which will look/feel like WinForms or have a good way to create apps with actions and forms.
Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramework/releases)
---
## Index
- [Introduction](#introduction)
- [How to Start](#how-to-start)
- [Message Handling](#message-handling)
* [Example #0 - System Calls](#add-some-system-calls-example-0---system-calls)
@@ -33,6 +30,27 @@ It gives you features which will look/feel like WinForms or have a good way to c
* [Example #4 - Registration Formular](#registration-example-example-4---registration-form-test)
- [Special Forms](#forms)
* [AlertDialog](#alert-dialog)
* [AutoCleanForm](#autocleanform)
* [PromptDialog](#prompt-dialog)
---
## Introduction
Hey guys,
here we are. After some time and thoughts i give my TelegramBot framework to public.
It is based on C#.
It is a module which is based on the original [TelegramBotLibrary](https://github.com/TelegramBots/Telegram.Bot) you will find in nuget.
It gives you features which will look/feel like WinForms or have a good way to create apps with actions and forms.
---
@@ -41,7 +59,7 @@ It gives you features which will look/feel like WinForms or have a good way to c
Within your empty App your need to put some initial lines including your APIKey to get things started.
The "BotBase" Class will manage a lot of things for you, like system calls, action events and so on.
"StartForm" is your first Formular which every user will get internally redirected to, like a start page, you could redirect from there later in code, so users won't recognize it.
It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBase.Form.
It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBase.Base
```
@@ -68,36 +86,46 @@ public class StartForm : FormBase
{
}
//Gets invoked during Navigation to this form
public override async Task Init(params object[] param)
{
}
//Gets invoked if the form gets opened (with Form.NavigateTo(NewForm))
public override async Task Opened()
{
}
//Gets invoked if the form gets leaved
public override async Task Closed()
{
}
//Gets invoked on every Message/Action/Data in this context
public override async Task Load(MessageResult message)
{
await this.Device.Send("Hello world!");
}
//Gets invoked on Button clicks
public override async Task Action(MessageResult message)
{
}
//Gets invoked on Data uploades by the user (of type Photo, Audio, Video, Contact, Location, Document)
public override async Task SentData(DataResult data)
{
}
//Gets invoked on every Message/Action/Data to render Design or Response
public override async Task Render(MessageResult message)
{
@@ -145,46 +173,46 @@ Below we have 4 options.
```
BotBase<Start> bb = new BotBase<Start>("{YOUR API KEY}");
BotBase<Start> bb = new BotBase<Start>("{YOUR API KEY}");
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/params");
bb.SystemCalls.Add("/params");
bb.SystemCall += async (s, en) =>
{
switch (en.Command)
{
case "/form1":
bb.SystemCall += async (s, en) =>
{
switch (en.Command)
{
case "/form1":
var form1 = new TestForm();
var form1 = new TestForm();
await en.Device.ActiveForm.NavigateTo(form1);
await en.Device.ActiveForm.NavigateTo(form1);
break;
break;
case "/form2":
case "/form2":
var form2 = new TestForm2();
var form2 = new TestForm2();
await en.Device.ActiveForm.NavigateTo(form2);
await en.Device.ActiveForm.NavigateTo(form2);
break;
break;
case "/params":
case "/params":
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
await en.Device.Send("Your parameters are " + m, replyTo: en.Device.LastMessage);
await en.Device.Send("Your parameters are " + m, replyTo: en.Device.LastMessage);
break;
}
break;
}
};
};
bb.Start();
bb.Start();
```
@@ -196,47 +224,50 @@ 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 override async Task Opened()
public SimpleForm()
{
await this.Device.Send("Hello world!");
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)\r\nOr\r\nhi, hello, maybe, bye and ciao");
}
public override async Task Load(MessageResult message)
{
//message.MessageText will work also, cause it is a string you could manage a lot different scenerios here
public override async Task Load(MessageResult message)
{
//message.MessageText will work also, cause it is a string you could manage a lot different scenerios here
var messageId = message.MessageId;
var messageId = message.MessageId;
switch (message.Command)
{
case "hello":
case "hi":
switch (message.Command)
{
case "hello":
case "hi":
//Send him a simple message
await this.Device.Send("Hello you there !");
break;
//Send him a simple message
await this.Device.Send("Hello you there !");
break;
case "maybe":
case "maybe":
//Send him a simple message and reply to the one of himself
await this.Device.Send("Maybe what?", replyTo: messageId);
//Send him a simple message and reply to the one of himself
await this.Device.Send("Maybe what?", replyTo: messageId);
break;
case "bye":
case "ciao":
//Send him a simple message
await this.Device.Send("Ok, take care !");
break;
}
}
break;
case "bye":
case "ciao":
//Send him a simple message
await this.Device.Send("Ok, take care !");
break;
}
}
}
```
@@ -339,131 +370,131 @@ Maybe, if i got more ideas, i will add other "controls" in the future.
```
public class ProgressTest : AutoCleanForm
public class ProgressTest : AutoCleanForm
{
public ProgressTest()
{
this.DeleteMode = eDeleteMode.OnLeavingForm;
}
public override async Task Opened()
{
await this.Device.Send("Welcome to ProgressTest");
}
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
await message.ConfirmAction();
if (call == null)
return;
TelegramBotBase.Controls.ProgressBar Bar = null;
switch (call.Value)
{
case "standard":
public ProgressTest()
{
this.DeleteMode = eDeleteMode.OnLeavingForm;
}
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.standard);
Bar.Device = this.Device;
public override async Task Opened()
{
await this.Device.Send("Welcome to ProgressTest");
}
break;
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
case "squares":
await message.ConfirmAction();
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squares);
Bar.Device = this.Device;
break;
if (call == null)
return;
case "circles":
TelegramBotBase.Controls.ProgressBar Bar = null;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.circles);
Bar.Device = this.Device;
switch (call.Value)
{
case "standard":
break;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.standard);
Bar.Device = this.Device;
case "lines":
break;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.lines);
Bar.Device = this.Device;
case "squares":
break;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squares);
Bar.Device = this.Device;
case "squaredlines":
break;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squaredLines);
Bar.Device = this.Device;
case "circles":
break;
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.circles);
Bar.Device = this.Device;
case "start":
break;
var sf = new Start();
case "lines":
await sf.Init();
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.lines);
Bar.Device = this.Device;
await this.NavigateTo(sf);
break;
case "squaredlines":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squaredLines);
Bar.Device = this.Device;
break;
case "start":
var sf = new Start();
await sf.Init();
await this.NavigateTo(sf);
return;
default:
return;
}
//Render Progress bar and show some "example" progress
await Bar.Render();
this.Controls.Add(Bar);
for (int i = 0; i <= 100; i++)
{
Bar.Value++;
await Bar.Render();
Thread.Sleep(250);
}
}
public override async Task Render(MessageResult message)
{
ButtonForm btn = new ButtonForm();
btn.AddButtonRow(new ButtonBase("Standard", new CallbackData("a", "standard").Serialize()), new ButtonBase("Squares", new CallbackData("a", "squares").Serialize()));
btn.AddButtonRow(new ButtonBase("Circles", new CallbackData("a", "circles").Serialize()), new ButtonBase("Lines", new CallbackData("a", "lines").Serialize()));
btn.AddButtonRow(new ButtonBase("Squared Line", new CallbackData("a", "squaredlines").Serialize()));
btn.AddButtonRow(new ButtonBase("Back to start", new CallbackData("a", "start").Serialize()));
await this.Device.Send("Choose your progress bar:", btn);
}
public override async Task Closed()
{
foreach (var b in this.Controls)
{
await b.Cleanup();
}
await this.Device.Send("Ciao from ProgressTest");
}
return;
default:
return;
}
//Render Progress bar and show some "example" progress
await Bar.Render();
this.Controls.Add(Bar);
for (int i = 0; i <= 100; i++)
{
Bar.Value++;
await Bar.Render();
Thread.Sleep(250);
}
}
public override async Task Render(MessageResult message)
{
ButtonForm btn = new ButtonForm();
btn.AddButtonRow(new ButtonBase("Standard", new CallbackData("a", "standard").Serialize()), new ButtonBase("Squares", new CallbackData("a", "squares").Serialize()));
btn.AddButtonRow(new ButtonBase("Circles", new CallbackData("a", "circles").Serialize()), new ButtonBase("Lines", new CallbackData("a", "lines").Serialize()));
btn.AddButtonRow(new ButtonBase("Squared Line", new CallbackData("a", "squaredlines").Serialize()));
btn.AddButtonRow(new ButtonBase("Back to start", new CallbackData("a", "start").Serialize()));
await this.Device.Send("Choose your progress bar:", btn);
}
public override async Task Closed()
{
foreach (var b in this.Controls)
{
await b.Cleanup();
}
await this.Device.Send("Ciao from ProgressTest");
}
}
```
### Registration Example (Example #4 - Registration Form Test)
@@ -473,105 +504,180 @@ To give you an example about the possiblities, i added into the Test project an
```
public class PerForm : AutoCleanForm
public class PerForm : AutoCleanForm
{
public String EMail { get; set; }
public String Firstname { get; set; }
public String Lastname { get; set; }
public async override Task Load(MessageResult message)
{
if (message.MessageText.Trim() == "")
return;
if (this.Firstname == null)
{
public String EMail { get; set; }
this.Firstname = message.MessageText;
return;
}
public String Firstname { get; set; }
if (this.Lastname == null)
{
this.Lastname = message.MessageText;
return;
}
public String Lastname { get; set; }
if (this.EMail == null)
{
this.EMail = message.MessageText;
return;
}
public async override Task Load(MessageResult message)
{
if (message.MessageText.Trim() == "")
return;
}
if (this.Firstname == null)
{
this.Firstname = message.MessageText;
return;
}
public async override Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
if (this.Lastname == null)
{
this.Lastname = message.MessageText;
return;
}
await message.ConfirmAction();
if (this.EMail == null)
{
this.EMail = message.MessageText;
return;
}
if (call == null)
return;
}
switch (call.Value)
{
case "back":
public async override Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
var start = new Start();
await message.ConfirmAction();
if (call == null)
return;
switch (call.Value)
{
case "back":
var start = new Start();
await this.NavigateTo(start);
break;
}
}
public async override Task Render(MessageResult message)
{
if (this.Firstname == null)
{
await this.Device.Send("Please sent your firstname:");
return;
}
if (this.Lastname == null)
{
await this.Device.Send("Please sent your lastname:");
return;
}
if (this.EMail == null)
{
await this.Device.Send("Please sent your email address:");
return;
}
String s = "";
s += "Firstname: " + this.Firstname + "\r\n";
s += "Lastname: " + this.Lastname + "\r\n";
s += "E-Mail: " + this.EMail + "\r\n";
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Back", new CallbackData("a", "back").Serialize()));
await this.Device.Send("Your details:\r\n" + s, bf);
}
await this.NavigateTo(start);
break;
}
}
public async override Task Render(MessageResult message)
{
if (this.Firstname == null)
{
await this.Device.Send("Please sent your firstname:");
return;
}
if (this.Lastname == null)
{
await this.Device.Send("Please sent your lastname:");
return;
}
if (this.EMail == null)
{
await this.Device.Send("Please sent your email address:");
return;
}
String s = "";
s += "Firstname: " + this.Firstname + "\r\n";
s += "Lastname: " + this.Lastname + "\r\n";
s += "E-Mail: " + this.EMail + "\r\n";
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Back", new CallbackData("a", "back").Serialize()));
await this.Device.Send("Your details:\r\n" + s, bf);
}
}
```
There is also a second example, where every of these 3 inputs gets requested by a different formular (class). Just for imagination of the possiblites.
Cause its to much Text, i didnt have added it here. You will find it under [TelegramBaseTest/Tests/Register/PerStep.cs](TelegramBaseTest/Tests/Register/PerStep.cs)
Beginn there and navigate your way through these Steps in the subfolder.
---
## Forms
There are some default types of forms to make the interaction with users easier.
For now we have the following:
- [AlertDialog](#alert-dialog)
Just a simple dialog with one Button.
- [AutoCleanForm](#autocleanform)
A form which needs to be derived from. It will be delete all in the context sent messages to the user after every new message or on leaving the formular and navigates somewhere else.
Makes sense to create a "feeling" of a clean environment for the user. For instance if you have a multilevel menu. This will remove the previously shown menu, and renders the new sub/top level.
- [PromptDialog](#prompt-dialog)
A simple dialog which is able to show multiple buttons and a Text message. The user could select one option and will get redirected to a different form, depending on the click.
### Alert Dialog
```
var fto = new TestForm2();
AlertDialog ad = new AlertDialog("This is a message", "Ok", fto);
await this.NavigateTo(ad);
```
### AutoCleanForm
### Prompt Dialog
#### Without Eventhandler (pre-init of form necessary)
```
PromptDialog pd = new PromptDialog("Please confirm");
pd.AddButton(new ButtonBase("Ok", "ok"));
pd.AddButton(new ButtonBase("Cancel", "cancel"));
var tf = new TestForm2();
pd.ButtonForms.Add("ok", tf);
pd.ButtonForms.Add("cancel", tf);
await this.NavigateTo(pd);
```
#### With Eventhandler (no pre-init of form necessary)
```
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
pd.ButtonClicked += async (s, en) =>
{
var tf = new TestForm2();
//Remember only to navigate from the current running form. (here it is the prompt dialog, cause we have left the above already)
await pd.NavigateTo(tf);
};
await this.NavigateTo(pd);
```
---
I will add more notes to it soon, so stay tuned.
+10 -2
View File
@@ -21,13 +21,19 @@ namespace TelegramBaseTest
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/params");
bb.SystemCall += async (s, en) =>
{
switch (en.Command)
{
case "/start":
var start = new Start();
await en.Device.ActiveForm.NavigateTo(start);
break;
case "/form1":
var form1 = new TestForm();
@@ -47,8 +53,10 @@ namespace TelegramBaseTest
case "/params":
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
await en.Device.Send("Your parameters are: " + m, replyTo: en.Device.LastMessageId);
await en.Device.Send("Your parameters are " + m, replyTo: en.Device.LastMessage);
en.Handled = true;
break;
}
+1
View File
@@ -53,6 +53,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\ButtonTestForm.cs" />
<Compile Include="Tests\DataForm.cs" />
<Compile Include="Tests\ProgressTest.cs" />
<Compile Include="Tests\Register\PerForm.cs" />
<Compile Include="Tests\Register\PerStep.cs" />
-2
View File
@@ -59,8 +59,6 @@ namespace TelegramBaseTest.Tests
var st = new Start();
await st.Init();
await this.NavigateTo(st);
break;
+142
View File
@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
{
public class DataForm : AutoCleanForm
{
public override async Task SentData(DataResult data)
{
String tmp = "";
InputOnlineFile file;
switch (data.Type)
{
case Telegram.Bot.Types.Enums.MessageType.Contact:
tmp += "Firstname: " + data.Contact.FirstName + "\r\n";
tmp += "Lastname: " + data.Contact.LastName + "\r\n";
tmp += "Phonenumber: " + data.Contact.PhoneNumber + "\r\n";
tmp += "UserId: " + data.Contact.UserId + "\r\n";
await this.Device.Send("Your contact: \r\n" + tmp, replyTo: data.MessageId);
break;
case Telegram.Bot.Types.Enums.MessageType.Document:
file = new InputOnlineFile(data.Document.FileId);
await this.Device.SendDocument(file, "Your uploaded document");
break;
case Telegram.Bot.Types.Enums.MessageType.Video:
file = new InputOnlineFile(data.Document.FileId);
await this.Device.SendDocument(file, "Your uploaded video");
break;
case Telegram.Bot.Types.Enums.MessageType.Audio:
file = new InputOnlineFile(data.Document.FileId);
await this.Device.SendDocument(file, "Your uploaded audio");
break;
case Telegram.Bot.Types.Enums.MessageType.Location:
tmp += "Lat: " + data.Location.Latitude + "\r\n";
tmp += "Lng: " + data.Location.Longitude + "\r\n";
await this.Device.Send("Your location: \r\n" + tmp, replyTo: data.MessageId);
break;
case Telegram.Bot.Types.Enums.MessageType.Photo:
InputOnlineFile photo = new InputOnlineFile(data.Photos.Last().FileId);
await this.Device.Send("Your image: ", replyTo: data.MessageId);
await this.Client.TelegramClient.SendPhotoAsync(this.Device.DeviceId, photo);
break;
default:
await this.Device.Send("Unknown response");
break;
}
}
public override async Task Action(MessageResult message)
{
await message.ConfirmAction();
if (message.Handled)
return;
switch (message.RawData)
{
case "contact":
await this.Device.RequestContact();
break;
case "location":
await this.Device.RequestLocation();
break;
case "back":
message.Handled = true;
var start = new Start();
await this.NavigateTo(start);
break;
}
}
public override async Task Render(MessageResult message)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Request User contact", "contact"));
bf.AddButtonRow(new ButtonBase("Request User location", "location"));
bf.AddButtonRow(new ButtonBase("Back", "back"));
InlineKeyboardMarkup ikv = bf;
await this.Device.Send("Please upload a contact, photo, video, audio, document or location.", bf);
}
}
}
-2
View File
@@ -75,8 +75,6 @@ namespace TelegramBaseTest.Tests
var sf = new Start();
await sf.Init();
await this.NavigateTo(sf);
return;
+8 -4
View File
@@ -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");
}
@@ -50,8 +56,6 @@ namespace TelegramBaseTest.Tests
var st = new Start();
await st.Init();
await this.NavigateTo(st);
break;
+32
View File
@@ -53,6 +53,32 @@ namespace TelegramBaseTest.Tests
await this.NavigateTo(reg);
break;
case "form1":
var form1 = new TestForm();
await this.NavigateTo(form1);
break;
case "form2":
var form2 = new TestForm2();
await this.NavigateTo(form2);
break;
case "data":
message.Handled = true;
var data = new DataForm();
await this.NavigateTo(data);
break;
}
@@ -68,6 +94,12 @@ namespace TelegramBaseTest.Tests
btn.AddButtonRow(new ButtonBase("#3 Progress Bar", new CallbackData("a", "progress").Serialize()));
btn.AddButtonRow(new ButtonBase("#4 Registration Example", new CallbackData("a", "registration").Serialize()));
btn.AddButtonRow(new ButtonBase("#5 Form1 Command", new CallbackData("a", "form1").Serialize()));
btn.AddButtonRow(new ButtonBase("#6 Form2 Command", new CallbackData("a", "form2").Serialize()));
btn.AddButtonRow(new ButtonBase("#7 Data Handling", new CallbackData("a", "data").Serialize()));
await this.Device.Send("Choose your test:", btn);
}
-12
View File
@@ -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)
{
@@ -51,8 +41,6 @@ namespace TelegramBaseTest.Tests
var tf = new TestForm2();
await tf.Init();
await this.NavigateTo(tf);
break;
+23 -17
View File
@@ -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)
{
@@ -48,30 +40,40 @@ namespace TelegramBaseTest.Tests
var tf = new TestForm();
await tf.Init();
await this.NavigateTo(tf);
}
else if (call.Value == "alert")
{
var fto = new TestForm2();
await fto.Init();
AlertDialog ad = new AlertDialog("This is a message", "Ok", fto);
await this.NavigateTo(ad);
}
else if (call.Value == "prompt")
{
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
PromptDialog pd = new PromptDialog("Please confirm");
pd.AddButton(new ButtonBase("Ok", "ok"));
pd.AddButton(new ButtonBase("Cancel", "cancel"));
var tf = new TestForm2();
await tf.Init();
pd.ButtonForms.Add("ok", tf);
pd.ButtonForms.Add("cancel", tf);
await this.NavigateTo(pd);
}
else if (call.Value == "promptevt")
{
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
pd.ButtonClicked += async (s, en) =>
{
var tf = new TestForm2();
await pd.NavigateTo(tf);
};
await this.NavigateTo(pd);
}
@@ -98,7 +100,11 @@ namespace TelegramBaseTest.Tests
//btn.AddButtonRow(new ButtonBase("Zum Testformular 1", CallbackData.Create("navigate", "testform1")), new ButtonBase("Zum Testformular 1", CallbackData.Create("navigate", "testform1")));
btn.AddButtonRow(new ButtonBase("Information Prompt", CallbackData.Create("navigate", "alert")), new ButtonBase("Confirmation Prompt", CallbackData.Create("navigate", "prompt")));
btn.AddButtonRow(new ButtonBase("Information Prompt", CallbackData.Create("navigate", "alert")));
btn.AddButtonRow(new ButtonBase("Confirmation Prompt without event", CallbackData.Create("navigate", "prompt")));
btn.AddButtonRow(new ButtonBase("Confirmation Prompt with event", CallbackData.Create("navigate", "promptevt")));
await this.Device.SendPhoto(bmp, "Test", btn);
+129
View File
@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputFiles;
namespace TelegramBotBase.Base
{
/// <summary>
/// Returns a class to manage attachments within messages.
/// </summary>
public class DataResult : ResultBase
{
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
public Contact Contact
{
get
{
return this.Message.Contact;
}
}
public Location Location
{
get
{
return this.Message.Location;
}
}
public Document Document
{
get
{
return this.Message.Document;
}
}
public Audio Audio
{
get
{
return this.Message.Audio;
}
}
public Video Video
{
get
{
return this.Message.Video;
}
}
public PhotoSize[] Photos
{
get
{
return this.Message.Photo;
}
}
public Telegram.Bot.Types.Enums.MessageType Type
{
get
{
return this.RawMessageData?.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
}
}
/// <summary>
/// Returns the FileId of the first reachable element.
/// </summary>
public String FileId
{
get
{
return (this.Document?.FileId ??
this.Audio?.FileId ??
this.Video?.FileId ??
this.Photos.FirstOrDefault()?.FileId);
}
}
public DataResult(Telegram.Bot.Args.MessageEventArgs rawdata)
{
this.RawMessageData = rawdata;
this.Message = rawdata.Message;
}
public DataResult(MessageResult message)
{
this.RawMessageData = message.RawMessageData;
this.Message = message.Message;
}
public async Task<InputOnlineFile> DownloadDocument()
{
var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, this.Document.FileName);
}
public async Task<InputOnlineFile> DownloadVideo()
{
var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task<InputOnlineFile> DownloadPhoto(int index)
{
var photo = this.Photos[index];
var encryptedContent = new System.IO.MemoryStream(photo.FileSize);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
}
}
+34 -4
View File
@@ -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; }
@@ -44,6 +44,10 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if gets navigated to this form
/// </summary>
/// <returns></returns>
public virtual async Task Opened()
{
@@ -62,17 +66,41 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if the form gets loaded and on every message belongs to this context
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Load(MessageResult message)
{
}
/// <summary>
/// Gets invoked if the user has clicked a button.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Action(MessageResult message)
{
}
/// <summary>
/// Gets invoked if the user has sent some media (Photo, Audio, Video, Contact, Location, Document)
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task SentData(DataResult message)
{
}
/// <summary>
/// Gets invoked at the end of the cycle to "Render" text, images, buttons, etc...
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Render(MessageResult message)
{
@@ -89,7 +117,9 @@ namespace TelegramBotBase.Form
if (ds == null)
return;
this.FormSwitched = true;
ds.FormSwitched = true;
ds.PreviousForm = ds.ActiveForm;
ds.ActiveForm = newForm;
newForm.Client = this.Client;
@@ -109,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;
}
}
}
+5 -1
View File
@@ -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>
@@ -62,7 +66,7 @@ namespace TelegramBotBase.Base
}
/// <summary>
/// Is this a system call ? Starts with a slash and command
/// Is this a system call ? Starts with a slash '/' and a command
/// </summary>
public bool IsSystemCall
{
+8 -8
View File
@@ -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;
}
+5 -1
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
@@ -22,6 +23,8 @@ namespace TelegramBotBase.Base
public bool Handled { get; set; } = false;
public Message OriginalMessage { get; set; }
public SystemCallEventArgs()
{
@@ -29,10 +32,11 @@ namespace TelegramBotBase.Base
}
public SystemCallEventArgs(String Command, List<String> Parameters, long DeviceId, DeviceSession Device)
public SystemCallEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
{
this.Command = Command;
this.Parameters = Parameters;
this.OriginalMessage = Message;
this.DeviceId = DeviceId;
this.Device = Device;
}
+65 -26
View File
@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Sessions;
@@ -42,6 +43,8 @@ namespace TelegramBotBase
private static object __evSystemCall = new object();
public delegate Task SystemCallEventHandler(object sender, SystemCallEventArgs e);
private static object __evException = new object();
private static object __evUnhandledCall = new object();
@@ -95,6 +98,23 @@ namespace TelegramBotBase
this.Sessions.Client = this.Client;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a TelegramBotClient instance.
/// </summary>
/// <param name="apiKey"></param>
/// <param name="client"></param>
public BotBase(String apiKey, TelegramBotClient client)
{
this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey, client);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a proxyAdress
/// </summary>
@@ -181,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);
}
@@ -203,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;
@@ -219,10 +243,11 @@ namespace TelegramBotBase
//Is this a systemcall ?
if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand))
{
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, ds.DeviceId, ds);
OnSystemCall(sce);
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, e.Message, ds.DeviceId, ds);
await OnSystemCall(sce);
return;
if (sce.Handled)
return;
}
FormBase activeForm = null;
@@ -233,6 +258,9 @@ namespace TelegramBotBase
do
{
i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm;
@@ -246,11 +274,18 @@ namespace TelegramBotBase
//Loading Event
await activeForm.Load(e);
//Is Attachment ? (Photo, Audio, Video, Contact, Location, Document)
if (e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Contact | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Document | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Location |
e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Photo | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Video | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Audio)
{
await activeForm.SentData(new DataResult(e));
}
//Render Event
if (!activeForm.FormSwitched)
if (!ds.FormSwitched)
await activeForm.Render(e);
} while (activeForm.FormSwitched && i < NavigationMaximum);
} while (ds.FormSwitched && i < NavigationMaximum);
}
@@ -258,6 +293,14 @@ namespace TelegramBotBase
{
try
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
e.Device = ds;
if (LogAllMessages)
{
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
}
Client_TryAction(sender, e);
}
catch (Exception ex)
@@ -269,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;
}
@@ -288,6 +332,9 @@ namespace TelegramBotBase
{
i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm;
//If the form manages the events by itselfs, skip here
@@ -301,7 +348,7 @@ namespace TelegramBotBase
await activeForm.Load(e);
//Action Event
if (!activeForm.FormSwitched)
if (!ds.FormSwitched)
{
await activeForm.Action(e);
@@ -312,7 +359,7 @@ namespace TelegramBotBase
if (uhc.Handled)
{
if (activeForm.FormSwitched)
if (ds.FormSwitched)
{
continue;
}
@@ -326,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);
}
@@ -379,21 +426,13 @@ namespace TelegramBotBase
/// <summary>
/// Will be called if a system call gets raised
/// </summary>
public event EventHandler<SystemCallEventArgs> SystemCall
{
add
{
this.__Events.AddHandler(__evSystemCall, value);
}
remove
{
this.__Events.RemoveHandler(__evSystemCall, value);
}
}
public event SystemCallEventHandler SystemCall;
public void OnSystemCall(SystemCallEventArgs e)
public async Task OnSystemCall(SystemCallEventArgs e)
{
(this.__Events[__evSystemCall] as EventHandler<SystemCallEventArgs>)?.Invoke(this, e);
if (this.SystemCall != null)
await SystemCall(this, e);
}
/// <summary>
+63 -14
View File
@@ -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);
}
}
}
}
+9 -2
View File
@@ -38,6 +38,15 @@ namespace TelegramBotBase.Form
this.Buttons = Buttons.ToList();
}
/// <summary>
/// Adds one Button
/// </summary>
/// <param name="button"></param>
public void AddButton(ButtonBase button)
{
this.Buttons.Add(button);
}
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
@@ -50,7 +59,6 @@ namespace TelegramBotBase.Form
await message.DeleteMessage();
ButtonBase button = this.Buttons.FirstOrDefault(a => a.Value == call.Value);
if (button == null)
@@ -73,7 +81,6 @@ namespace TelegramBotBase.Form
{
ButtonForm btn = new ButtonForm();
var buttons = this.Buttons.Select(a => new ButtonBase(a.Text, CallbackData.Create("action", a.Value))).ToList();
btn.AddButtonRow(buttons);
+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
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.4.2.0")]
+113 -10
View File
@@ -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()
{
@@ -114,6 +136,40 @@ namespace TelegramBotBase.Sessions
try
{
var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, replyMarkup: markup);
return m;
}
catch
{
}
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
@@ -149,6 +205,8 @@ namespace TelegramBotBase.Sessions
try
{
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
OnMessageSent(new MessageSentEventArgs(m));
}
catch (ApiRequestException ex)
{
@@ -159,9 +217,6 @@ namespace TelegramBotBase.Sessions
return null;
}
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
return m;
}
@@ -183,6 +238,8 @@ namespace TelegramBotBase.Sessions
try
{
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
OnMessageSent(new MessageSentEventArgs(m));
}
catch (ApiRequestException ex)
{
@@ -193,8 +250,6 @@ namespace TelegramBotBase.Sessions
return null;
}
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
return m;
}
@@ -222,6 +277,8 @@ namespace TelegramBotBase.Sessions
try
{
m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
OnMessageSent(new MessageSentEventArgs(m));
}
catch (ApiRequestException ex)
{
@@ -232,8 +289,6 @@ namespace TelegramBotBase.Sessions
return null;
}
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
return m;
}
@@ -319,7 +374,7 @@ 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;
}
@@ -334,6 +389,34 @@ namespace TelegramBotBase.Sessions
await this.Client.TelegramClient.SendChatActionAsync(this.DeviceId, action);
}
/// <summary>
/// Requests the contact from the user.
/// </summary>
/// <param name="buttonText"></param>
/// <param name="requestMessage"></param>
/// <param name="OneTimeOnly"></param>
/// <returns></returns>
public async Task<Message> RequestContact(String buttonText = "Send your contact", String requestMessage = "Give me your phone number!", bool OneTimeOnly = true)
{
var rck = new ReplyKeyboardMarkup(KeyboardButton.WithRequestContact(buttonText));
rck.OneTimeKeyboard = OneTimeOnly;
return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rck);
}
/// <summary>
/// Requests the location from the user.
/// </summary>
/// <param name="buttonText"></param>
/// <param name="requestMessage"></param>
/// <param name="OneTimeOnly"></param>
/// <returns></returns>
public async Task<Message> RequestLocation(String buttonText = "Send your location", String requestMessage = "Give me your location!", bool OneTimeOnly = true)
{
var rcl = new ReplyKeyboardMarkup(KeyboardButton.WithRequestLocation(buttonText));
rcl.OneTimeKeyboard = OneTimeOnly;
return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl);
}
/// <summary>
/// Deletes a message
/// </summary>
@@ -386,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);
}
}
}
+1
View File
@@ -51,6 +51,7 @@
<ItemGroup>
<Compile Include="Base\ButtonClickedEventArgs.cs" />
<Compile Include="Base\ControlBase.cs" />
<Compile Include="Base\DataResult.cs" />
<Compile Include="Base\MessageClient.cs" />
<Compile Include="Base\MessageIncomeResult.cs" />
<Compile Include="Base\MessageResult.cs" />