Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2db44fe4c | ||
|
|
3a78a98b9e | ||
|
|
b14fa2dc87 | ||
|
|
5cd54fc746 | ||
|
|
fb68da6ab1 | ||
|
|
2f7ded8ce5 | ||
|
|
906177a5f9 | ||
|
|
873c91a165 | ||
|
|
46c1acbf99 | ||
|
|
acd06a6948 | ||
|
|
a97305fa35 | ||
|
|
16baef0c45 | ||
|
|
211bc55aff | ||
|
|
19f3c9c195 | ||
|
|
c6666f72bc | ||
|
|
bf7f6f851a | ||
|
|
816bf4cbbe | ||
|
|
da80af86e3 |
@@ -8,19 +8,12 @@
|
||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
||||
[](https://www.nuget.org/packages/TelegramBotBase)
|
||||
|
||||
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.
|
||||
Test the Testproject: [@TGBaseBot](https://t.me/TGBaseBot)
|
||||
|
||||
---
|
||||
|
||||
## 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 +26,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 +55,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
|
||||
|
||||
|
||||
```
|
||||
@@ -69,35 +83,45 @@ 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)
|
||||
{
|
||||
|
||||
@@ -235,8 +259,6 @@ public class SimpleForm : FormBase
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
@@ -572,6 +594,81 @@ There is also a second example, where every of these 3 inputs gets requested by
|
||||
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.
|
||||
|
||||
@@ -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();
|
||||
@@ -48,7 +54,9 @@ namespace TelegramBaseTest
|
||||
|
||||
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.LastMessageId);
|
||||
|
||||
en.Handled = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -59,8 +59,6 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
var st = new Start();
|
||||
|
||||
await st.Init();
|
||||
|
||||
await this.NavigateTo(st);
|
||||
|
||||
break;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -75,8 +75,6 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
var sf = new Start();
|
||||
|
||||
await sf.Init();
|
||||
|
||||
await this.NavigateTo(sf);
|
||||
|
||||
return;
|
||||
|
||||
@@ -50,8 +50,6 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
var st = new Start();
|
||||
|
||||
await st.Init();
|
||||
|
||||
await this.NavigateTo(st);
|
||||
|
||||
break;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,8 +51,6 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
var tf = new TestForm2();
|
||||
|
||||
await tf.Init();
|
||||
|
||||
await this.NavigateTo(tf);
|
||||
|
||||
break;
|
||||
|
||||
@@ -48,33 +48,43 @@ 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 +108,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);
|
||||
|
||||
@@ -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, "");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
|
||||
@@ -62,7 +62,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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+41
-15
@@ -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>
|
||||
@@ -219,9 +239,10 @@ 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);
|
||||
|
||||
if (sce.Handled)
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,6 +267,13 @@ 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)
|
||||
await activeForm.Render(e);
|
||||
@@ -258,6 +286,12 @@ namespace TelegramBotBase
|
||||
{
|
||||
try
|
||||
{
|
||||
if (LogAllMessages)
|
||||
{
|
||||
DeviceSession ds2 = this.Sessions.GetSession(e.DeviceId);
|
||||
OnMessage(new MessageIncomeResult(e.DeviceId, ds2, e));
|
||||
}
|
||||
|
||||
Client_TryAction(sender, e);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -379,21 +413,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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.4.0.0")]
|
||||
|
||||
@@ -114,6 +114,7 @@ namespace TelegramBotBase.Sessions
|
||||
try
|
||||
{
|
||||
var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, replyMarkup: markup);
|
||||
|
||||
return m;
|
||||
}
|
||||
catch
|
||||
@@ -149,6 +150,8 @@ namespace TelegramBotBase.Sessions
|
||||
try
|
||||
{
|
||||
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
}
|
||||
catch (ApiRequestException ex)
|
||||
{
|
||||
@@ -159,9 +162,6 @@ namespace TelegramBotBase.Sessions
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -183,6 +183,8 @@ namespace TelegramBotBase.Sessions
|
||||
try
|
||||
{
|
||||
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
}
|
||||
catch (ApiRequestException ex)
|
||||
{
|
||||
@@ -193,8 +195,6 @@ namespace TelegramBotBase.Sessions
|
||||
return null;
|
||||
}
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -222,6 +222,8 @@ namespace TelegramBotBase.Sessions
|
||||
try
|
||||
{
|
||||
m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
}
|
||||
catch (ApiRequestException ex)
|
||||
{
|
||||
@@ -232,8 +234,6 @@ namespace TelegramBotBase.Sessions
|
||||
return null;
|
||||
}
|
||||
|
||||
OnMessageSent(new MessageSentEventArgs(m.MessageId, m));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -334,6 +334,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>
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user