Update Testproject
This commit is contained in:
parent
6a42d75267
commit
9416e2ec85
149
README.md
149
README.md
@ -1,9 +1,9 @@
|
||||
Hey guys,
|
||||
|
||||
here we are. After some time and thoughts i give my Telegram Bot framework to public.
|
||||
here we are. After some time and thoughts i give my TelegramBot framework to public.
|
||||
It is based on C#.
|
||||
|
||||
It is module which is based on the original TelegramBotLibrary you will find in nuget.
|
||||
It is a module which is based on the original TelegramBotLibrary 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.
|
||||
|
||||
@ -11,7 +11,10 @@ It gives you features which will look/feel like WinForms or have a good way to c
|
||||
|
||||
## How to start:
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
|
||||
```
|
||||
@ -94,11 +97,13 @@ await tf.Init();
|
||||
await this.NavigateTo(tf);
|
||||
```
|
||||
|
||||
## Simple Message Handling
|
||||
## Message Handling
|
||||
|
||||
On every input the user is sending back to the bot Action event get raised. So here we could manage to send something back to him. For sure we could also manage different button inputs:
|
||||
All examples are within the test project, so just try it out on your own.
|
||||
|
||||
### Lets start with text messages
|
||||
On every input the user is sending back to the bot the Action event gets raised. So here we could manage to send something back to him. For sure we could also manage different button inputs:
|
||||
|
||||
### Lets start with text messages (Example #1 - Simple Test)
|
||||
|
||||
|
||||
|
||||
@ -106,43 +111,41 @@ On every input the user is sending back to the bot Action event get raised. So h
|
||||
public class SimpleForm : FormBase
|
||||
{
|
||||
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
public override async Task Opened()
|
||||
{
|
||||
await this.Device.Send("Hello world!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
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
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -150,6 +153,90 @@ public class SimpleForm : FormBase
|
||||
|
||||
```
|
||||
|
||||
### Now some buttons (Example #2 - Button Test)
|
||||
|
||||
I using a different base class (AutoCleanForm) I created for a better "feeling" inside the bot which will delete "old" messages from this form. You have some settings within this class to manage when messages should be getting deleted.
|
||||
|
||||
|
||||
```
|
||||
public class ButtonTestForm : AutoCleanForm
|
||||
{
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
await this.Device.Send("Hello world! (Click 'back' to get back to Start)");
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
|
||||
var call = message.GetData<CallbackData>();
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
switch (call.Value)
|
||||
{
|
||||
case "button1":
|
||||
|
||||
await this.Device.Send("Button 1 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button2":
|
||||
|
||||
await this.Device.Send("Button 2 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button3":
|
||||
|
||||
await this.Device.Send("Button 3 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button4":
|
||||
|
||||
await this.Device.Send("Button 4 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "back":
|
||||
|
||||
var st = new Start();
|
||||
|
||||
await st.Init();
|
||||
|
||||
await this.NavigateTo(st);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
|
||||
ButtonForm btn = new ButtonForm();
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Button 1", new CallbackData("a", "button1").Serialize()), new ButtonBase("Button 2", new CallbackData("a", "button2").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Button 3", new CallbackData("a", "button3").Serialize()), new ButtonBase("Button 4", new CallbackData("a", "button4").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Back", new CallbackData("a", "back").Serialize()));
|
||||
|
||||
await this.Device.Send("Click a button", btn);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ namespace TelegramBaseTest
|
||||
|
||||
String APIKey = "";
|
||||
|
||||
BotBase<TestForm2> bb = new BotBase<TestForm2>(APIKey);
|
||||
BotBase<Start> bb = new BotBase<Start>(APIKey);
|
||||
|
||||
bb.SystemCalls.Add("/start");
|
||||
bb.SystemCalls.Add("/form1");
|
||||
|
||||
@ -54,6 +54,9 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tests\ButtonTestForm.cs" />
|
||||
<Compile Include="Tests\SimpleForm.cs" />
|
||||
<Compile Include="Tests\Start.cs" />
|
||||
<Compile Include="Tests\TestForm.cs" />
|
||||
<Compile Include="Tests\TestForm2.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
89
TelegramBaseTest/Tests/ButtonTestForm.cs
Normal file
89
TelegramBaseTest/Tests/ButtonTestForm.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBaseTest.Tests
|
||||
{
|
||||
public class ButtonTestForm : AutoCleanForm
|
||||
{
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
await this.Device.Send("Hello world! (Click 'back' to get back to Start)");
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
|
||||
var call = message.GetData<CallbackData>();
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
switch (call.Value)
|
||||
{
|
||||
case "button1":
|
||||
|
||||
await this.Device.Send("Button 1 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button2":
|
||||
|
||||
await this.Device.Send("Button 2 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button3":
|
||||
|
||||
await this.Device.Send("Button 3 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "button4":
|
||||
|
||||
await this.Device.Send("Button 4 pressed");
|
||||
|
||||
break;
|
||||
|
||||
case "back":
|
||||
|
||||
var st = new Start();
|
||||
|
||||
await st.Init();
|
||||
|
||||
await this.NavigateTo(st);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
|
||||
ButtonForm btn = new ButtonForm();
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Button 1", new CallbackData("a", "button1").Serialize()), new ButtonBase("Button 2", new CallbackData("a", "button2").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Button 3", new CallbackData("a", "button3").Serialize()), new ButtonBase("Button 4", new CallbackData("a", "button4").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Back", new CallbackData("a", "back").Serialize()));
|
||||
|
||||
await this.Device.Send("Click a button", btn);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
63
TelegramBaseTest/Tests/SimpleForm.cs
Normal file
63
TelegramBaseTest/Tests/SimpleForm.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBaseTest.Tests
|
||||
{
|
||||
public class SimpleForm : FormBase
|
||||
{
|
||||
|
||||
public override async Task Opened()
|
||||
{
|
||||
await this.Device.Send("Hello world! (send 'back' to get back to Start)");
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
switch (message.Command)
|
||||
{
|
||||
case "hello":
|
||||
case "hi":
|
||||
|
||||
//Send him a simple message
|
||||
await this.Device.Send("Hello you there !");
|
||||
break;
|
||||
|
||||
case "maybe":
|
||||
|
||||
//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;
|
||||
|
||||
case "back":
|
||||
|
||||
var st = new Start();
|
||||
|
||||
await st.Init();
|
||||
|
||||
await this.NavigateTo(st);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
63
TelegramBaseTest/Tests/Start.cs
Normal file
63
TelegramBaseTest/Tests/Start.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBaseTest.Tests
|
||||
{
|
||||
public class Start : FormBase
|
||||
{
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
var call = message.GetData<CallbackData>();
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
switch(call.Value)
|
||||
{
|
||||
case "text":
|
||||
|
||||
var sf = new SimpleForm();
|
||||
|
||||
await sf.Init();
|
||||
|
||||
await this.NavigateTo(sf);
|
||||
|
||||
break;
|
||||
|
||||
case "buttons":
|
||||
|
||||
var bf = new ButtonTestForm();
|
||||
|
||||
await bf.Init();
|
||||
|
||||
await this.NavigateTo(bf);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
|
||||
ButtonForm btn = new ButtonForm();
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#1 Simple Text", new CallbackData("a", "text").Serialize()), new ButtonBase("#2 Button Test", new CallbackData("a", "buttons").Serialize()));
|
||||
|
||||
|
||||
await this.Device.Send("Choose your test:", btn);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user