Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3340ef7b76 | ||
|
|
211424569b | ||
|
|
8303b0c87f | ||
|
|
19856ab044 | ||
|
|
5453add956 | ||
|
|
ff3c22566a | ||
|
|
50fd5255c8 | ||
|
|
4f85677deb | ||
|
|
7d2703138c | ||
|
|
d0d93a07c0 | ||
|
|
f03e67f345 | ||
|
|
18bb6584f4 | ||
|
|
d5a89a4ed8 | ||
|
|
09d7164385 | ||
|
|
2b914f6f58 | ||
|
|
b65ec13d8b | ||
|
|
ca1c382924 | ||
|
|
584bb76e0f | ||
|
|
c5fa153d54 | ||
|
|
c6106f17ec | ||
|
|
4f97e2a919 | ||
|
|
21c6b2fde5 | ||
|
|
caf9b3cc8e | ||
|
|
27e45b397d | ||
|
|
b8bdc756d4 | ||
|
|
e5d8e403a0 | ||
|
|
2c58f86524 | ||
|
|
4bd43de0fe | ||
|
|
871d25b95b | ||
|
|
b508a7e3ce | ||
|
|
0ebba5e204 | ||
|
|
1cc0a2bdf7 | ||
|
|
fc33da0496 | ||
|
|
35045f3aeb | ||
|
|
3b8b24593f | ||
|
|
03565aba98 | ||
|
|
bbc436d1d6 | ||
|
|
4026f9ba4e | ||
|
|
fb38d32e9e | ||
|
|
077bd1384f | ||
|
|
e28663138b | ||
|
|
65ec7f1c2b | ||
|
|
1fc581b959 | ||
|
|
31e52887ba | ||
|
|
e9c25ea9e4 | ||
|
|
2aee486370 | ||
|
|
c1a0b12a94 | ||
|
|
2bd4634624 | ||
|
|
0bc77ca79c | ||
|
|
d0edf8531c | ||
|
|
b001d5e9c4 | ||
|
|
e1d5051288 | ||
|
|
d00c5e1799 | ||
|
|
c4a31e987b | ||
|
|
eafb81bf7b | ||
|
|
1dff9d5ffe | ||
|
|
755f5a245b | ||
|
|
a22ede0f4f | ||
|
|
8e29652148 | ||
|
|
5bb316d874 | ||
|
|
d2ae08771e | ||
|
|
9162c0894c | ||
|
|
e0ec133209 | ||
|
|
7004ee5963 | ||
|
|
ef8ca3c988 | ||
|
|
f021f12486 | ||
|
|
d06e1eaa52 | ||
|
|
23969881c1 | ||
|
|
9fa3df2889 | ||
|
|
6ed91224eb | ||
|
|
aabc7f04b7 | ||
|
|
71433c0e4e | ||
|
|
894d0799c8 | ||
|
|
6c2543630d | ||
|
|
84d18f6f45 | ||
|
|
5f49e25458 | ||
|
|
f28c9b1e22 | ||
|
|
0db0c00cd0 | ||
|
|
2158f53cde | ||
|
|
fd7d72b986 | ||
|
|
47a5b502b9 | ||
|
|
ce4c7b6f1c | ||
|
|
5bae650edc | ||
|
|
9eae63dae6 | ||
|
|
6f005114bc | ||
|
|
00440a50e8 | ||
|
|
ae9dd17ae3 | ||
|
|
be2660239a | ||
|
|
e88114049e | ||
|
|
177c198916 | ||
|
|
1f7a9900c4 | ||
|
|
d6051ce607 | ||
|
|
5613e84bc3 | ||
|
|
d52fd41467 | ||
|
|
8df6e4ce55 | ||
|
|
9012ec76b1 | ||
|
|
36fb40b6bd | ||
|
|
47aa8c2aaa | ||
|
|
1709631965 | ||
|
|
264a998052 | ||
|
|
a00aaacbfb | ||
|
|
bd908db867 | ||
|
|
e2c485aede | ||
|
|
c8936d0748 | ||
|
|
92f12735c8 | ||
|
|
8142b626b2 | ||
|
|
5dcbe04a73 | ||
|
|
e742ded371 | ||
|
|
706b0f522a | ||
|
|
61e3652edf | ||
|
|
c484edd427 | ||
|
|
181902336c | ||
|
|
dc364b5fd3 | ||
|
|
1fd4752c0e | ||
|
|
d84be50243 | ||
|
|
1caa71cd25 | ||
|
|
e81e5f1854 | ||
|
|
399e056a8f | ||
|
|
26c4f479fa | ||
|
|
72d94df907 | ||
|
|
31e0683699 | ||
|
|
676be49a62 | ||
|
|
5b67f334ff | ||
|
|
6a556af070 | ||
|
|
4910a04923 | ||
|
|
10f4e1b745 | ||
|
|
42b624c28f | ||
|
|
75013d51bd | ||
|
|
7f3bcfe418 | ||
|
|
f5e26924d8 | ||
|
|
884aa60fac | ||
|
|
adc18891e0 | ||
|
|
07eb4bef75 | ||
|
|
dfe64b22ab | ||
|
|
15a8b8897f | ||
|
|
91048b4f7c | ||
|
|
426bb078af | ||
|
|
63deb978a4 | ||
|
|
1a2b5818aa | ||
|
|
fc44b7d38c | ||
|
|
dc8cb81284 | ||
|
|
0202fc29a8 | ||
|
|
7e51126fd3 | ||
|
|
67e82acf20 | ||
|
|
c32fc2c3da | ||
|
|
4351fc7c68 | ||
|
|
80d43ac369 | ||
|
|
376ba68e8e | ||
|
|
6b404545df | ||
|
|
a524fef0d5 | ||
|
|
73ba36ea2b | ||
|
|
46160b54ae | ||
|
|
87690a3630 | ||
|
|
975a942993 | ||
|
|
4336ff1681 | ||
|
|
567a9f937f | ||
|
|
95b4ff77c5 | ||
|
|
ba82c11d66 | ||
|
|
c2e33da277 | ||
|
|
259fa54236 | ||
|
|
f790a16e85 | ||
|
|
97fea333f7 | ||
|
|
e1b31b0b9a | ||
|
|
7aca5a648f | ||
|
|
f24c309f63 | ||
|
|
ba9fd8062c | ||
|
|
b37c31f2bd | ||
|
|
2c34a178ee | ||
|
|
2f030f3c12 | ||
|
|
a364562fd3 | ||
|
|
8ef8733a2b | ||
|
|
049b2081ec | ||
|
|
48b76a84be | ||
|
|
31c8231b2d | ||
|
|
e9504f3b36 | ||
|
|
8592f4279d | ||
|
|
2457773a12 | ||
|
|
46e65faf31 | ||
|
|
193a4811bf | ||
|
|
08a0de8087 | ||
|
|
82469cff70 | ||
|
|
81eafe1219 | ||
|
|
183e89c36f | ||
|
|
ad6cf9539c | ||
|
|
46576eb819 | ||
|
|
cf9e2285f1 | ||
|
|
ecfacdadbb | ||
|
|
c00c2e8d8f | ||
|
|
bfd905d590 | ||
|
|
7b1bf75dde | ||
|
|
7a7a65c3a7 | ||
|
|
8a42a861cf | ||
|
|
47876681e0 | ||
|
|
9314b4c0e5 | ||
|
|
01c19b9900 | ||
|
|
0af587d27f | ||
|
|
97b9176c3a | ||
|
|
dfa479d409 | ||
|
|
07dec0dc9d | ||
|
|
2fc16b18da | ||
|
|
11eeb0dcab | ||
|
|
fbfcbe99fb | ||
|
|
948c1368d7 | ||
|
|
d66df4978d | ||
|
|
7d8895850a | ||
|
|
5e87d00c58 | ||
|
|
efe5fef7d0 | ||
|
|
eebb5cb6f5 | ||
|
|
992065d289 | ||
|
|
1acfeb873f | ||
|
|
de35ebb66e | ||
|
|
2c3e707673 | ||
|
|
6d39d2fb73 | ||
|
|
ef667d12d8 | ||
|
|
d24f8e7ac5 | ||
|
|
e5dc3fd2bc | ||
|
|
6ebb96bd93 | ||
|
|
a1b3b9246b | ||
|
|
25a0ff2e3e | ||
|
|
b74233d66d | ||
|
|
fb7b624ad8 | ||
|
|
965e6e18b9 | ||
|
|
b1c518cbd3 | ||
|
|
ab0ae5cda4 | ||
|
|
b850330f65 | ||
|
|
317dad7d24 |
@@ -0,0 +1,2 @@
|
||||
github: #[MajMcCloud]
|
||||
custom: ["https://paypal.me/majmccloud", dahn.partners]
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{673A56F5-6110-4AED-A68D-562FD6ED3EA6}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>AsyncFormUpdates</RootNamespace>
|
||||
<AssemblyName>AsyncFormUpdates</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Telegram.Bot, Version=15.7.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Telegram.Bot.15.7.1\lib\net45\Telegram.Bot.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="forms\AsyncFormEdit.cs" />
|
||||
<Compile Include="forms\AsyncFormUpdate.cs" />
|
||||
<Compile Include="forms\Start.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj">
|
||||
<Project>{0bd16fb9-7ed4-4ccb-83eb-5cee538e1b6c}</Project>
|
||||
<Name>TelegramBotBase</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
|
||||
namespace AsyncFormUpdates
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static TelegramBotBase.BotBase<forms.Start> bot = null;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
String apiKey = "APIKey";
|
||||
|
||||
bot = new TelegramBotBase.BotBase<forms.Start>(apiKey);
|
||||
|
||||
bot.Start();
|
||||
|
||||
var timer = new Timer(5000);
|
||||
|
||||
timer.Elapsed += Timer_Elapsed;
|
||||
timer.Start();
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
timer.Stop();
|
||||
bot.Stop();
|
||||
}
|
||||
|
||||
private static async void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
|
||||
foreach(var s in bot.Sessions.SessionList)
|
||||
{
|
||||
//Only for AsyncUpdateForm
|
||||
if (s.Value.ActiveForm.GetType() != typeof(forms.AsyncFormUpdate) && s.Value.ActiveForm.GetType() != typeof(forms.AsyncFormEdit))
|
||||
continue;
|
||||
|
||||
await bot.InvokeMessageLoop(s.Key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die einer Assembly zugeordnet sind.
|
||||
[assembly: AssemblyTitle("AsyncFormUpdates")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("AsyncFormUpdates")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2021")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("673a56f5-6110-4aed-a68d-562fd6ed3ea6")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||
// indem Sie "*" wie unten gezeigt eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Attributes;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace AsyncFormUpdates.forms
|
||||
{
|
||||
public class AsyncFormEdit : FormBase
|
||||
{
|
||||
[SaveState]
|
||||
int counter = 0;
|
||||
|
||||
int MessageId = 0;
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
await message.ConfirmAction("");
|
||||
|
||||
switch (message.RawData ?? "")
|
||||
{
|
||||
case "back":
|
||||
|
||||
var st = new Start();
|
||||
await NavigateTo(st);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
var bf = new ButtonForm();
|
||||
bf.AddButtonRow("Back", "back");
|
||||
|
||||
if (MessageId != 0)
|
||||
{
|
||||
await Device.Edit(MessageId, $"Your current count is at: {counter}", bf);
|
||||
}
|
||||
else
|
||||
{
|
||||
var m = await Device.Send($"Your current count is at: {counter}", bf, disableNotification: true);
|
||||
MessageId = m.MessageId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Attributes;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace AsyncFormUpdates.forms
|
||||
{
|
||||
public class AsyncFormUpdate : AutoCleanForm
|
||||
{
|
||||
[SaveState]
|
||||
int counter = 0;
|
||||
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
await message.ConfirmAction("");
|
||||
|
||||
switch (message.RawData ?? "")
|
||||
{
|
||||
case "back":
|
||||
|
||||
var st = new Start();
|
||||
await NavigateTo(st);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
var bf = new ButtonForm();
|
||||
bf.AddButtonRow("Back", "back");
|
||||
|
||||
await Device.Send($"Your current count is at: {counter}", bf, disableNotification: true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace AsyncFormUpdates.forms
|
||||
{
|
||||
public class Start : AutoCleanForm
|
||||
{
|
||||
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
await message.ConfirmAction("");
|
||||
|
||||
switch (message.RawData ?? "")
|
||||
{
|
||||
case "async":
|
||||
|
||||
var afe = new AsyncFormEdit();
|
||||
await NavigateTo(afe);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "async_del":
|
||||
|
||||
var afu = new AsyncFormUpdate();
|
||||
await NavigateTo(afu);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
var bf = new ButtonForm();
|
||||
|
||||
bf.AddButtonRow("Open Async Form with AutoCleanupForm", "async_del");
|
||||
|
||||
bf.AddButtonRow("Open Async Form with Edit", "async");
|
||||
|
||||
await Device.Send("Choose your option", bf);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net472" />
|
||||
<package id="System.Net.Requests" version="4.3.0" targetFramework="net472" />
|
||||
<package id="Telegram.Bot" version="15.7.1" targetFramework="net472" />
|
||||
</packages>
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TelegramBotBase" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace JoinHiderBot
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
String apiKey = "";
|
||||
|
||||
var bot = new TelegramBotBase.BotBase<forms.Start>(apiKey);
|
||||
|
||||
bot.Start();
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace JoinHiderBot.forms
|
||||
{
|
||||
public class GroupManageForm : GroupForm
|
||||
{
|
||||
|
||||
public override async Task OnMemberChanges(MemberChangeEventArgs e)
|
||||
{
|
||||
if (e.Type != Telegram.Bot.Types.Enums.MessageType.ChatMembersAdded && e.Type != Telegram.Bot.Types.Enums.MessageType.ChatMemberLeft)
|
||||
return;
|
||||
|
||||
|
||||
var m = e.Result.Message;
|
||||
|
||||
await this.Device.DeleteMessage(m);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace JoinHiderBot.forms
|
||||
{
|
||||
public class Start : SplitterForm
|
||||
{
|
||||
public override async Task<bool> Open(MessageResult e)
|
||||
{
|
||||
await this.Device.Send("This bot works only in groups.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override async Task<bool> OpenGroup(MessageResult e)
|
||||
{
|
||||
var gmf = new GroupManageForm();
|
||||
|
||||
await this.NavigateTo(gmf);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
|
||||
namespace SystemCommandsBot
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static config.Config BotConfig { get; set; }
|
||||
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
BotConfig = config.Config.load();
|
||||
|
||||
if (BotConfig.ApiKey == null || BotConfig.ApiKey.Trim() == "")
|
||||
{
|
||||
Console.WriteLine("Config created...");
|
||||
Console.ReadLine();
|
||||
return;
|
||||
}
|
||||
|
||||
var bot = new TelegramBotBase.BotBase<forms.StartForm>(BotConfig.ApiKey);
|
||||
|
||||
bot.Start();
|
||||
|
||||
Console.WriteLine("Bot started");
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
|
||||
bot.Stop();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
<PackageReference Include="TelegramBotBase" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -19,15 +19,17 @@ Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramewor
|
||||
|
||||
Donations
|
||||
|
||||
Bitcoin: 1NqyGWmZg8HLp9qQELbf6bChEoba3mxaFc
|
||||
Bitcoin: 1GoUJYMwAvBipQTfw2FKydAz12J8RDyeJs / bc1qqwlp0p5ley29lsu6jhe0qv7s7963kfc7d0m53d
|
||||
|
||||
ETH: 0x795B70CFC27C69603ce115F2b450cbAC5a5460D0
|
||||
ETH: 0xAf3835104c2C3E5b3e721FA2c7365955e87DB931
|
||||
|
||||
Litecoin: LM5iCN6Nz22wAi8LSFnKgdGGWEtxWfJZXv
|
||||
Litecoin: LRhF1eB7kneFontcDRDU8YjJhEm2GoYHch
|
||||
|
||||
DASH: Xb2qyVefvbKTXusHoxZ4Soja2S6R4vLsSr
|
||||
DASH: XudiUwWtSmAJj1QDdVW7jocQumJFLsyoGZ
|
||||
|
||||
Paypal: https://paypal.me/majmccloud
|
||||
TRON: TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW
|
||||
|
||||
BITTORRENT: TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW
|
||||
|
||||
|
||||
Thanks !
|
||||
@@ -37,6 +39,7 @@ Thanks !
|
||||
## Index
|
||||
- [Introduction](#introduction)
|
||||
- [How to Start](#how-to-start)
|
||||
- [Quick Start](#quick-start)
|
||||
- [Message Handling](#message-handling)
|
||||
* [Example #0 - System Calls](#add-some-system-calls-example-0---system-calls)
|
||||
|
||||
@@ -70,16 +73,53 @@ Thanks !
|
||||
* [ToggleButton](#toggle-button)
|
||||
|
||||
* [ButtonGrid](#button-grid)
|
||||
|
||||
* [Paging and Searching](#paging--searching)
|
||||
|
||||
* [TaggedButtonGrid](#tagged-button-grid)
|
||||
|
||||
* [CheckedButtonList](#checked-button-list)
|
||||
|
||||
* [MultiToggleButton](#multi-toggle-button)
|
||||
|
||||
- [Groups](#groups)
|
||||
* [SplitterForm](#splitter-form)
|
||||
|
||||
* [GroupForm](#group-form)
|
||||
* [GroupForm](#group-form)
|
||||
|
||||
- [State Machine and Session Serialization](#statemachine)
|
||||
- [State Machine and Session Serialization (v3.0.0)](#statemachine-and-sessions)
|
||||
|
||||
* In the making, is comming soon.
|
||||
* [StateMachines](#statemachines)
|
||||
|
||||
* [SimpleJSONStateMachine](#simplejsonstatemachine)
|
||||
|
||||
* [JSONStateMachine](#jsonstatemachine)
|
||||
|
||||
* [XMLStateMachine](#xmlstatemachine)
|
||||
|
||||
* [Interfaces](#interfaces)
|
||||
|
||||
* [IStateMachine](#istatemachine)
|
||||
|
||||
* [IStateForm](#istateform)
|
||||
|
||||
* [Attributes](#attributes)
|
||||
|
||||
* [SaveState](#savestate)
|
||||
|
||||
* [IgnoreState](#ignorestate)
|
||||
|
||||
|
||||
- [Navigation and NavigationController (v4.0.0)](#navigation-and-navigationcontroller)
|
||||
|
||||
* [As of Now](#as-of-now)
|
||||
|
||||
* [How to use](#how-to-use-)
|
||||
|
||||
|
||||
|
||||
|
||||
- [Examples](#examples)
|
||||
|
||||
---
|
||||
|
||||
@@ -99,18 +139,31 @@ 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.
|
||||
The "BotBase" Class will manage a lot of things for you, like bot commands, 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.Base
|
||||
|
||||
|
||||
```
|
||||
|
||||
//Prepare the System
|
||||
BotBase<StartForm> bb = new BotBase<StartForm>("{YOUR API KEY}");
|
||||
//Prepare the System (New in V5)
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey("{YOUR API KEY}")
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<StartForm>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot")
|
||||
|
||||
//Add Systemcommands if you like, you could catch them later
|
||||
bb.SystemCalls.Add("/start");
|
||||
})
|
||||
.NoSerialization()
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
//Update bot commands to botfather
|
||||
bb.UploadBotCommands().Wait();
|
||||
|
||||
//Start your Bot
|
||||
bb.Start();
|
||||
@@ -129,29 +182,26 @@ 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 during Navigation to this form
|
||||
|
||||
}
|
||||
//Init() got replaced with event handler
|
||||
|
||||
//Gets invoked if the form gets leaved
|
||||
public override async Task Closed()
|
||||
{
|
||||
//Opened() got replaced with event handler
|
||||
|
||||
//Closed() got replaced with event handler
|
||||
|
||||
}
|
||||
|
||||
//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 edited messages
|
||||
public override async Task Edited(MessageResult message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//Gets invoked on Button clicks
|
||||
public override async Task Action(MessageResult message)
|
||||
@@ -192,14 +242,36 @@ var tf = new TestForm();
|
||||
await this.NavigateTo(tf);
|
||||
```
|
||||
|
||||
## Quick Start:
|
||||
|
||||
|
||||
When migrating from a previous version or starting completely new, all these options can be a bit overwhelming.
|
||||
For this I added a QuickStart option, directly after the Create call. It just need basic parameters like in earlier versions.
|
||||
|
||||
|
||||
```
|
||||
|
||||
//Prepare the System (New in V5)
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.QuickStart<StartForm>("{YOUR API KEY}")
|
||||
.Build();
|
||||
|
||||
//Start your Bot
|
||||
bb.Start();
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Message Handling
|
||||
|
||||
All examples are within the test project, so just try it out on your own.
|
||||
|
||||
### Add some system calls (Example #0 - System Calls)
|
||||
### Add some system calls (Example #0 - Bot Commands)
|
||||
|
||||
Inside of the BotFather you are able to add "Commands" to your TelegramBot. The user will see them, depending on the application as options he could choose.
|
||||
I'm calling them Systemcalls. Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them.
|
||||
Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them.
|
||||
If so, a special event Handler will get raised where you are easier able to manage the action behind.
|
||||
|
||||
Below we have 4 options.
|
||||
@@ -215,15 +287,26 @@ Below we have 4 options.
|
||||
|
||||
|
||||
```
|
||||
BotBase<Start> bb = new BotBase<Start>("{YOUR API KEY}");
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey("{YOUR API KEY}")
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<Start>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot");
|
||||
a.Add("form1","Opens test form 1" );
|
||||
a.Add("form2", "Opens test form 2" );
|
||||
a.Add("params", "Returns all send parameters as a message." );
|
||||
|
||||
bb.SystemCalls.Add("/start");
|
||||
bb.SystemCalls.Add("/form1");
|
||||
bb.SystemCalls.Add("/form2");
|
||||
|
||||
bb.SystemCalls.Add("/params");
|
||||
})
|
||||
.NoSerialization()
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
bb.SystemCall += async (s, en) =>
|
||||
bb.BotCommand += async (s, en) =>
|
||||
{
|
||||
switch (en.Command)
|
||||
{
|
||||
@@ -254,6 +337,9 @@ bb.SystemCall += async (s, en) =>
|
||||
|
||||
};
|
||||
|
||||
//Update Bot commands to botfather
|
||||
bb.UploadBotCommands().Wait();
|
||||
|
||||
bb.Start();
|
||||
|
||||
```
|
||||
@@ -268,19 +354,21 @@ On every input the user is sending back to the bot the Action event gets raised.
|
||||
```
|
||||
public class SimpleForm : AutoCleanForm
|
||||
{
|
||||
public SimpleForm()
|
||||
{
|
||||
this.DeleteSide = eSide.Both;
|
||||
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
||||
}
|
||||
public SimpleForm()
|
||||
{
|
||||
this.DeleteSide = TelegramBotBase.Enums.eDeleteSide.Both;
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
|
||||
this.Opened += SimpleForm_Opened;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
private async Task SimpleForm_Opened(object sender, EventArgs e)
|
||||
{
|
||||
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)
|
||||
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
|
||||
|
||||
@@ -655,7 +743,7 @@ public async override Task Render(MessageResult message)
|
||||
```
|
||||
|
||||
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)
|
||||
Cause its to much Text, i didnt have added it here. You will find it under [TelegramBotBaseTest/Tests/Register/PerStep.cs](TelegramBotBaseTest/Tests/Register/PerStep.cs)
|
||||
Beginn there and navigate your way through these Steps in the subfolder.
|
||||
|
||||
|
||||
@@ -774,6 +862,17 @@ await this.NavigateTo(cd);
|
||||
### Button Grid
|
||||
<img src="images/buttongrid.gif?raw=true" />
|
||||
|
||||
#### Paging & Searching
|
||||
<img src="images/buttongrid_pagingfilter.gif?raw=true" />
|
||||
|
||||
### Tagged Button Grid
|
||||
<img src="images/taggedbuttongrid.gif?raw=true" />
|
||||
|
||||
### Checked Button List
|
||||
<img src="images/checkedbuttonlist.gif?raw=true" />
|
||||
|
||||
### Multi Toggle Button
|
||||
<img src="images/multitogglebutton.gif?raw=true" />
|
||||
|
||||
## Groups
|
||||
|
||||
@@ -856,6 +955,7 @@ public class GroupForm : FormBase
|
||||
case Telegram.Bot.Types.Enums.MessageType.MessagePinned:
|
||||
case Telegram.Bot.Types.Enums.MessageType.GroupCreated:
|
||||
case Telegram.Bot.Types.Enums.MessageType.SupergroupCreated:
|
||||
case Telegram.Bot.Types.Enums.MessageType.ChannelCreated:
|
||||
|
||||
await OnGroupChanged(new GroupChangedEventArgs(message.MessageType, message));
|
||||
|
||||
@@ -891,8 +991,252 @@ public class GroupForm : FormBase
|
||||
```
|
||||
|
||||
|
||||
## Statemachine and Sessions
|
||||
|
||||
Depending on the usecases and the overall structure of a Telegram Bot it is essential to have some kind of session serialization or state machine to keep the user context after restarts of the bot (ie. due to updates) or crashes.
|
||||
For this I have created some easy to implement structures which fits into the current environment.
|
||||
|
||||
Below you find all possiblities.
|
||||
|
||||
### Statemachines
|
||||
|
||||
There are actually 3 types of example state machines you could use. A state machine is a kind of serializer which saves the important session data in a reusable structure like JSON or XML.
|
||||
|
||||
You could use one of the following state machines:
|
||||
|
||||
#### SimpleJSONStateMachine
|
||||
Is easy to use and useful for simple structures like basic datatypes. Did not work for complex ones like generics. Use the JSONStateMachine for them.
|
||||
In general you didn't need to do more then, to keep the actual form:
|
||||
|
||||
```
|
||||
//Prepare the System
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey("{YOUR API KEY}")
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<StartForm>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot");
|
||||
})
|
||||
.UseSimpleJSON(AppContext.BaseDirectory + "config\\states.json")
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
//Start your Bot
|
||||
bb.Start();
|
||||
|
||||
```
|
||||
|
||||
#### JSONStateMachine
|
||||
Is easy to use too, but works for complex datatypes cause it saves there namespaces and additional type informations into the JSON file too.
|
||||
In general you didn't need to do more then, to keep the actual form:
|
||||
|
||||
```
|
||||
//Prepare the System
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey("{YOUR API KEY}")
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<StartForm>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot");
|
||||
})
|
||||
.UseJSON(AppContext.BaseDirectory + "config\\states.json")
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
//Start your Bot
|
||||
bb.Start();
|
||||
|
||||
```
|
||||
|
||||
#### XMLStateMachine
|
||||
|
||||
The last one, should work like the others.
|
||||
In general you didn't need to do more then, to keep the actual form:
|
||||
|
||||
```
|
||||
//Prepare the System
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey("{YOUR API KEY}")
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<StartForm>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot");
|
||||
})
|
||||
.UseXML(AppContext.BaseDirectory + "config\\states.xml")
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
//Start your Bot
|
||||
bb.Start();
|
||||
|
||||
```
|
||||
|
||||
### Interfaces
|
||||
|
||||
There are two interfaces, one for the StateMachine itself, which is useful to build a custom one for a different datatype and one for implementing into a form which should be invoked with events.
|
||||
|
||||
#### IStateMachine
|
||||
|
||||
Is the basic StateMachine interface, it has two methods SaveFormStates(SaveStatesEventArgs e) and StateContainer LoadFormStates(), nothing fancy, just simple calls. Implement into both methods your own serialization process.
|
||||
|
||||
```
|
||||
|
||||
public interface IStateMachine
|
||||
{
|
||||
void SaveFormStates(SaveStatesEventArgs e);
|
||||
|
||||
StateContainer LoadFormStates();
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### IStateForm
|
||||
|
||||
When implemented, this will invoke one of these two methods: LoadState(LoadStateEventArgs e) or SaveState(SaveStateEventArgs e).
|
||||
They have methods to load or save data from the statemachine of the current form.
|
||||
|
||||
```
|
||||
|
||||
public interface IStateForm
|
||||
{
|
||||
void LoadState(LoadStateEventArgs e);
|
||||
|
||||
void SaveState(SaveStateEventArgs e);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Attributes
|
||||
|
||||
If you don't want to implement the IStateForm interface, cause there are maybe "just" one or two properties you want to keep and restore, use the following attribute:
|
||||
|
||||
#### SaveState
|
||||
|
||||
This will let the engine know, that you want too keep and restore this field automatically. Unlike the IStateForm methods, you have no option to manipulate data.
|
||||
|
||||
```
|
||||
|
||||
[SaveState]
|
||||
public long UserId { get; set; }
|
||||
|
||||
|
||||
```
|
||||
|
||||
#### IgnoreState
|
||||
|
||||
Due to the fact that Attribute implementation and interaction is optional, you want to let the engine maybe know, that you don't want to keep a specific form. So it should get "lost". This attribute will help you here, add it to the form class and it will not get serialized, even if it implements IStateForm or the SaveState attributes.
|
||||
|
||||
```
|
||||
|
||||
[IgnoreState]
|
||||
public class Registration : STForm
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Navigation and NavigationController
|
||||
|
||||
### As of now
|
||||
|
||||
As from earlier topics on this readme you already know the default way for (cross) navigation between Forms.
|
||||
It will look something like this:
|
||||
|
||||
```
|
||||
|
||||
var f = new FormBase();
|
||||
await this.NavigateTo(f);
|
||||
|
||||
```
|
||||
|
||||
Depending on the model and structure of your bot it can make sense, to have more linear navigation instead of "cross" navigation.
|
||||
|
||||
In example you have a bot which shows a list of football teams. And when clicking on it you want to open the team details and latest matches.
|
||||
|
||||
After the matches you want to maybe switch to a different teams and take a look at their statistics and matches.
|
||||
|
||||
At some point, you "just" want to get back to the first team so like on Android your clicking the "back" button multiple times.
|
||||
|
||||
This can become really complicated, when not having some controller below which handle these "Push/Pop" calls.
|
||||
|
||||
Thats why I introduced a NavigationController class which manages these situations and the stack.
|
||||
|
||||
|
||||
### How to use ?
|
||||
|
||||
First you need to create a NavigationController instance at the same position in code, where you want to start the navigation.
|
||||
|
||||
You will use the current FormBase instance as a root class within the constructor. So you can later come back to this one.
|
||||
|
||||
**Tip**: *You can add also a completely new instance of i.e. a main menu form here to get back to it then. So you are free to choose.*
|
||||
|
||||
We are using the same FormBase instance as above.
|
||||
|
||||
|
||||
```
|
||||
|
||||
var nc = new NavigationController(this);
|
||||
|
||||
var f = new FormBase();
|
||||
|
||||
//Replace the current form in the context with the controller.
|
||||
await this.NavigateTo(nc);
|
||||
|
||||
//Push the new from onto the stack to render it
|
||||
nc.PushAsync(f);
|
||||
|
||||
```
|
||||
|
||||
|
||||
Later to open a new form use PushAsync again:
|
||||
|
||||
```
|
||||
|
||||
await this.NavigationController.PushAsync(newForm);
|
||||
|
||||
```
|
||||
|
||||
When you want to go back one Form on the stack use PopAsync:
|
||||
|
||||
|
||||
```
|
||||
|
||||
await this.NavigationController.PopAsync();
|
||||
|
||||
```
|
||||
|
||||
**Notice**: *By default the NavigationController has ForceCleanupOnLastPop enabled, which means that when the stack is again at 1 (due to PopAsync or PopToRootAsync calls) it will replace the controller automatically with the root form you have given to the constructor at the beginning.*
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
You find all example projects in the "Examples" subfolder.
|
||||
|
||||
- [Examples/SystemCommandsBot/](Examples/SystemCommandsBot/)
|
||||
|
||||
Will allow you to run specific system commands or run/kill processes via Bot. Has also a simple authentication mechanism with one password.
|
||||
|
||||
- [Examples/JoinHiderBot/](Examples/JoinHiderBot/)
|
||||
|
||||
Will delete Join and Leave messages automatically in groups.
|
||||
|
||||
- [Examples/AsyncFormUpdates/](Examples/AsyncFormUpdates/)
|
||||
|
||||
When you want to update forms async without any user interaction (message/action) before. Use the new InvokeMessageLoop method of BotBase.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types.InputFiles;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Sessions;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Extensions.Images
|
||||
{
|
||||
public static class ImageExtensions
|
||||
{
|
||||
public static Stream ToStream(this Image image, ImageFormat format)
|
||||
{
|
||||
var stream = new System.IO.MemoryStream();
|
||||
image.Save(stream, format);
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an image
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="buttons"></param>
|
||||
/// <param name="replyTo"></param>
|
||||
/// <param name="disableNotification"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Message> SendPhoto(this DeviceSession session, Image image, String name, String caption, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
|
||||
{
|
||||
using (var fileStream = ToStream(image, ImageFormat.Png))
|
||||
{
|
||||
InputOnlineFile fts = new InputOnlineFile(fileStream, name);
|
||||
|
||||
return await session.SendPhoto(fts, caption: caption, buttons, replyTo, disableNotification);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends an image
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="buttons"></param>
|
||||
/// <param name="replyTo"></param>
|
||||
/// <param name="disableNotification"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Message> SendPhoto(this DeviceSession session, Bitmap image, String name, String caption, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
|
||||
{
|
||||
using (var fileStream = ToStream(image, ImageFormat.Png))
|
||||
{
|
||||
InputOnlineFile fts = new InputOnlineFile(fileStream, name);
|
||||
|
||||
return await session.SendPhoto(fts, caption: caption, buttons, replyTo, disableNotification);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
# TelegramBotBase.Extensions.Images
|
||||
|
||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
|
||||
[](https://www.t.me/tgbotbase)
|
||||
|
||||
|
||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images)
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net5;netcoreapp3.1;net6</TargetFrameworks>
|
||||
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework</RepositoryUrl>
|
||||
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework</PackageProjectUrl>
|
||||
<Copyright>MIT</Copyright>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
|
||||
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBaseTest.Tests;
|
||||
using TelegramBotBase.Commands;
|
||||
using TelegramBotBase.Builder;
|
||||
|
||||
namespace TelegramBotBaseTest
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
String APIKey = "";
|
||||
|
||||
var bb = BotBaseBuilder
|
||||
.Create()
|
||||
.WithAPIKey(APIKey)
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<Start>()
|
||||
.NoProxy()
|
||||
.CustomCommands(a =>
|
||||
{
|
||||
a.Start("Starts the bot");
|
||||
a.Help("Should show you some help");
|
||||
a.Settings("Should show you some settings");
|
||||
a.Add("form1", "Opens test form 1");
|
||||
a.Add("form2", "Opens test form 2");
|
||||
a.Add("params", "Returns all send parameters as a message.");
|
||||
})
|
||||
.NoSerialization()
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
|
||||
|
||||
bb.BotCommand += async (s, en) =>
|
||||
{
|
||||
switch (en.Command)
|
||||
{
|
||||
case "/start":
|
||||
|
||||
var start = new Menu();
|
||||
|
||||
await en.Device.ActiveForm.NavigateTo(start);
|
||||
|
||||
break;
|
||||
case "/form1":
|
||||
|
||||
var form1 = new TestForm();
|
||||
|
||||
await en.Device.ActiveForm.NavigateTo(form1);
|
||||
|
||||
break;
|
||||
|
||||
case "/form2":
|
||||
|
||||
var form2 = new TestForm2();
|
||||
|
||||
await en.Device.ActiveForm.NavigateTo(form2);
|
||||
|
||||
break;
|
||||
|
||||
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);
|
||||
|
||||
en.Handled = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//Update Bot commands to botfather
|
||||
bb.UploadBotCommands().Wait();
|
||||
|
||||
bb.SetSetting(TelegramBotBase.Enums.eSettings.LogAllMessages, true);
|
||||
|
||||
bb.Message += (s, en) =>
|
||||
{
|
||||
Console.WriteLine(en.DeviceId + " " + en.Message.MessageText + " " + (en.Message.RawData ?? ""));
|
||||
};
|
||||
|
||||
bb.Start();
|
||||
|
||||
|
||||
|
||||
Console.WriteLine("Telegram Bot started...");
|
||||
|
||||
Console.WriteLine("Press q to quit application.");
|
||||
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
bb.Stop();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworks>netcoreapp3.0;net461</TargetFrameworks>
|
||||
<TargetFrameworks>netcoreapp3.1;net5;net6</TargetFrameworks>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
</PropertyGroup>
|
||||
@@ -27,6 +27,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TelegramBotBase.Extensions.Images\TelegramBotBase.Extensions.Images.csproj" />
|
||||
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
@@ -44,7 +45,7 @@ namespace TelegramBotBaseTest.Tests.Controls
|
||||
|
||||
}
|
||||
|
||||
private async void Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
|
||||
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
|
||||
{
|
||||
if (e.Button == null)
|
||||
return;
|
||||
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
public class ButtonGridPagingForm : AutoCleanForm
|
||||
{
|
||||
|
||||
ButtonGrid m_Buttons = null;
|
||||
|
||||
public ButtonGridPagingForm()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
|
||||
this.Init += ButtonGridForm_Init;
|
||||
}
|
||||
|
||||
private async Task ButtonGridForm_Init(object sender, InitEventArgs e)
|
||||
{
|
||||
m_Buttons = new ButtonGrid();
|
||||
|
||||
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
|
||||
|
||||
m_Buttons.EnablePaging = true;
|
||||
m_Buttons.EnableSearch = true;
|
||||
|
||||
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back") };
|
||||
|
||||
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
|
||||
foreach (var c in countries)
|
||||
{
|
||||
bf.AddButtonRow(new ButtonBase(c.EnglishName, c.EnglishName));
|
||||
}
|
||||
|
||||
m_Buttons.ButtonsForm = bf;
|
||||
|
||||
m_Buttons.ButtonClicked += Bg_ButtonClicked;
|
||||
|
||||
this.AddControl(m_Buttons);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
|
||||
{
|
||||
if (e.Button == null)
|
||||
return;
|
||||
|
||||
if (e.Button.Value == "back")
|
||||
{
|
||||
var start = new Menu();
|
||||
await this.NavigateTo(start);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
await this.Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
public class ButtonGridTagForm : AutoCleanForm
|
||||
{
|
||||
|
||||
TaggedButtonGrid m_Buttons = null;
|
||||
|
||||
public ButtonGridTagForm()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
|
||||
this.Init += ButtonGridTagForm_Init;
|
||||
}
|
||||
|
||||
private async Task ButtonGridTagForm_Init(object sender, InitEventArgs e)
|
||||
{
|
||||
m_Buttons = new TaggedButtonGrid();
|
||||
|
||||
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
|
||||
|
||||
m_Buttons.EnablePaging = true;
|
||||
|
||||
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back") };
|
||||
|
||||
|
||||
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
|
||||
foreach (var c in countries)
|
||||
{
|
||||
bf.AddButtonRow(new TagButtonBase(c.EnglishName, c.EnglishName, c.Parent.EnglishName));
|
||||
}
|
||||
|
||||
m_Buttons.Tags = countries.Select(a => a.Parent.EnglishName).Distinct().OrderBy(a => a).ToList();
|
||||
m_Buttons.SelectedTags = countries.Select(a => a.Parent.EnglishName).Distinct().OrderBy(a => a).ToList();
|
||||
|
||||
m_Buttons.EnableCheckAllTools = true;
|
||||
|
||||
m_Buttons.DataSource = new TelegramBotBase.Datasources.ButtonFormDataSource(bf);
|
||||
|
||||
m_Buttons.ButtonClicked += Bg_ButtonClicked;
|
||||
|
||||
this.AddControl(m_Buttons);
|
||||
}
|
||||
|
||||
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
|
||||
{
|
||||
if (e.Button == null)
|
||||
return;
|
||||
|
||||
switch (e.Button.Value)
|
||||
{
|
||||
|
||||
case "back":
|
||||
var start = new Menu();
|
||||
await this.NavigateTo(start);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
await this.Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -8,6 +8,7 @@ using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
public class CheckedButtonListForm : AutoCleanForm
|
||||
{
|
||||
|
||||
CheckedButtonList m_Buttons = null;
|
||||
|
||||
public CheckedButtonListForm()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
|
||||
this.Init += CheckedButtonListForm_Init;
|
||||
}
|
||||
|
||||
private async Task CheckedButtonListForm_Init(object sender, InitEventArgs e)
|
||||
{
|
||||
m_Buttons = new CheckedButtonList();
|
||||
|
||||
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
|
||||
m_Buttons.EnablePaging = true;
|
||||
|
||||
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back"), new ButtonBase("Switch Keyboard", "switch") };
|
||||
|
||||
m_Buttons.SubHeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("No checked items", "$") };
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
bf.AddButtonRow($"{i + 1}. Item", i.ToString());
|
||||
}
|
||||
|
||||
m_Buttons.DataSource = new TelegramBotBase.Datasources.ButtonFormDataSource(bf);
|
||||
|
||||
m_Buttons.ButtonClicked += Bg_ButtonClicked;
|
||||
m_Buttons.CheckedChanged += M_Buttons_CheckedChanged;
|
||||
|
||||
this.AddControl(m_Buttons);
|
||||
}
|
||||
|
||||
private async Task M_Buttons_CheckedChanged(object sender, CheckedChangedEventArgs e)
|
||||
{
|
||||
m_Buttons.SubHeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase($"{m_Buttons.CheckedItems.Count} checked items", "$") };
|
||||
}
|
||||
|
||||
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
|
||||
{
|
||||
if (e.Button == null)
|
||||
return;
|
||||
|
||||
switch (e.Button.Value)
|
||||
{
|
||||
case "back":
|
||||
|
||||
var start = new Menu();
|
||||
await NavigateTo(start);
|
||||
break;
|
||||
|
||||
|
||||
case "switch":
|
||||
switch (m_Buttons.KeyboardType)
|
||||
{
|
||||
case TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard:
|
||||
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
|
||||
break;
|
||||
case TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard:
|
||||
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
await Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -8,6 +8,7 @@ using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
public class MultiToggleButtons : AutoCleanForm
|
||||
{
|
||||
public MultiToggleButtons()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
|
||||
this.Init += ToggleButtons_Init;
|
||||
}
|
||||
|
||||
private async Task ToggleButtons_Init(object sender, InitEventArgs e)
|
||||
{
|
||||
|
||||
var mtb = new MultiToggleButton();
|
||||
|
||||
mtb.Options = new List<ButtonBase>() { new ButtonBase("Option 1", "1"), new ButtonBase("Option 2", "2"), new ButtonBase("Option 3", "3") };
|
||||
mtb.SelectedOption = mtb.Options.FirstOrDefault();
|
||||
mtb.Toggled += Tb_Toggled;
|
||||
this.AddControl(mtb);
|
||||
|
||||
mtb = new MultiToggleButton();
|
||||
|
||||
mtb.Options = new List<ButtonBase>() { new ButtonBase("Option 4", "4"), new ButtonBase("Option 5", "5"), new ButtonBase("Option 6", "6") };
|
||||
mtb.SelectedOption = mtb.Options.FirstOrDefault();
|
||||
mtb.AllowEmptySelection = false;
|
||||
mtb.Toggled += Tb_Toggled;
|
||||
this.AddControl(mtb);
|
||||
}
|
||||
|
||||
private void Tb_Toggled(object sender, EventArgs e)
|
||||
{
|
||||
var tb = sender as MultiToggleButton;
|
||||
if (tb.SelectedOption != null)
|
||||
{
|
||||
Console.WriteLine(tb.ID.ToString() + " was pressed, and toggled to " + tb.SelectedOption.Value);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Selection for " + tb.ID.ToString() + " has been removed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
public class MultiViewForm : AutoCleanForm
|
||||
{
|
||||
|
||||
Subclass.MultiViewTest mvt = null;
|
||||
|
||||
ButtonGrid bg = null;
|
||||
|
||||
public MultiViewForm()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
this.Init += MultiViewForm_Init;
|
||||
}
|
||||
|
||||
private async Task MultiViewForm_Init(object sender, TelegramBotBase.Args.InitEventArgs e)
|
||||
{
|
||||
mvt = new Subclass.MultiViewTest();
|
||||
|
||||
AddControl(mvt);
|
||||
|
||||
bg = new ButtonGrid();
|
||||
bg.ButtonsForm = new ButtonForm();
|
||||
bg.ButtonsForm.AddButtonRow("Back", "$back$");
|
||||
bg.ButtonClicked += Bg_ButtonClicked;
|
||||
bg.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
|
||||
AddControl(bg);
|
||||
}
|
||||
|
||||
private async Task Bg_ButtonClicked(object sender, TelegramBotBase.Args.ButtonClickedEventArgs e)
|
||||
{
|
||||
switch(e.Button.Value)
|
||||
{
|
||||
case "$back$":
|
||||
|
||||
var mn = new Menu();
|
||||
await NavigateTo(mn);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls.Subclass
|
||||
{
|
||||
public class MultiViewTest : TelegramBotBase.Controls.Hybrid.MultiView
|
||||
{
|
||||
|
||||
|
||||
public override async Task Action(MessageResult result, string value = null)
|
||||
{
|
||||
|
||||
switch (result.RawData)
|
||||
{
|
||||
case "back":
|
||||
|
||||
this.SelectedViewIndex--;
|
||||
|
||||
break;
|
||||
case "next":
|
||||
|
||||
this.SelectedViewIndex++;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override async Task RenderView(RenderViewEventArgs e)
|
||||
{
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
bf.AddButtonRow(new ButtonBase("Back", "back"), new ButtonBase("Next", "next"));
|
||||
|
||||
switch (e.CurrentView)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
|
||||
await Device.Send($"Page {e.CurrentView + 1}", bf);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
await Device.Send("Unknown Page", bf);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+1
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
+1
@@ -7,6 +7,7 @@ using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Controls;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Controls
|
||||
{
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.InputFiles;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Datasources;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Datasources
|
||||
{
|
||||
public class CustomDataSource : ButtonFormDataSource
|
||||
{
|
||||
|
||||
public List<String> Countries = new List<string>() { "Country 1", "Country 2", "Country 3" };
|
||||
|
||||
public CustomDataSource()
|
||||
{
|
||||
loadData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method has the example purpose of creating and loading some example data.
|
||||
/// When using a database you do not need this kind of method.
|
||||
/// </summary>
|
||||
private void loadData()
|
||||
{
|
||||
//Exists data source? Read it
|
||||
if (File.Exists(AppContext.BaseDirectory + "countries.json"))
|
||||
{
|
||||
try
|
||||
{
|
||||
var List = Newtonsoft.Json.JsonConvert.DeserializeObject<List<String>>(File.ReadAllText("countries.json"));
|
||||
|
||||
|
||||
Countries = List;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//If not, create it
|
||||
try
|
||||
{
|
||||
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(a => a.DisplayName).ToList();
|
||||
|
||||
Countries = countries;
|
||||
|
||||
var tmp = Newtonsoft.Json.JsonConvert.SerializeObject(countries);
|
||||
|
||||
File.WriteAllText( AppContext.BaseDirectory + "countries.json", tmp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override ButtonRow ItemAt(int index)
|
||||
{
|
||||
var item = Countries.ElementAt(index);
|
||||
if (item == null)
|
||||
return new ButtonRow();
|
||||
|
||||
return Render(item);
|
||||
}
|
||||
|
||||
public override List<ButtonRow> ItemRange(int start, int count)
|
||||
{
|
||||
var items = Countries.Skip(start).Take(count);
|
||||
|
||||
List<ButtonRow> lst = new List<ButtonRow>();
|
||||
foreach (var c in items)
|
||||
{
|
||||
lst.Add(Render(c));
|
||||
}
|
||||
|
||||
return lst;
|
||||
}
|
||||
|
||||
public override List<ButtonRow> AllItems()
|
||||
{
|
||||
List<ButtonRow> lst = new List<ButtonRow>();
|
||||
foreach (var c in Countries)
|
||||
{
|
||||
lst.Add(Render(c));
|
||||
}
|
||||
return lst;
|
||||
}
|
||||
|
||||
public override ButtonForm PickItems(int start, int count, string filter = null)
|
||||
{
|
||||
List<ButtonRow> rows = ItemRange(start, count);
|
||||
|
||||
ButtonForm lst = new ButtonForm();
|
||||
foreach (var c in rows)
|
||||
{
|
||||
lst.AddButtonRow(c);
|
||||
}
|
||||
return lst;
|
||||
}
|
||||
|
||||
public override ButtonForm PickAllItems(string filter = null)
|
||||
{
|
||||
List<ButtonRow> rows = AllItems();
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
|
||||
bf.AddButtonRows(rows);
|
||||
|
||||
return bf;
|
||||
}
|
||||
|
||||
public override int CalculateMax(string filter = null)
|
||||
{
|
||||
if (filter == null)
|
||||
return Countries.Count;
|
||||
|
||||
return Countries.Where(a => a.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) != -1).Count();
|
||||
}
|
||||
|
||||
public override ButtonRow Render(object data)
|
||||
{
|
||||
var s = data as String;
|
||||
if (s == null)
|
||||
return new ButtonRow(new ButtonBase("Empty", "zero"));
|
||||
|
||||
return new ButtonRow(new ButtonBase(s, s));
|
||||
}
|
||||
|
||||
public override int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return Countries.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public override int ColumnCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public override int RowCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Count;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Datasources
|
||||
{
|
||||
public class List : FormBase
|
||||
{
|
||||
ButtonGrid __buttons = null;
|
||||
|
||||
public List()
|
||||
{
|
||||
this.Init += List_Init;
|
||||
}
|
||||
|
||||
private async Task List_Init(object sender, TelegramBotBase.Args.InitEventArgs e)
|
||||
{
|
||||
|
||||
__buttons = new ButtonGrid();
|
||||
|
||||
__buttons.EnablePaging = true;
|
||||
__buttons.EnableSearch = false;
|
||||
__buttons.ButtonClicked += __buttons_ButtonClicked;
|
||||
__buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
|
||||
__buttons.DeleteReplyMessage = true;
|
||||
|
||||
__buttons.HeadLayoutButtonRow = new ButtonRow(new ButtonBase("Back", "back"));
|
||||
|
||||
var cds = new CustomDataSource();
|
||||
__buttons.DataSource = cds;
|
||||
|
||||
AddControl(__buttons);
|
||||
}
|
||||
|
||||
private async Task __buttons_ButtonClicked(object sender, TelegramBotBase.Args.ButtonClickedEventArgs e)
|
||||
{
|
||||
switch(e.Button.Value)
|
||||
{
|
||||
case "back":
|
||||
|
||||
var mn = new Menu();
|
||||
await NavigateTo(mn);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@ namespace TelegramBotBaseTest.Tests.Groups
|
||||
public class LinkReplaceTest : TelegramBotBase.Form.GroupForm
|
||||
{
|
||||
|
||||
Dictionary<int, int> Counter { get; set; } = new Dictionary<int, int>();
|
||||
Dictionary<long, int> Counter { get; set; } = new Dictionary<long, int>();
|
||||
|
||||
private const int Maximum = 3;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBaseTest.Tests.Controls;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests
|
||||
{
|
||||
@@ -27,6 +28,7 @@ namespace TelegramBotBaseTest.Tests
|
||||
}
|
||||
|
||||
|
||||
await Device.HideReplyKeyboard();
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
@@ -39,7 +41,9 @@ namespace TelegramBotBaseTest.Tests
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
switch(call.Value)
|
||||
message.Handled = true;
|
||||
|
||||
switch (call.Value)
|
||||
{
|
||||
case "text":
|
||||
|
||||
@@ -91,8 +95,6 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
case "data":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var data = new DataForm();
|
||||
|
||||
await this.NavigateTo(data);
|
||||
@@ -101,8 +103,6 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
case "calendar":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var calendar = new Controls.CalendarPickerForm();
|
||||
|
||||
await this.NavigateTo(calendar);
|
||||
@@ -111,8 +111,6 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
case "month":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var month = new Controls.MonthPickerForm();
|
||||
|
||||
await this.NavigateTo(month);
|
||||
@@ -121,8 +119,6 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
case "treeview":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var tree = new Controls.TreeViewForms();
|
||||
|
||||
await this.NavigateTo(tree);
|
||||
@@ -131,21 +127,90 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
case "togglebuttons":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var tb = new Controls.ToggleButtons();
|
||||
|
||||
await this.NavigateTo(tb);
|
||||
|
||||
break;
|
||||
case "buttongrid":
|
||||
|
||||
message.Handled = true;
|
||||
case "multitogglebuttons":
|
||||
|
||||
var mtb = new Controls.MultiToggleButtons();
|
||||
|
||||
await this.NavigateTo(mtb);
|
||||
|
||||
break;
|
||||
|
||||
case "buttongrid":
|
||||
|
||||
var bg = new Controls.ButtonGridForm();
|
||||
|
||||
await this.NavigateTo(bg);
|
||||
|
||||
break;
|
||||
|
||||
case "buttongridfilter":
|
||||
|
||||
var bg2 = new Controls.ButtonGridPagingForm();
|
||||
|
||||
await this.NavigateTo(bg2);
|
||||
|
||||
break;
|
||||
|
||||
case "buttongridtags":
|
||||
|
||||
var bg3 = new Controls.ButtonGridTagForm();
|
||||
|
||||
await this.NavigateTo(bg3);
|
||||
|
||||
break;
|
||||
|
||||
case "multiview":
|
||||
|
||||
var mvf = new MultiViewForm();
|
||||
|
||||
await NavigateTo(mvf);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "checkedbuttonlist":
|
||||
|
||||
var cbl = new CheckedButtonListForm();
|
||||
|
||||
await NavigateTo(cbl);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "navigationcontroller":
|
||||
|
||||
var nc = new Navigation.Start();
|
||||
|
||||
await NavigateTo(nc);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "dynamicbuttongrid":
|
||||
|
||||
var dg = new Datasources.List();
|
||||
|
||||
await NavigateTo(dg);
|
||||
|
||||
break;
|
||||
|
||||
case "notifications":
|
||||
|
||||
var not = new Notifications.Start();
|
||||
await NavigateTo(not);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
message.Handled = false;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -173,8 +238,24 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#11 ToggleButtons", new CallbackData("a", "togglebuttons").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#11.2 MultiToggleButtons", new CallbackData("a", "multitogglebuttons").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#12 ButtonGrid", new CallbackData("a", "buttongrid").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#13 ButtonGrid Paging & Filter", new CallbackData("a", "buttongridfilter").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#14 ButtonGrid Tags (Filter)", new CallbackData("a", "buttongridtags").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#15 MultiView", new CallbackData("a", "multiview").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#16 CheckedButtonList", new CallbackData("a", "checkedbuttonlist").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#17 NavigationController (Push/Pop)", new CallbackData("a", "navigationcontroller").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#18 Dynamic ButtonGrid (DataSources)", new CallbackData("a", "dynamicbuttongrid").Serialize()));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("#19 Notifications", new CallbackData("a", "notifications").Serialize()));
|
||||
|
||||
await this.Device.Send("Choose your test:", btn);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Form.Navigation;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Navigation
|
||||
{
|
||||
class CustomController : NavigationController
|
||||
{
|
||||
public CustomController(FormBase form) : base(form)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override Task PushAsync(FormBase form, params object[] args)
|
||||
{
|
||||
Console.WriteLine($"Pushes form (Count on stack {this.Index + 1})");
|
||||
//Device.Send($"Pushes form (Count on stack {this.Index + 1})");
|
||||
|
||||
return base.PushAsync(form, args);
|
||||
}
|
||||
|
||||
|
||||
public override Task PopAsync()
|
||||
{
|
||||
Console.WriteLine($"Pops one form (Count on stack {this.Index + 1})");
|
||||
//Device.Send($"Pops one form (Count on stack {this.Index + 1})");
|
||||
|
||||
return base.PopAsync();
|
||||
}
|
||||
|
||||
public override Task PopToRootAsync()
|
||||
{
|
||||
Console.WriteLine($"Moved back to root (Count on stack {this.Index + 1})");
|
||||
//Device.Send($"Moved back to root (Count on stack {this.Index + 1})");
|
||||
|
||||
return base.PopToRootAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Navigation
|
||||
{
|
||||
public class Form1 : FormBase
|
||||
{
|
||||
Message msg = null;
|
||||
|
||||
public Form1()
|
||||
{
|
||||
this.Closed += Form1_Closed;
|
||||
}
|
||||
|
||||
private async Task Form1_Closed(object sender, EventArgs e)
|
||||
{
|
||||
if (msg == null)
|
||||
return;
|
||||
|
||||
await Device.DeleteMessage(msg);
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
if (message.Handled)
|
||||
return;
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
switch (message.RawData)
|
||||
{
|
||||
case "next":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var f1 = new Form1();
|
||||
|
||||
await NavigationController.PushAsync(f1);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "previous":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
//Pop's the current form and move the previous one. The root form will be the Start class.
|
||||
await NavigationController.PopAsync();
|
||||
|
||||
break;
|
||||
|
||||
case "root":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
await NavigationController.PopToRootAsync();
|
||||
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
if (msg != null)
|
||||
return;
|
||||
|
||||
var bf = new ButtonForm();
|
||||
bf.AddButtonRow("Next page", "next");
|
||||
bf.AddButtonRow("Previous page", "previous");
|
||||
bf.AddButtonRow("Back to root", "root");
|
||||
|
||||
msg = await Device.Send($"Choose your options (Count on stack {NavigationController.Index + 1})", bf);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Form.Navigation;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Navigation
|
||||
{
|
||||
public class Start : FormBase
|
||||
{
|
||||
|
||||
Message msg = null;
|
||||
|
||||
public Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
if (message.Handled)
|
||||
return;
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
switch (message.RawData)
|
||||
{
|
||||
case "yes":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
//Create navigation controller and navigate to it, keep the current form as root form so we can get back to here later
|
||||
var nc = new CustomController(this);
|
||||
nc.ForceCleanupOnLastPop = true;
|
||||
|
||||
var f1 = new Form1();
|
||||
|
||||
await nc.PushAsync(f1);
|
||||
|
||||
await NavigateTo(nc);
|
||||
|
||||
if (msg == null)
|
||||
return;
|
||||
|
||||
await Device.DeleteMessage(msg);
|
||||
|
||||
|
||||
break;
|
||||
case "no":
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
var mn = new Menu();
|
||||
|
||||
await NavigateTo(mn);
|
||||
|
||||
if (msg == null)
|
||||
return;
|
||||
|
||||
await Device.DeleteMessage(msg);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
var bf = new ButtonForm();
|
||||
|
||||
bf.AddButtonRow("Yes", "yes");
|
||||
bf.AddButtonRow("No", "no");
|
||||
|
||||
msg = await Device.Send("Open controller?", bf);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests.Notifications
|
||||
{
|
||||
public class Start : AutoCleanForm
|
||||
{
|
||||
bool sent = false;
|
||||
|
||||
public Start()
|
||||
{
|
||||
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
if (message.Handled)
|
||||
return;
|
||||
|
||||
switch (message.RawData)
|
||||
{
|
||||
case "alert":
|
||||
|
||||
await message.ConfirmAction("This is an alert.", true);
|
||||
|
||||
break;
|
||||
case "back":
|
||||
|
||||
var mn = new Menu();
|
||||
await NavigateTo(mn);
|
||||
|
||||
break;
|
||||
default:
|
||||
|
||||
await message.ConfirmAction("This is feedback");
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
if (sent)
|
||||
return;
|
||||
|
||||
var bf = new ButtonForm();
|
||||
bf.AddButtonRow("Normal feeback", "normal");
|
||||
bf.AddButtonRow("Alert Box", "alert");
|
||||
bf.AddButtonRow("Back", "back");
|
||||
|
||||
await Device.Send("Choose your test", bf);
|
||||
|
||||
sent = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+7
-6
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Controls.Inline;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests
|
||||
@@ -35,41 +36,41 @@ namespace TelegramBotBaseTest.Tests
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
TelegramBotBase.Controls.ProgressBar Bar = null;
|
||||
ProgressBar Bar = null;
|
||||
|
||||
switch (call.Value)
|
||||
{
|
||||
case "standard":
|
||||
|
||||
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.standard);
|
||||
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.standard);
|
||||
Bar.Device = this.Device;
|
||||
|
||||
break;
|
||||
|
||||
case "squares":
|
||||
|
||||
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squares);
|
||||
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.squares);
|
||||
Bar.Device = this.Device;
|
||||
|
||||
break;
|
||||
|
||||
case "circles":
|
||||
|
||||
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.circles);
|
||||
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.circles);
|
||||
Bar.Device = this.Device;
|
||||
|
||||
break;
|
||||
|
||||
case "lines":
|
||||
|
||||
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.lines);
|
||||
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.lines);
|
||||
Bar.Device = this.Device;
|
||||
|
||||
break;
|
||||
|
||||
case "squaredlines":
|
||||
|
||||
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squaredLines);
|
||||
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.squaredLines);
|
||||
Bar.Device = this.Device;
|
||||
|
||||
break;
|
||||
@@ -38,7 +38,6 @@ namespace TelegramBotBaseTest.Tests
|
||||
{
|
||||
case "reply":
|
||||
|
||||
|
||||
|
||||
break;
|
||||
|
||||
@@ -52,10 +51,10 @@ namespace TelegramBotBaseTest.Tests
|
||||
|
||||
default:
|
||||
|
||||
if (message.RawMessageData == null)
|
||||
if (message.UpdateData == null)
|
||||
return;
|
||||
|
||||
this.LastMessage = message.RawMessageData.Message.Text;
|
||||
this.LastMessage = message.Message.Text;
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Extensions.Images;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBaseTest.Tests
|
||||
@@ -31,7 +32,7 @@ namespace TelegramBotBaseTest.Tests
|
||||
{
|
||||
await this.Device.Send("Ciao from Form 2");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -118,7 +119,7 @@ namespace TelegramBotBaseTest.Tests
|
||||
btn.AddButtonRow(new ButtonBase("Request Prompt", CallbackData.Create("navigate", "prompt")));
|
||||
|
||||
|
||||
await this.Device.SendPhoto(bmp, "Test", btn);
|
||||
await this.Device.SendPhoto(bmp, "Test", "", btn);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net452" />
|
||||
<package id="System.Net.Requests" version="4.3.0" targetFramework="net452" />
|
||||
<package id="Telegram.Bot" version="14.12.0" targetFramework="net452" />
|
||||
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net5" />
|
||||
<package id="System.Net.Requests" version="4.3.0" targetFramework="net5" />
|
||||
<package id="Telegram.Bot" version="14.12.0" targetFramework="net5" />
|
||||
</packages>
|
||||
+4
-4
@@ -9,9 +9,9 @@ using TelegramBotBase.Sessions;
|
||||
namespace TelegramBotBase.Args
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for given system call results
|
||||
/// Base class for given bot command results
|
||||
/// </summary>
|
||||
public class SystemCallEventArgs : EventArgs
|
||||
public class BotCommandEventArgs : EventArgs
|
||||
{
|
||||
public String Command { get; set; }
|
||||
|
||||
@@ -26,13 +26,13 @@ namespace TelegramBotBase.Args
|
||||
public Message OriginalMessage { get; set; }
|
||||
|
||||
|
||||
public SystemCallEventArgs()
|
||||
public BotCommandEventArgs()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public SystemCallEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
|
||||
public BotCommandEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
|
||||
{
|
||||
this.Command = Command;
|
||||
this.Parameters = Parameters;
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Args
|
||||
@@ -14,6 +15,12 @@ namespace TelegramBotBase.Args
|
||||
{
|
||||
public ButtonBase Button { get; set; }
|
||||
|
||||
public int Index { get; set; }
|
||||
|
||||
public object Tag { get; set; }
|
||||
|
||||
public ButtonRow Row { get; set; }
|
||||
|
||||
|
||||
public ButtonClickedEventArgs()
|
||||
{
|
||||
@@ -23,7 +30,20 @@ namespace TelegramBotBase.Args
|
||||
public ButtonClickedEventArgs(ButtonBase button)
|
||||
{
|
||||
this.Button = button;
|
||||
this.Index = -1;
|
||||
}
|
||||
|
||||
public ButtonClickedEventArgs(ButtonBase button, int Index)
|
||||
{
|
||||
this.Button = button;
|
||||
this.Index = Index;
|
||||
}
|
||||
|
||||
public ButtonClickedEventArgs(ButtonBase button, int Index, ButtonRow row)
|
||||
{
|
||||
this.Button = button;
|
||||
this.Index = Index;
|
||||
this.Row = row;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Args
|
||||
{
|
||||
public class CheckedChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains the index of the row where the button is inside.
|
||||
/// Contains -1 when it is a layout button or not found.
|
||||
/// </summary>
|
||||
public int Index { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Contains all buttons within this row, excluding the checkbox.
|
||||
/// </summary>
|
||||
public ButtonRow Row { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Contains the new checked status of the row.
|
||||
/// </summary>
|
||||
public bool Checked { get; set; }
|
||||
|
||||
|
||||
public CheckedChangedEventArgs()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public CheckedChangedEventArgs(ButtonRow row, int Index, bool Checked)
|
||||
{
|
||||
this.Row = row;
|
||||
this.Index = Index;
|
||||
this.Checked = Checked;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -54,5 +54,10 @@ namespace TelegramBotBase.Args
|
||||
return false;
|
||||
}
|
||||
|
||||
public object GetObject(String key)
|
||||
{
|
||||
return Values[key];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBotBase.Args
|
||||
{
|
||||
public class MessageDeletedEventArgs
|
||||
{
|
||||
public int MessageId
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public MessageDeletedEventArgs(int messageId)
|
||||
{
|
||||
this.MessageId = messageId;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,16 @@ namespace TelegramBotBase.Args
|
||||
|
||||
public Message Message { get; set; }
|
||||
|
||||
public MessageSentEventArgs(Message message)
|
||||
/// <summary>
|
||||
/// Contains the element, which has called the method.
|
||||
/// </summary>
|
||||
public Type Origin { get; set; }
|
||||
|
||||
|
||||
public MessageSentEventArgs(Message message, Type Origin)
|
||||
{
|
||||
this.Message = message;
|
||||
this.Origin = Origin;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace TelegramBotBase.Args
|
||||
{
|
||||
public class PromptDialogCompletedEventArgs
|
||||
{
|
||||
public object Tag { get; set; }
|
||||
|
||||
public String Value { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace TelegramBotBase.Args
|
||||
{
|
||||
public class RenderViewEventArgs : EventArgs
|
||||
{
|
||||
public int CurrentView { get; set; }
|
||||
|
||||
|
||||
public RenderViewEventArgs(int ViewIndex)
|
||||
{
|
||||
|
||||
CurrentView = ViewIndex;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,10 @@ namespace TelegramBotBase.Args
|
||||
{
|
||||
Values[key] = value;
|
||||
}
|
||||
public void SetObject(String key, object value)
|
||||
{
|
||||
Values[key] = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,19 +28,27 @@ namespace TelegramBotBase.Base
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Get invoked when control will be added to a form and invoked.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual void Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual async Task Load(MessageResult result)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public virtual async Task Action(MessageResult result, String value = null)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -50,18 +58,22 @@ namespace TelegramBotBase.Base
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public virtual async Task Hidden(bool FormClose)
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will be called on a cleanup.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual async Task Cleanup()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.InputFiles;
|
||||
|
||||
@@ -14,7 +15,11 @@ namespace TelegramBotBase.Base
|
||||
/// </summary>
|
||||
public class DataResult : ResultBase
|
||||
{
|
||||
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
|
||||
|
||||
//public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
|
||||
|
||||
public UpdateResult UpdateData { get; set; }
|
||||
|
||||
|
||||
public Contact Contact
|
||||
{
|
||||
@@ -64,11 +69,20 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Telegram.Bot.Types.Enums.MessageType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
|
||||
return this.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
public override Message Message
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.UpdateData?.Message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,29 +100,26 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public DataResult(Telegram.Bot.Args.MessageEventArgs rawdata)
|
||||
public DataResult(UpdateResult update)
|
||||
{
|
||||
this.RawMessageData = rawdata;
|
||||
this.Message = rawdata.Message;
|
||||
this.UpdateData = update;
|
||||
}
|
||||
|
||||
public DataResult(MessageResult message)
|
||||
{
|
||||
this.RawMessageData = message.RawMessageData;
|
||||
this.Message = message.Message;
|
||||
|
||||
this.Client = message.Client;
|
||||
}
|
||||
|
||||
public async Task<InputOnlineFile> DownloadDocument()
|
||||
{
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize);
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize.Value);
|
||||
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent);
|
||||
|
||||
|
||||
return new InputOnlineFile(encryptedContent, this.Document.FileName);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Downloads a file and saves it to the given path.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DownloadDocument(String path)
|
||||
{
|
||||
var file = await this.Client.TelegramClient.GetFileAsync(this.Document.FileId);
|
||||
@@ -118,11 +129,47 @@ namespace TelegramBotBase.Base
|
||||
fs.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads the document and returns an byte array.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<byte[]> DownloadRawDocument()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads a document and returns it as string. (txt,csv,etc) Default encoding ist UTF8.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<String> DownloadRawTextDocument()
|
||||
{
|
||||
return await DownloadRawTextDocument(Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Downloads a document and returns it as string. (txt,csv,etc)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<String> DownloadRawTextDocument(Encoding encoding)
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
var sr = new StreamReader(ms, encoding);
|
||||
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
public async Task<InputOnlineFile> DownloadVideo()
|
||||
{
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize);
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize.Value);
|
||||
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent);
|
||||
|
||||
|
||||
return new InputOnlineFile(encryptedContent, "");
|
||||
}
|
||||
|
||||
@@ -137,7 +184,7 @@ namespace TelegramBotBase.Base
|
||||
|
||||
public async Task<InputOnlineFile> DownloadAudio()
|
||||
{
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Audio.FileSize);
|
||||
var encryptedContent = new System.IO.MemoryStream(this.Audio.FileSize.Value);
|
||||
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Audio.FileId, encryptedContent);
|
||||
|
||||
return new InputOnlineFile(encryptedContent, "");
|
||||
@@ -155,9 +202,9 @@ namespace TelegramBotBase.Base
|
||||
public async Task<InputOnlineFile> DownloadPhoto(int index)
|
||||
{
|
||||
var photo = this.Photos[index];
|
||||
var encryptedContent = new System.IO.MemoryStream(photo.FileSize);
|
||||
var encryptedContent = new System.IO.MemoryStream(photo.FileSize.Value);
|
||||
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent);
|
||||
|
||||
|
||||
return new InputOnlineFile(encryptedContent, "");
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form.Navigation;
|
||||
using TelegramBotBase.Sessions;
|
||||
using static TelegramBotBase.Base.Async;
|
||||
|
||||
@@ -17,6 +18,8 @@ namespace TelegramBotBase.Form
|
||||
public class FormBase : IDisposable
|
||||
{
|
||||
|
||||
public NavigationController NavigationController { get; set; }
|
||||
|
||||
public DeviceSession Device { get; set; }
|
||||
|
||||
public MessageClient Client { get; set; }
|
||||
@@ -55,7 +58,10 @@ namespace TelegramBotBase.Form
|
||||
if (this.Events[__evInit] == null)
|
||||
return;
|
||||
|
||||
var handler = this.Events[__evInit].GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
|
||||
var handler = this.Events[__evInit]?.GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<InitEventArgs>(h, this, e);
|
||||
@@ -84,7 +90,10 @@ namespace TelegramBotBase.Form
|
||||
if (this.Events[__evOpened] == null)
|
||||
return;
|
||||
|
||||
var handler = this.Events[__evOpened].GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
|
||||
var handler = this.Events[__evOpened]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<EventArgs>(h, this, e);
|
||||
@@ -114,7 +123,10 @@ namespace TelegramBotBase.Form
|
||||
if (this.Events[__evClosed] == null)
|
||||
return;
|
||||
|
||||
var handler = this.Events[__evClosed].GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
|
||||
var handler = this.Events[__evClosed]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<EventArgs>(h, this, e);
|
||||
@@ -171,7 +183,7 @@ namespace TelegramBotBase.Form
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task LoadControls(MessageResult message)
|
||||
public virtual async Task LoadControls(MessageResult message)
|
||||
{
|
||||
//Looking for the control by id, if not listened, raise event for all
|
||||
if (message.RawData?.StartsWith("#c") ?? false)
|
||||
@@ -219,7 +231,7 @@ namespace TelegramBotBase.Form
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ActionControls(MessageResult message)
|
||||
public virtual async Task ActionControls(MessageResult message)
|
||||
{
|
||||
//Looking for the control by id, if not listened, raise event for all
|
||||
if (message.RawData.StartsWith("#c"))
|
||||
@@ -269,7 +281,7 @@ namespace TelegramBotBase.Form
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RenderControls(MessageResult message)
|
||||
public virtual async Task RenderControls(MessageResult message)
|
||||
{
|
||||
foreach (var b in this.Controls)
|
||||
{
|
||||
@@ -391,9 +403,15 @@ namespace TelegramBotBase.Form
|
||||
/// <param name="control"></param>
|
||||
public void AddControl(ControlBase control)
|
||||
{
|
||||
//Duplicate check
|
||||
if (this.Controls.Contains(control))
|
||||
throw new ArgumentException("Control has been already added.");
|
||||
|
||||
control.ID = this.Controls.Count + 1;
|
||||
control.Device = this.Device;
|
||||
this.Controls.Add(control);
|
||||
|
||||
control.Init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -410,6 +428,19 @@ namespace TelegramBotBase.Form
|
||||
this.Controls.Remove(control);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all controls.
|
||||
/// </summary>
|
||||
public void RemoveAllControls()
|
||||
{
|
||||
foreach(var c in this.Controls)
|
||||
{
|
||||
c.Cleanup().Wait();
|
||||
|
||||
this.Controls.Remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup
|
||||
/// </summary>
|
||||
|
||||
@@ -5,7 +5,13 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Extensions.Polling;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
@@ -15,18 +21,23 @@ namespace TelegramBotBase.Base
|
||||
public class MessageClient
|
||||
{
|
||||
|
||||
|
||||
public String APIKey { get; set; }
|
||||
|
||||
public Telegram.Bot.TelegramBotClient TelegramClient { get; set; }
|
||||
public ITelegramBotClient TelegramClient { get; set; }
|
||||
|
||||
private EventHandlerList __Events { get; set; } = new EventHandlerList();
|
||||
|
||||
private static object __evOnMessageLoop = new object();
|
||||
|
||||
private static object __evOnMessage = new object();
|
||||
|
||||
private static object __evOnMessageEdit = new object();
|
||||
|
||||
private static object __evCallbackQuery = new object();
|
||||
|
||||
CancellationTokenSource __cancellationTokenSource;
|
||||
|
||||
|
||||
public MessageClient(String APIKey)
|
||||
{
|
||||
@@ -40,18 +51,27 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
this.APIKey = APIKey;
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
|
||||
|
||||
|
||||
|
||||
Prepare();
|
||||
}
|
||||
|
||||
public MessageClient(String APIKey, Uri proxyUrl)
|
||||
|
||||
|
||||
public MessageClient(String APIKey, Uri proxyUrl, NetworkCredential credential = null)
|
||||
{
|
||||
this.APIKey = APIKey;
|
||||
|
||||
var proxy = new WebProxy(proxyUrl);
|
||||
var proxy = new WebProxy(proxyUrl)
|
||||
{
|
||||
Credentials = credential
|
||||
};
|
||||
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
|
||||
var httpClient = new HttpClient(
|
||||
new HttpClientHandler { Proxy = proxy, UseProxy = true }
|
||||
);
|
||||
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
|
||||
|
||||
Prepare();
|
||||
}
|
||||
@@ -68,11 +88,17 @@ namespace TelegramBotBase.Base
|
||||
|
||||
var proxy = new WebProxy(proxyHost, proxyPort);
|
||||
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
|
||||
var httpClient = new HttpClient(
|
||||
new HttpClientHandler { Proxy = proxy, UseProxy = true }
|
||||
);
|
||||
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
|
||||
|
||||
Prepare();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client)
|
||||
{
|
||||
this.APIKey = APIKey;
|
||||
@@ -86,114 +112,90 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
this.TelegramClient.Timeout = new TimeSpan(0, 0, 30);
|
||||
|
||||
this.TelegramClient.OnMessage += TelegramClient_OnMessage;
|
||||
this.TelegramClient.OnMessageEdited += TelegramClient_OnMessageEdited;
|
||||
this.TelegramClient.OnCallbackQuery += TelegramClient_OnCallbackQuery;
|
||||
}
|
||||
|
||||
private async void TelegramClient_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
|
||||
|
||||
|
||||
public void StartReceiving()
|
||||
{
|
||||
//Skip empty messages by default
|
||||
if (e.Message == null)
|
||||
return;
|
||||
__cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
try
|
||||
{
|
||||
var mr = new MessageResult(e);
|
||||
mr.Client = this;
|
||||
OnMessage(mr);
|
||||
}
|
||||
catch
|
||||
var receiverOptions = new ReceiverOptions
|
||||
{
|
||||
AllowedUpdates = { } // receive all update types
|
||||
};
|
||||
|
||||
}
|
||||
this.TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, __cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
|
||||
private async void TelegramClient_OnMessageEdited(object sender, Telegram.Bot.Args.MessageEventArgs e)
|
||||
public void StopReceiving()
|
||||
{
|
||||
//Skip empty messages by default
|
||||
if (e.Message == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var mr = new MessageResult(e);
|
||||
mr.Client = this;
|
||||
OnMessageEdit(mr);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
__cancellationTokenSource.Cancel();
|
||||
}
|
||||
|
||||
private async void TelegramClient_OnCallbackQuery(object sender, Telegram.Bot.Args.CallbackQueryEventArgs e)
|
||||
|
||||
public Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ar = new MessageResult(e);
|
||||
ar.Client = this;
|
||||
OnAction(ar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
OnMessageLoop(new UpdateResult(update, null));
|
||||
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
if (exception is ApiRequestException exAPI)
|
||||
{
|
||||
Console.WriteLine($"Telegram API Error:\n[{exAPI.ErrorCode}]\n{exAPI.Message}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(exception.ToString());
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This will return the current list of bot commands.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<BotCommand[]> GetBotCommands()
|
||||
{
|
||||
return await this.TelegramClient.GetMyCommandsAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will set your bot commands to the given list.
|
||||
/// </summary>
|
||||
/// <param name="botcommands"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SetBotCommands(List<BotCommand> botcommands)
|
||||
{
|
||||
await this.TelegramClient.SetMyCommandsAsync(botcommands);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#region "Events"
|
||||
|
||||
public event EventHandler<MessageResult> Message
|
||||
|
||||
|
||||
public event Async.AsyncEventHandler<UpdateResult> MessageLoop
|
||||
{
|
||||
add
|
||||
{
|
||||
this.__Events.AddHandler(__evOnMessage, value);
|
||||
this.__Events.AddHandler(__evOnMessageLoop, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.__Events.RemoveHandler(__evOnMessage, value);
|
||||
this.__Events.RemoveHandler(__evOnMessageLoop, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMessage(MessageResult result)
|
||||
public void OnMessageLoop(UpdateResult update)
|
||||
{
|
||||
(this.__Events[__evOnMessage] as EventHandler<MessageResult>)?.Invoke(this, result);
|
||||
}
|
||||
|
||||
public event EventHandler<MessageResult> MessageEdit
|
||||
{
|
||||
add
|
||||
{
|
||||
this.__Events.AddHandler(__evOnMessageEdit, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.__Events.RemoveHandler(__evOnMessageEdit, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMessageEdit(MessageResult result)
|
||||
{
|
||||
(this.__Events[__evOnMessageEdit] as EventHandler<MessageResult>)?.Invoke(this, result);
|
||||
}
|
||||
|
||||
public event EventHandler<MessageResult> Action
|
||||
{
|
||||
add
|
||||
{
|
||||
this.__Events.AddHandler(__evCallbackQuery, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.__Events.RemoveHandler(__evCallbackQuery, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnAction(MessageResult result)
|
||||
{
|
||||
(this.__Events[__evCallbackQuery] as EventHandler<MessageResult>)?.Invoke(this, result);
|
||||
(this.__Events[__evOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,15 +3,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class MessageResult : ResultBase
|
||||
{
|
||||
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
|
||||
|
||||
public Telegram.Bot.Args.CallbackQueryEventArgs RawCallbackData { get; set; }
|
||||
public Telegram.Bot.Types.Update UpdateData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Device/ChatId
|
||||
@@ -20,12 +21,19 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Chat.Id ?? this.RawCallbackData?.CallbackQuery.Message?.Chat.Id ?? 0;
|
||||
return this.UpdateData?.Message?.Chat?.Id
|
||||
?? this.UpdateData?.EditedMessage?.Chat.Id
|
||||
?? this.UpdateData?.CallbackQuery.Message?.Chat.Id
|
||||
?? Device?.DeviceId
|
||||
?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
public DeviceSession Device
|
||||
{ get; set; }
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The message id
|
||||
@@ -34,7 +42,10 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Message?.MessageId ?? this.RawCallbackData?.CallbackQuery?.Message?.MessageId ?? 0;
|
||||
return this.UpdateData?.Message?.MessageId
|
||||
?? this.Message?.MessageId
|
||||
?? this.UpdateData?.CallbackQuery?.Message?.MessageId
|
||||
?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +53,7 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Text ?? "";
|
||||
return this.UpdateData?.Message?.Text ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +61,7 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Text ?? "";
|
||||
return this.UpdateData?.Message?.Text ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +69,19 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
|
||||
return Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
public Message Message
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.UpdateData?.Message
|
||||
?? this.UpdateData?.EditedMessage
|
||||
?? this.UpdateData?.ChannelPost
|
||||
?? this.UpdateData?.EditedChannelPost
|
||||
?? this.UpdateData?.CallbackQuery?.Message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,14 +92,14 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return (this.RawCallbackData != null);
|
||||
return (this.UpdateData.CallbackQuery != null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is this a system call ? Starts with a slash '/' and a command
|
||||
/// Is this a command ? Starts with a slash '/' and a command
|
||||
/// </summary>
|
||||
public bool IsSystemCall
|
||||
public bool IsBotCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -87,11 +110,11 @@ namespace TelegramBotBase.Base
|
||||
/// <summary>
|
||||
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789)
|
||||
/// </summary>
|
||||
public List<String> SystemCallParameters
|
||||
public List<String> BotCommandParameters
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsSystemCall)
|
||||
if (!IsBotCommand)
|
||||
return new List<string>();
|
||||
|
||||
//Split by empty space and skip first entry (command itself), return as list
|
||||
@@ -100,13 +123,13 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns just the System call command (i.e. /start 1 2 3 => /start)
|
||||
/// Returns just the command (i.e. /start 1 2 3 => /start)
|
||||
/// </summary>
|
||||
public String SystemCommand
|
||||
public String BotCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsSystemCall)
|
||||
if (!IsBotCommand)
|
||||
return null;
|
||||
|
||||
return this.MessageText.Split(' ')[0];
|
||||
@@ -124,7 +147,7 @@ namespace TelegramBotBase.Base
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawCallbackData?.CallbackQuery?.Data;
|
||||
return this.UpdateData?.CallbackQuery?.Data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,16 +174,9 @@ namespace TelegramBotBase.Base
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ConfirmAction(String message = "")
|
||||
public async Task ConfirmAction(String message = "", bool showAlert = false, String urlToOpen = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.Client.TelegramClient.AnswerCallbackQueryAsync(this.RawCallbackData.CallbackQuery.Id, message);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
await this.Device.ConfirmAction(this.UpdateData.CallbackQuery.Id, message, showAlert, urlToOpen);
|
||||
}
|
||||
|
||||
public override async Task DeleteMessage()
|
||||
@@ -175,17 +191,15 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MessageResult(Telegram.Bot.Args.MessageEventArgs rawdata)
|
||||
internal MessageResult()
|
||||
{
|
||||
this.RawMessageData = rawdata;
|
||||
this.Message = rawdata.Message;
|
||||
|
||||
}
|
||||
|
||||
public MessageResult(Telegram.Bot.Args.CallbackQueryEventArgs callback)
|
||||
public MessageResult(Telegram.Bot.Types.Update update)
|
||||
{
|
||||
this.RawCallbackData = callback;
|
||||
this.Message = callback.CallbackQuery.Message;
|
||||
this.UpdateData = update;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
@@ -20,7 +21,7 @@ namespace TelegramBotBase.Base
|
||||
}
|
||||
}
|
||||
|
||||
public Telegram.Bot.Types.Message Message { get; set; }
|
||||
public virtual Telegram.Bot.Types.Message Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the current message
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class UpdateResult : ResultBase
|
||||
{
|
||||
public UpdateResult(Update rawData, DeviceSession device)
|
||||
{
|
||||
RawData = rawData;
|
||||
Device = device;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Device/ChatId
|
||||
/// </summary>
|
||||
public override long DeviceId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawData?.Message?.Chat?.Id
|
||||
?? this.RawData?.CallbackQuery?.Message?.Chat?.Id
|
||||
?? Device?.DeviceId
|
||||
?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
public Update RawData { get; set; }
|
||||
|
||||
public override Message Message
|
||||
{
|
||||
get
|
||||
{
|
||||
return RawData?.Message
|
||||
?? RawData?.EditedMessage
|
||||
?? RawData?.ChannelPost
|
||||
?? RawData?.EditedChannelPost
|
||||
?? RawData?.CallbackQuery?.Message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public DeviceSession Device
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+173
-344
@@ -3,11 +3,17 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Attributes;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Factories.MessageLoops;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
using TelegramBotBase.Sessions;
|
||||
@@ -18,8 +24,7 @@ namespace TelegramBotBase
|
||||
/// Bot base class for full Device/Context and Messagehandling
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class BotBase<T>
|
||||
where T : FormBase
|
||||
public class BotBase
|
||||
{
|
||||
public MessageClient Client { get; set; }
|
||||
|
||||
@@ -36,7 +41,10 @@ namespace TelegramBotBase
|
||||
/// <summary>
|
||||
/// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start
|
||||
/// </summary>
|
||||
public List<String> SystemCalls { get; set; }
|
||||
public List<BotCommand> BotCommands { get; set; }
|
||||
|
||||
|
||||
#region "Events"
|
||||
|
||||
private EventHandlerList __Events = new EventHandlerList();
|
||||
|
||||
@@ -46,22 +54,14 @@ namespace TelegramBotBase
|
||||
|
||||
private static object __evSystemCall = new object();
|
||||
|
||||
public delegate Task SystemCallEventHandler(object sender, SystemCallEventArgs e);
|
||||
public delegate Task BotCommandEventHandler(object sender, BotCommandEventArgs e);
|
||||
|
||||
private static object __evException = new object();
|
||||
|
||||
private static object __evUnhandledCall = new object();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Skips all messages during running (good for big delay updates)
|
||||
/// </summary>
|
||||
public bool SkipAllMessages { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Loggs all messages and sent them to the event handler
|
||||
/// </summary>
|
||||
public bool LogAllMessages { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Enable the SessionState (you need to implement on call forms the IStateForm interface)
|
||||
@@ -69,97 +69,37 @@ namespace TelegramBotBase
|
||||
public IStateMachine StateMachine { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// How often could a form navigate to another (within one user action/call/message)
|
||||
/// Offers functionality to manage the creation process of the start form.
|
||||
/// </summary>
|
||||
private const int NavigationMaximum = 10;
|
||||
public IStartFormFactory StartFormFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Simple start of your Bot with the APIKey
|
||||
/// Contains the message loop factory, which cares about "message-management."
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
public BotBase(String apiKey)
|
||||
{
|
||||
this.APIKey = apiKey;
|
||||
|
||||
this.Client = new Base.MessageClient(this.APIKey);
|
||||
this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
|
||||
this.SystemCalls = new List<string>();
|
||||
|
||||
this.Sessions = new SessionBase();
|
||||
this.Sessions.Client = this.Client;
|
||||
}
|
||||
public IMessageLoopFactory MessageLoopFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Simple start of your Bot with the APIKey and a proxyAdress
|
||||
/// All internal used settings.
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
|
||||
public BotBase(String apiKey, System.Net.Http.HttpClient proxy)
|
||||
public Dictionary<eSettings, uint> SystemSettings { get; private set; }
|
||||
|
||||
public BotBase()
|
||||
{
|
||||
this.APIKey = apiKey;
|
||||
this.SystemSettings = new Dictionary<eSettings, uint>();
|
||||
|
||||
this.Client = new Base.MessageClient(this.APIKey, proxy);
|
||||
SetSetting(eSettings.MaxNumberOfRetries, 5);
|
||||
SetSetting(eSettings.NavigationMaximum, 10);
|
||||
SetSetting(eSettings.LogAllMessages, false);
|
||||
SetSetting(eSettings.SkipAllMessages, false);
|
||||
SetSetting(eSettings.SaveSessionsOnConsoleExit, false);
|
||||
|
||||
this.SystemCalls = new List<string>();
|
||||
this.BotCommands = new List<BotCommand>();
|
||||
|
||||
this.Sessions = new SessionBase();
|
||||
this.Sessions.Client = this.Client;
|
||||
this.Sessions.BotBase = this;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
|
||||
public BotBase(String apiKey, String proxyBaseAddress)
|
||||
{
|
||||
this.APIKey = apiKey;
|
||||
|
||||
var url = new Uri(proxyBaseAddress);
|
||||
|
||||
this.Client = new Base.MessageClient(this.APIKey, url);
|
||||
|
||||
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>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="proxyHost">i.e. 127.0.0.1</param>
|
||||
/// <param name="proxyPort">i.e. 10000</param>
|
||||
public BotBase(String apiKey, String proxyHost, int proxyPort)
|
||||
{
|
||||
this.APIKey = apiKey;
|
||||
|
||||
this.Client = new Base.MessageClient(this.APIKey, proxyHost, proxyPort);
|
||||
|
||||
this.SystemCalls = new List<string>();
|
||||
|
||||
this.Sessions = new SessionBase();
|
||||
this.Sessions.Client = this.Client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start your Bot
|
||||
@@ -169,16 +109,58 @@ namespace TelegramBotBase
|
||||
if (this.Client == null)
|
||||
return;
|
||||
|
||||
this.Client.Message += Client_Message;
|
||||
this.Client.MessageEdit += Client_MessageEdit;
|
||||
this.Client.Action += Client_Action;
|
||||
this.Client.MessageLoop += Client_MessageLoop;
|
||||
|
||||
|
||||
if (this.StateMachine != null)
|
||||
{
|
||||
this.Sessions.LoadSessionStates(this.StateMachine);
|
||||
}
|
||||
|
||||
this.Client.TelegramClient.StartReceiving();
|
||||
//Enable auto session saving
|
||||
if (this.GetSetting(eSettings.SaveSessionsOnConsoleExit, false))
|
||||
{
|
||||
TelegramBotBase.Tools.Console.SetHandler(() =>
|
||||
{
|
||||
this.Sessions.SaveSessionStates();
|
||||
});
|
||||
}
|
||||
|
||||
DeviceSession.MaxNumberOfRetries = this.GetSetting(eSettings.MaxNumberOfRetries, 5);
|
||||
|
||||
this.Client.StartReceiving();
|
||||
}
|
||||
|
||||
|
||||
private async Task Client_MessageLoop(object sender, UpdateResult e)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
if (ds == null)
|
||||
{
|
||||
ds = this.Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult();
|
||||
e.Device = ds;
|
||||
ds.LastMessage = e.RawData.Message;
|
||||
|
||||
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
|
||||
}
|
||||
|
||||
var mr = new MessageResult(e.RawData);
|
||||
|
||||
int i = 0;
|
||||
|
||||
//Should formulars get navigated (allow maximum of 10, to dont get loops)
|
||||
do
|
||||
{
|
||||
i++;
|
||||
|
||||
//Reset navigation
|
||||
ds.FormSwitched = false;
|
||||
|
||||
await MessageLoopFactory.MessageLoop(this, ds, e, mr);
|
||||
|
||||
mr.IsFirstHandler = false;
|
||||
|
||||
} while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10));
|
||||
}
|
||||
|
||||
|
||||
@@ -190,15 +172,12 @@ namespace TelegramBotBase
|
||||
if (this.Client == null)
|
||||
return;
|
||||
|
||||
this.Client.Message -= Client_Message;
|
||||
this.Client.Action -= Client_Action;
|
||||
this.Client.MessageLoop -= Client_MessageLoop;
|
||||
|
||||
this.Client.TelegramClient.StopReceiving();
|
||||
|
||||
if (this.StateMachine != null)
|
||||
{
|
||||
this.Sessions.SaveSessionStates(this.StateMachine);
|
||||
}
|
||||
this.Client.StopReceiving();
|
||||
|
||||
this.Sessions.SaveSessionStates();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -217,263 +196,110 @@ namespace TelegramBotBase
|
||||
}
|
||||
}
|
||||
|
||||
private async void Client_Message(object sender, MessageResult e)
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This will invoke the full message loop for the device even when no "userevent" like message or action has been raised.
|
||||
/// </summary>
|
||||
/// <param name="DeviceId">Contains the device/chat id of the device to update.</param>
|
||||
public async Task InvokeMessageLoop(long DeviceId)
|
||||
{
|
||||
if (this.SkipAllMessages)
|
||||
return;
|
||||
var mr = new MessageResult();
|
||||
|
||||
try
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
if (LogAllMessages)
|
||||
{
|
||||
OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e));
|
||||
}
|
||||
|
||||
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
|
||||
|
||||
await Client_TryMessage(sender, e);
|
||||
}
|
||||
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
OnException(new SystemExceptionEventArgs(e.Message.Text, ds?.DeviceId ?? -1, ds, ex));
|
||||
}
|
||||
await InvokeMessageLoop(DeviceId, mr);
|
||||
}
|
||||
|
||||
private async Task Client_TryMessage(object sender, MessageResult e)
|
||||
{
|
||||
DeviceSession ds = e.Device;
|
||||
if (ds == null)
|
||||
{
|
||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
ds.LastMessage = e.Message;
|
||||
|
||||
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
|
||||
}
|
||||
|
||||
ds.ChatTitle = e.Message.Chat.Username ?? e.Message.Chat.Title;
|
||||
ds.LastAction = DateTime.Now;
|
||||
ds.LastMessage = e.Message;
|
||||
|
||||
//Is this a systemcall ?
|
||||
if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand))
|
||||
{
|
||||
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, e.Message, ds.DeviceId, ds);
|
||||
await OnSystemCall(sce);
|
||||
|
||||
if (sce.Handled)
|
||||
return;
|
||||
}
|
||||
|
||||
FormBase activeForm = null;
|
||||
|
||||
int i = 0;
|
||||
|
||||
//Should formulars get navigated (allow maximum of 10, to dont get loops)
|
||||
do
|
||||
{
|
||||
i++;
|
||||
|
||||
//Reset navigation
|
||||
ds.FormSwitched = false;
|
||||
|
||||
activeForm = ds.ActiveForm;
|
||||
|
||||
//Pre Loading Event
|
||||
await activeForm.PreLoad(e);
|
||||
|
||||
//Send Load event to controls
|
||||
await activeForm.LoadControls(e);
|
||||
|
||||
//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 (!ds.FormSwitched)
|
||||
{
|
||||
await activeForm.RenderControls(e);
|
||||
|
||||
await activeForm.Render(e);
|
||||
}
|
||||
|
||||
e.IsFirstHandler = false;
|
||||
|
||||
} while (ds.FormSwitched && i < NavigationMaximum);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async void Client_MessageEdit(object sender, MessageResult e)
|
||||
{
|
||||
if (this.SkipAllMessages)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
if (LogAllMessages)
|
||||
{
|
||||
OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e));
|
||||
}
|
||||
|
||||
//Call same, to handle received liked edited
|
||||
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
|
||||
|
||||
await Client_TryMessageEdit(sender, e);
|
||||
}
|
||||
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
OnException(new SystemExceptionEventArgs(e.Message.Text, ds?.DeviceId ?? -1, ds, ex));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Client_TryMessageEdit(object sender, MessageResult e)
|
||||
{
|
||||
DeviceSession ds = e.Device;
|
||||
if (ds == null)
|
||||
{
|
||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||
e.Device = ds;
|
||||
}
|
||||
|
||||
ds.ChatTitle = e.Message.Chat.Username ?? e.Message.Chat.Title;
|
||||
ds.LastAction = DateTime.Now;
|
||||
ds.LastMessage = e.Message;
|
||||
|
||||
//Pre Loading Event
|
||||
await ds.ActiveForm.Edited(e);
|
||||
|
||||
//When form has been switched due navigation within the edit method, reopen Client_Message
|
||||
if (ds.FormSwitched)
|
||||
{
|
||||
await Client_TryMessage(sender, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Client_Action(object sender, MessageResult e)
|
||||
/// <summary>
|
||||
/// This will invoke the full message loop for the device even when no "userevent" like message or action has been raised.
|
||||
/// </summary>
|
||||
/// <param name="DeviceId">Contains the device/chat id of the device to update.</param>
|
||||
/// <param name="e"></param>
|
||||
public async Task InvokeMessageLoop(long DeviceId, MessageResult e)
|
||||
{
|
||||
try
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
DeviceSession ds = this.Sessions.GetSession(DeviceId);
|
||||
e.Device = ds;
|
||||
|
||||
if (LogAllMessages)
|
||||
{
|
||||
OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e));
|
||||
}
|
||||
|
||||
Client_TryAction(sender, e);
|
||||
await MessageLoopFactory.MessageLoop(this, ds, new UpdateResult(e.UpdateData, ds), e);
|
||||
//await Client_Loop(this, e);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
|
||||
OnException(new SystemExceptionEventArgs(e.Message.Text, ds?.DeviceId ?? -1, ds, ex));
|
||||
DeviceSession ds = this.Sessions.GetSession(DeviceId);
|
||||
OnException(new SystemExceptionEventArgs(e.Message.Text, DeviceId, ds, ex));
|
||||
}
|
||||
}
|
||||
|
||||
private async void Client_TryAction(object sender, MessageResult e)
|
||||
|
||||
/// <summary>
|
||||
/// Will get invoke on an unhandled call.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
public void MessageLoopFactory_UnhandledCall(object sender, UnhandledCallEventArgs e)
|
||||
{
|
||||
DeviceSession ds = e.Device;
|
||||
if (ds == null)
|
||||
{
|
||||
ds = await this.Sessions.StartSession<T>(e.DeviceId);
|
||||
e.Device = ds;
|
||||
}
|
||||
|
||||
ds.ChatTitle = e.Message.Chat.Username ?? e.Message.Chat.Title;
|
||||
ds.LastAction = DateTime.Now;
|
||||
ds.LastMessage = e.Message;
|
||||
|
||||
FormBase activeForm = null;
|
||||
|
||||
int i = 0;
|
||||
|
||||
//Should formulars get navigated (allow maximum of 10, to dont get loops)
|
||||
do
|
||||
{
|
||||
i++;
|
||||
|
||||
//Reset navigation
|
||||
ds.FormSwitched = false;
|
||||
|
||||
activeForm = ds.ActiveForm;
|
||||
|
||||
//Pre Loading Event
|
||||
await activeForm.PreLoad(e);
|
||||
|
||||
//Send Load event to controls
|
||||
await activeForm.LoadControls(e);
|
||||
|
||||
//Loading Event
|
||||
await activeForm.Load(e);
|
||||
|
||||
//Action Event
|
||||
if (!ds.FormSwitched)
|
||||
{
|
||||
//Send Action event to controls
|
||||
await activeForm.ActionControls(e);
|
||||
|
||||
//Send Action event to form itself
|
||||
await activeForm.Action(e);
|
||||
|
||||
if (!e.Handled)
|
||||
{
|
||||
var uhc = new UnhandledCallEventArgs(e.Message.Text, e.RawData, ds.DeviceId, e.MessageId, e.Message, ds);
|
||||
OnUnhandledCall(uhc);
|
||||
|
||||
if (uhc.Handled)
|
||||
{
|
||||
if (ds.FormSwitched)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Render Event
|
||||
if (!ds.FormSwitched)
|
||||
{
|
||||
await activeForm.RenderControls(e);
|
||||
|
||||
await activeForm.Render(e);
|
||||
}
|
||||
|
||||
e.IsFirstHandler = false;
|
||||
|
||||
} while (ds.FormSwitched && i < NavigationMaximum);
|
||||
|
||||
OnUnhandledCall(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method will update all local created bot commands to the botfather.
|
||||
/// </summary>
|
||||
public async Task UploadBotCommands()
|
||||
{
|
||||
await this.Client.SetBotCommands(this.BotCommands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Could set a variety of settings to improve the bot handling.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <param name="Value"></param>
|
||||
public void SetSetting(eSettings set, uint Value)
|
||||
{
|
||||
this.SystemSettings[set] = Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Could set a variety of settings to improve the bot handling.
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <param name="Value"></param>
|
||||
public void SetSetting(eSettings set, bool Value)
|
||||
{
|
||||
this.SystemSettings[set] = (Value ? 1u : 0u);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Could get the current value of a setting
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public uint GetSetting(eSettings set, uint defaultValue)
|
||||
{
|
||||
if (!this.SystemSettings.ContainsKey(set))
|
||||
return defaultValue;
|
||||
|
||||
return this.SystemSettings[set];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Could get the current value of a setting
|
||||
/// </summary>
|
||||
/// <param name="set"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public bool GetSetting(eSettings set, bool defaultValue)
|
||||
{
|
||||
if (!this.SystemSettings.ContainsKey(set))
|
||||
return defaultValue;
|
||||
|
||||
return this.SystemSettings[set] == 0u ? false : true;
|
||||
}
|
||||
|
||||
#region "Events"
|
||||
|
||||
/// <summary>
|
||||
/// Will be called if a session/context gets started
|
||||
/// </summary>
|
||||
@@ -518,15 +344,15 @@ namespace TelegramBotBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will be called if a system call gets raised
|
||||
/// Will be called if a bot command gets raised
|
||||
/// </summary>
|
||||
public event SystemCallEventHandler SystemCall;
|
||||
public event BotCommandEventHandler BotCommand;
|
||||
|
||||
|
||||
public async Task OnSystemCall(SystemCallEventArgs e)
|
||||
public async Task OnBotCommand(BotCommandEventArgs e)
|
||||
{
|
||||
if (this.SystemCall != null)
|
||||
await SystemCall(this, e);
|
||||
if (this.BotCommand != null)
|
||||
await BotCommand(this, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -570,5 +396,8 @@ namespace TelegramBotBase
|
||||
(this.__Events[__evUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Builder.Interfaces;
|
||||
using TelegramBotBase.Commands;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
using TelegramBotBase.Localizations;
|
||||
using TelegramBotBase.States;
|
||||
|
||||
namespace TelegramBotBase.Builder
|
||||
{
|
||||
public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage, ILanguageSelectionStage
|
||||
{
|
||||
|
||||
String _apiKey = null;
|
||||
|
||||
IStartFormFactory _factory = null;
|
||||
|
||||
MessageClient _client = null;
|
||||
|
||||
List<BotCommand> _botcommands = new List<BotCommand>();
|
||||
|
||||
IStateMachine _statemachine = null;
|
||||
|
||||
IMessageLoopFactory _messageloopfactory = null;
|
||||
|
||||
private BotBaseBuilder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static IAPIKeySelectionStage Create()
|
||||
{
|
||||
return new BotBaseBuilder();
|
||||
}
|
||||
|
||||
#region "Step 1 (Basic Stuff)"
|
||||
|
||||
public IMessageLoopSelectionStage WithAPIKey(string apiKey)
|
||||
{
|
||||
this._apiKey = apiKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public IBuildingStage QuickStart(string apiKey, Type StartForm)
|
||||
{
|
||||
this._apiKey = apiKey;
|
||||
this._factory = new Factories.DefaultStartFormFactory(StartForm);
|
||||
|
||||
DefaultMessageLoop();
|
||||
|
||||
NoProxy();
|
||||
|
||||
OnlyStart();
|
||||
|
||||
NoSerialization();
|
||||
|
||||
DefaultLanguage();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public IBuildingStage QuickStart<T>(string apiKey)
|
||||
where T : FormBase
|
||||
{
|
||||
this._apiKey = apiKey;
|
||||
this._factory = new Factories.DefaultStartFormFactory(typeof(T));
|
||||
|
||||
DefaultMessageLoop();
|
||||
|
||||
NoProxy();
|
||||
|
||||
OnlyStart();
|
||||
|
||||
NoSerialization();
|
||||
|
||||
DefaultLanguage();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory)
|
||||
{
|
||||
this._apiKey = apiKey;
|
||||
this._factory = StartFormFactory;
|
||||
|
||||
DefaultMessageLoop();
|
||||
|
||||
NoProxy();
|
||||
|
||||
OnlyStart();
|
||||
|
||||
NoSerialization();
|
||||
|
||||
DefaultLanguage();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 2 (Message Loop)"
|
||||
|
||||
public IStartFormSelectionStage DefaultMessageLoop()
|
||||
{
|
||||
_messageloopfactory = new Factories.MessageLoops.FormBaseMessageLoop();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public IStartFormSelectionStage CustomMessageLoop(IMessageLoopFactory messageLoopClass)
|
||||
{
|
||||
_messageloopfactory = messageLoopClass;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public IStartFormSelectionStage CustomMessageLoop<T>()
|
||||
where T : class, new()
|
||||
{
|
||||
_messageloopfactory = typeof(T).GetConstructor(new Type[] { })?.Invoke(new object[] { }) as IMessageLoopFactory;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 3 (Start Form/Factory)"
|
||||
|
||||
public INetworkingSelectionStage WithStartForm(Type startFormClass)
|
||||
{
|
||||
this._factory = new Factories.DefaultStartFormFactory(startFormClass);
|
||||
return this;
|
||||
}
|
||||
|
||||
public INetworkingSelectionStage WithStartForm<T>()
|
||||
where T : FormBase, new()
|
||||
{
|
||||
this._factory = new Factories.DefaultStartFormFactory(typeof(T));
|
||||
return this;
|
||||
}
|
||||
|
||||
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
|
||||
{
|
||||
this._factory = factory;
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 4 (Network Settings)"
|
||||
|
||||
public IBotCommandsStage WithProxy(string proxyAddress)
|
||||
{
|
||||
var url = new Uri(proxyAddress);
|
||||
_client = new MessageClient(_apiKey, url);
|
||||
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public IBotCommandsStage NoProxy()
|
||||
{
|
||||
_client = new MessageClient(_apiKey);
|
||||
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
|
||||
{
|
||||
_client = new MessageClient(_apiKey, tgclient);
|
||||
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
|
||||
{
|
||||
_client = new MessageClient(_apiKey, proxyHost, proxyPort);
|
||||
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IBotCommandsStage WithHttpClient(HttpClient tgclient)
|
||||
{
|
||||
_client = new MessageClient(_apiKey, tgclient);
|
||||
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 5 (Bot Commands)"
|
||||
|
||||
public ISessionSerializationStage NoCommands()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISessionSerializationStage OnlyStart()
|
||||
{
|
||||
_botcommands.Start("Starts the bot");
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public ISessionSerializationStage DefaultCommands()
|
||||
{
|
||||
_botcommands.Start("Starts the bot");
|
||||
_botcommands.Help("Should show you some help");
|
||||
_botcommands.Settings("Should show you some settings");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISessionSerializationStage CustomCommands(Action<List<BotCommand>> action)
|
||||
{
|
||||
action?.Invoke(_botcommands);
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 6 (Serialization)"
|
||||
|
||||
public ILanguageSelectionStage NoSerialization()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILanguageSelectionStage UseSerialization(IStateMachine machine)
|
||||
{
|
||||
this._statemachine = machine;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public ILanguageSelectionStage UseJSON(string path)
|
||||
{
|
||||
this._statemachine = new JSONStateMachine(path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILanguageSelectionStage UseSimpleJSON(string path)
|
||||
{
|
||||
this._statemachine = new SimpleJSONStateMachine(path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILanguageSelectionStage UseXML(string path)
|
||||
{
|
||||
this._statemachine = new XMLStateMachine(path);
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 7 (Language)"
|
||||
|
||||
public IBuildingStage DefaultLanguage()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public IBuildingStage UseEnglish()
|
||||
{
|
||||
Localizations.Default.Language = new Localizations.English();
|
||||
return this;
|
||||
}
|
||||
|
||||
public IBuildingStage UseGerman()
|
||||
{
|
||||
Localizations.Default.Language = new Localizations.German();
|
||||
return this;
|
||||
}
|
||||
|
||||
public IBuildingStage Custom(Localization language)
|
||||
{
|
||||
Localizations.Default.Language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public BotBase Build()
|
||||
{
|
||||
var bb = new BotBase();
|
||||
|
||||
bb.APIKey = _apiKey;
|
||||
bb.StartFormFactory = _factory;
|
||||
|
||||
bb.Client = _client;
|
||||
|
||||
bb.Sessions.Client = bb.Client;
|
||||
|
||||
bb.BotCommands = _botcommands;
|
||||
|
||||
bb.StateMachine = _statemachine;
|
||||
|
||||
bb.MessageLoopFactory = _messageloopfactory;
|
||||
|
||||
bb.MessageLoopFactory.UnhandledCall += bb.MessageLoopFactory_UnhandledCall;
|
||||
|
||||
return bb;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface IAPIKeySelectionStage
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the API Key which will be used by the telegram bot client.
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <returns></returns>
|
||||
IMessageLoopSelectionStage WithAPIKey(String apiKey);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Quick and easy way to create a BotBase instance.
|
||||
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="StartForm"></param>
|
||||
/// <returns></returns>
|
||||
IBuildingStage QuickStart(String apiKey, Type StartForm);
|
||||
|
||||
/// <summary>
|
||||
/// Quick and easy way to create a BotBase instance.
|
||||
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <returns></returns>
|
||||
IBuildingStage QuickStart<T>(String apiKey) where T : FormBase;
|
||||
|
||||
/// <summary>
|
||||
/// Quick and easy way to create a BotBase instance.
|
||||
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="StartFormFactory"></param>
|
||||
/// <returns></returns>
|
||||
IBuildingStage QuickStart(String apiKey, IStartFormFactory StartFormFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface IBotCommandsStage
|
||||
{
|
||||
/// <summary>
|
||||
/// Does not create any commands.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
ISessionSerializationStage NoCommands();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates default commands for start, help and settings.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
ISessionSerializationStage DefaultCommands();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Only adds the start command.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
ISessionSerializationStage OnlyStart();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gives you the ability to add custom commands.
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
ISessionSerializationStage CustomCommands(Action<List<BotCommand>> action);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface IBuildingStage
|
||||
{
|
||||
BotBase Build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Localizations;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface ILanguageSelectionStage
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Selects the default language for control usage. (English)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IBuildingStage DefaultLanguage();
|
||||
|
||||
/// <summary>
|
||||
/// Selects english as the default language for control labels.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IBuildingStage UseEnglish();
|
||||
|
||||
/// <summary>
|
||||
/// Selects german as the default language for control labels.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IBuildingStage UseGerman();
|
||||
|
||||
/// <summary>
|
||||
/// Selects a custom language as the default language for control labels.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IBuildingStage Custom(Localization language);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface IMessageLoopSelectionStage
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a default message loop.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
IStartFormSelectionStage DefaultMessageLoop();
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a custom message loop.
|
||||
/// </summary>
|
||||
/// <param name="startFormClass"></param>
|
||||
/// <returns></returns>
|
||||
IStartFormSelectionStage CustomMessageLoop(IMessageLoopFactory startFormClass);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a custom message loop.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
IStartFormSelectionStage CustomMessageLoop<T>() where T : class, new();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface INetworkingSelectionStage
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a proxy as network configuration.
|
||||
/// </summary>
|
||||
/// <param name="proxyAddress"></param>
|
||||
/// <returns></returns>
|
||||
IBotCommandsStage WithProxy(String proxyAddress);
|
||||
|
||||
/// <summary>
|
||||
/// Do not choose a proxy as network configuration.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IBotCommandsStage NoProxy();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a custom instance of TelegramBotClient.
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <returns></returns>
|
||||
IBotCommandsStage WithBotClient(TelegramBotClient client);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets the custom proxy host and port.
|
||||
/// </summary>
|
||||
/// <param name="proxyHost"></param>
|
||||
/// <param name="Port"></param>
|
||||
/// <returns></returns>
|
||||
IBotCommandsStage WithHostAndPort(String proxyHost, int Port);
|
||||
|
||||
/// <summary>
|
||||
/// Uses a custom http client.
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <returns></returns>
|
||||
IBotCommandsStage WithHttpClient(HttpClient client);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface ISessionSerializationStage
|
||||
{
|
||||
/// <summary>
|
||||
/// Do not uses serialization.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
ILanguageSelectionStage NoSerialization();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets the state machine for serialization.
|
||||
/// </summary>
|
||||
/// <param name="machine"></param>
|
||||
/// <returns></returns>
|
||||
ILanguageSelectionStage UseSerialization(IStateMachine machine);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Using the complex version of .Net JSON, which can serialize all objects.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
ILanguageSelectionStage UseJSON(String path);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Use the easy version of .Net JSON, which can serialize basic types, but not generics and others.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
ILanguageSelectionStage UseSimpleJSON(String path);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uses the XML serializer for session serialization.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
ILanguageSelectionStage UseXML(String path);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Builder.Interfaces
|
||||
{
|
||||
public interface IStartFormSelectionStage
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a start form type which will be used for new sessions.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
INetworkingSelectionStage WithStartForm(Type startFormClass);
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a generic start form which will be used for new sessions.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
INetworkingSelectionStage WithStartForm<T>() where T : FormBase, new();
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a StartFormFactory which will be use for new sessions.
|
||||
/// </summary>
|
||||
/// <param name="factory"></param>
|
||||
/// <returns></returns>
|
||||
INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBotBase.Commands
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adding the default /start command with a description.
|
||||
/// </summary>
|
||||
/// <param name="cmds"></param>
|
||||
/// <param name="description"></param>
|
||||
public static void Start(this List<BotCommand> cmds, String description)
|
||||
{
|
||||
cmds.Add(new BotCommand() { Command = "start", Description = description });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adding the default /help command with a description.
|
||||
/// </summary>
|
||||
/// <param name="cmds"></param>
|
||||
/// <param name="description"></param>
|
||||
public static void Help(this List<BotCommand> cmds, String description)
|
||||
{
|
||||
cmds.Add(new BotCommand() { Command = "help", Description = description });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adding the default /settings command with a description.
|
||||
/// </summary>
|
||||
/// <param name="cmds"></param>
|
||||
/// <param name="description"></param>
|
||||
public static void Settings(this List<BotCommand> cmds, String description)
|
||||
{
|
||||
cmds.Add(new BotCommand() { Command = "settings", Description = description });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adding the command with a description.
|
||||
/// </summary>
|
||||
/// <param name="cmds"></param>
|
||||
/// <param name="command"></param>
|
||||
/// <param name="description"></param>
|
||||
public static void Add(this List<BotCommand> cmds, String command, String description)
|
||||
{
|
||||
cmds.Add(new BotCommand() { Command = command, Description = description });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,7 @@ namespace TelegramBotBase.Constants
|
||||
|
||||
public const int MaxReplyKeyboardCols = 12;
|
||||
|
||||
public const int MessageDeletionsPerSecond = 30;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,308 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Exceptions;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
{
|
||||
public class ButtonGrid : Base.ControlBase
|
||||
{
|
||||
|
||||
public String Title { get; set; } = "Toggle";
|
||||
|
||||
public String ConfirmationText { get; set; } = "";
|
||||
|
||||
private bool RenderNecessary = true;
|
||||
|
||||
private static readonly object __evButtonClicked = new object();
|
||||
|
||||
private readonly EventHandlerList Events = new EventHandlerList();
|
||||
|
||||
public ButtonForm ButtonsForm { get; set; }
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
|
||||
/// Source: https://core.telegram.org/bots/api#replykeyboardmarkup
|
||||
/// </summary>
|
||||
public bool ResizeKeyboard { get; set; } = false;
|
||||
|
||||
public bool OneTimeKeyboard { get; set; } = false;
|
||||
|
||||
public bool HideKeyboardOnCleanup { get; set; } = true;
|
||||
|
||||
public bool DeletePreviousMessage { get; set; } = true;
|
||||
|
||||
public ParseMode MessageParseMode { get; set; } = ParseMode.Default;
|
||||
|
||||
/// <summary>
|
||||
/// Defines which type of Button Keyboard should be rendered.
|
||||
/// </summary>
|
||||
public eKeyboardType KeyboardType
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_eKeyboardType;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_eKeyboardType != value)
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
|
||||
Cleanup().Wait();
|
||||
|
||||
m_eKeyboardType = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private eKeyboardType m_eKeyboardType = eKeyboardType.ReplyKeyboard;
|
||||
|
||||
public ButtonGrid()
|
||||
{
|
||||
this.ButtonsForm = new ButtonForm();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public ButtonGrid(eKeyboardType type) : this()
|
||||
{
|
||||
m_eKeyboardType = type;
|
||||
}
|
||||
|
||||
|
||||
public ButtonGrid(ButtonForm form)
|
||||
{
|
||||
this.ButtonsForm = form;
|
||||
}
|
||||
|
||||
|
||||
public event EventHandler<ButtonClickedEventArgs> ButtonClicked
|
||||
{
|
||||
add
|
||||
{
|
||||
this.Events.AddHandler(__evButtonClicked, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.Events.RemoveHandler(__evButtonClicked, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnButtonClicked(ButtonClickedEventArgs e)
|
||||
{
|
||||
(this.Events[__evButtonClicked] as EventHandler<ButtonClickedEventArgs>)?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public async override Task Load(MessageResult result)
|
||||
{
|
||||
if (this.KeyboardType != eKeyboardType.ReplyKeyboard)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
var button = ButtonsForm.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||
|
||||
if (button == null)
|
||||
return;
|
||||
|
||||
OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeletePreviousMessage)
|
||||
await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
}
|
||||
|
||||
public async override Task Action(MessageResult result, string value = null)
|
||||
{
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
await result.ConfirmAction(this.ConfirmationText ?? "");
|
||||
|
||||
//Find clicked button depending on Text or Value (depending on markup type)
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
var button = ButtonsForm.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||
|
||||
if (button == null)
|
||||
return;
|
||||
|
||||
OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async override Task Render(MessageResult result)
|
||||
{
|
||||
if (!this.RenderNecessary)
|
||||
return;
|
||||
|
||||
|
||||
switch (m_eKeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
if (ButtonsForm.Rows > Constants.Telegram.MaxInlineKeyBoardRows)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = ButtonsForm.Rows, Maximum = Constants.Telegram.MaxInlineKeyBoardRows };
|
||||
}
|
||||
|
||||
if (ButtonsForm.Cols > Constants.Telegram.MaxInlineKeyBoardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = ButtonsForm.Rows, Maximum = Constants.Telegram.MaxInlineKeyBoardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (ButtonsForm.Rows > Constants.Telegram.MaxReplyKeyboardRows)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = ButtonsForm.Rows, Maximum = Constants.Telegram.MaxReplyKeyboardRows };
|
||||
}
|
||||
|
||||
if (ButtonsForm.Cols > Constants.Telegram.MaxReplyKeyboardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = ButtonsForm.Rows, Maximum = Constants.Telegram.MaxReplyKeyboardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Message m = null;
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
//Reply Keyboard could only be updated with a new keyboard.
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
if (this.ButtonsForm.Count == 0)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
this.MessageId = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.DeletePreviousMessage)
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
var rkm = (ReplyKeyboardMarkup)this.ButtonsForm;
|
||||
rkm.ResizeKeyboard = this.ResizeKeyboard;
|
||||
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
|
||||
m = await this.Device.Send(this.Title, rkm, disableNotification: true, parseMode: MessageParseMode);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
m = await this.Device.Edit(this.MessageId.Value, this.Title, (InlineKeyboardMarkup)this.ButtonsForm);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
var rkm = (ReplyKeyboardMarkup)this.ButtonsForm;
|
||||
rkm.ResizeKeyboard = this.ResizeKeyboard;
|
||||
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
|
||||
m = await this.Device.Send(this.Title, rkm, disableNotification: true, parseMode: MessageParseMode);
|
||||
break;
|
||||
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
m = await this.Device.Send(this.Title, (InlineKeyboardMarkup)this.ButtonsForm, disableNotification: true, parseMode: MessageParseMode);
|
||||
break;
|
||||
}
|
||||
|
||||
if (m != null)
|
||||
{
|
||||
this.MessageId = m.MessageId;
|
||||
}
|
||||
}
|
||||
|
||||
this.RenderNecessary = false;
|
||||
|
||||
}
|
||||
|
||||
public override async Task Hidden(bool FormClose)
|
||||
{
|
||||
//Prepare for opening Modal, and comming back
|
||||
if (!FormClose)
|
||||
{
|
||||
this.Updated();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tells the control that it has been updated.
|
||||
/// </summary>
|
||||
public void Updated()
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
}
|
||||
|
||||
public async override Task Cleanup()
|
||||
{
|
||||
if (this.MessageId == null)
|
||||
return;
|
||||
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (this.HideKeyboardOnCleanup)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
}
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,738 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.SymbolStore;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Datasources;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Exceptions;
|
||||
using TelegramBotBase.Form;
|
||||
using static TelegramBotBase.Base.Async;
|
||||
|
||||
namespace TelegramBotBase.Controls.Hybrid
|
||||
{
|
||||
public class ButtonGrid : Base.ControlBase
|
||||
{
|
||||
|
||||
public String Title { get; set; } = Localizations.Default.Language["ButtonGrid_Title"];
|
||||
|
||||
public String ConfirmationText { get; set; } = "";
|
||||
|
||||
private bool RenderNecessary = true;
|
||||
|
||||
private static readonly object __evButtonClicked = new object();
|
||||
|
||||
private readonly EventHandlerList Events = new EventHandlerList();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Obsolete("This property is obsolete. Please use the DataSource property instead.")]
|
||||
public ButtonForm ButtonsForm
|
||||
{
|
||||
get
|
||||
{
|
||||
return DataSource.ButtonForm;
|
||||
}
|
||||
set
|
||||
{
|
||||
DataSource = new ButtonFormDataSource(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data source of the items.
|
||||
/// </summary>
|
||||
public ButtonFormDataSource DataSource { get; set; }
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
|
||||
/// Source: https://core.telegram.org/bots/api#replykeyboardmarkup
|
||||
/// </summary>
|
||||
public bool ResizeKeyboard { get; set; } = false;
|
||||
|
||||
public bool OneTimeKeyboard { get; set; } = false;
|
||||
|
||||
public bool HideKeyboardOnCleanup { get; set; } = true;
|
||||
|
||||
public bool DeletePreviousMessage { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Removes the reply message from a user.
|
||||
/// </summary>
|
||||
public bool DeleteReplyMessage { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Parsemode of the message.
|
||||
/// </summary>
|
||||
public ParseMode MessageParseMode { get; set; } = ParseMode.Markdown;
|
||||
|
||||
/// <summary>
|
||||
/// Enables automatic paging of buttons when the amount of rows is exceeding the limits.
|
||||
/// </summary>
|
||||
public bool EnablePaging { get; set; } = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Enabled a search function.
|
||||
/// </summary>
|
||||
public bool EnableSearch { get; set; } = false;
|
||||
|
||||
public String SearchQuery { get; set; }
|
||||
|
||||
public eNavigationBarVisibility NavigationBarVisibility { get; set; } = eNavigationBarVisibility.always;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current page
|
||||
/// </summary>
|
||||
public int CurrentPageIndex { get; set; } = 0;
|
||||
|
||||
public String PreviousPageLabel = Localizations.Default.Language["ButtonGrid_PreviousPage"];
|
||||
|
||||
public String NextPageLabel = Localizations.Default.Language["ButtonGrid_NextPage"];
|
||||
|
||||
public String NoItemsLabel = Localizations.Default.Language["ButtonGrid_NoItems"];
|
||||
|
||||
public String SearchLabel = Localizations.Default.Language["ButtonGrid_SearchFeature"];
|
||||
|
||||
/// <summary>
|
||||
/// Layout of the buttons which should be displayed always on top.
|
||||
/// </summary>
|
||||
public ButtonRow HeadLayoutButtonRow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Layout of columns which should be displayed below the header
|
||||
/// </summary>
|
||||
public ButtonRow SubHeadLayoutButtonRow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines which type of Button Keyboard should be rendered.
|
||||
/// </summary>
|
||||
public eKeyboardType KeyboardType
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_eKeyboardType;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_eKeyboardType != value)
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
|
||||
Cleanup().Wait();
|
||||
|
||||
m_eKeyboardType = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private eKeyboardType m_eKeyboardType = eKeyboardType.ReplyKeyboard;
|
||||
|
||||
public ButtonGrid()
|
||||
{
|
||||
this.DataSource = new ButtonFormDataSource();
|
||||
|
||||
}
|
||||
|
||||
public ButtonGrid(eKeyboardType type) : this()
|
||||
{
|
||||
m_eKeyboardType = type;
|
||||
}
|
||||
|
||||
|
||||
public ButtonGrid(ButtonForm form)
|
||||
{
|
||||
this.DataSource = new ButtonFormDataSource(form);
|
||||
}
|
||||
|
||||
|
||||
public event AsyncEventHandler<ButtonClickedEventArgs> ButtonClicked
|
||||
{
|
||||
add
|
||||
{
|
||||
this.Events.AddHandler(__evButtonClicked, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.Events.RemoveHandler(__evButtonClicked, value);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnButtonClicked(ButtonClickedEventArgs e)
|
||||
{
|
||||
var handler = this.Events[__evButtonClicked]?.GetInvocationList().Cast<AsyncEventHandler<ButtonClickedEventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<ButtonClickedEventArgs>(h, this, e);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
this.Device.MessageDeleted += Device_MessageDeleted;
|
||||
}
|
||||
|
||||
private void Device_MessageDeleted(object sender, MessageDeletedEventArgs e)
|
||||
{
|
||||
if (this.MessageId == null)
|
||||
return;
|
||||
|
||||
if (e.MessageId != this.MessageId)
|
||||
return;
|
||||
|
||||
this.MessageId = null;
|
||||
}
|
||||
|
||||
public async override Task Load(MessageResult result)
|
||||
{
|
||||
if (this.KeyboardType != eKeyboardType.ReplyKeyboard)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
if (result.MessageText == null || result.MessageText == "")
|
||||
return;
|
||||
|
||||
var matches = new List<ButtonRow>();
|
||||
ButtonRow match = null;
|
||||
int index = -1;
|
||||
|
||||
if (HeadLayoutButtonRow?.Matches(result.MessageText) ?? false)
|
||||
{
|
||||
match = HeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
if (SubHeadLayoutButtonRow?.Matches(result.MessageText) ?? false)
|
||||
{
|
||||
match = SubHeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
var br = DataSource.FindRow(result.MessageText);
|
||||
if (br != null)
|
||||
{
|
||||
match = br.Item1;
|
||||
index = br.Item2;
|
||||
}
|
||||
|
||||
|
||||
//var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||
|
||||
// bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
|
||||
//var index = bf.FindRowByButton(button);
|
||||
|
||||
check:
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeleteReplyMessage)
|
||||
await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
if (match != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.MessageText), index, match));
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (result.MessageText == PreviousPageLabel)
|
||||
{
|
||||
if (this.CurrentPageIndex > 0)
|
||||
this.CurrentPageIndex--;
|
||||
|
||||
this.Updated();
|
||||
}
|
||||
else if (result.MessageText == NextPageLabel)
|
||||
{
|
||||
if (this.CurrentPageIndex < this.PageCount - 1)
|
||||
this.CurrentPageIndex++;
|
||||
|
||||
this.Updated();
|
||||
}
|
||||
else if (this.EnableSearch)
|
||||
{
|
||||
if (result.MessageText.StartsWith("🔍"))
|
||||
{
|
||||
//Sent note about searching
|
||||
if (this.SearchQuery == null)
|
||||
{
|
||||
await this.Device.Send(this.SearchLabel);
|
||||
}
|
||||
|
||||
this.SearchQuery = null;
|
||||
this.Updated();
|
||||
return;
|
||||
}
|
||||
|
||||
this.SearchQuery = result.MessageText;
|
||||
|
||||
if (this.SearchQuery != null && this.SearchQuery != "")
|
||||
{
|
||||
this.CurrentPageIndex = 0;
|
||||
this.Updated();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async override Task Action(MessageResult result, string value = null)
|
||||
{
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
//Find clicked button depending on Text or Value (depending on markup type)
|
||||
if (this.KeyboardType != eKeyboardType.InlineKeyBoard)
|
||||
return;
|
||||
|
||||
await result.ConfirmAction(this.ConfirmationText ?? "");
|
||||
|
||||
ButtonRow match = null;
|
||||
int index = -1;
|
||||
|
||||
if (HeadLayoutButtonRow?.Matches(result.RawData, false) ?? false)
|
||||
{
|
||||
match = HeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
if (SubHeadLayoutButtonRow?.Matches(result.RawData, false) ?? false)
|
||||
{
|
||||
match = SubHeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
var br = DataSource.FindRow(result.RawData, false);
|
||||
if (br != null)
|
||||
{
|
||||
match = br.Item1;
|
||||
index = br.Item2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//var bf = DataSource.ButtonForm;
|
||||
|
||||
//var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
// ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||
|
||||
//var index = bf.FindRowByButton(button);
|
||||
|
||||
check:
|
||||
if (match != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.RawData, false), index, match));
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (result.RawData)
|
||||
{
|
||||
case "$previous$":
|
||||
|
||||
if (this.CurrentPageIndex > 0)
|
||||
this.CurrentPageIndex--;
|
||||
|
||||
this.Updated();
|
||||
|
||||
break;
|
||||
case "$next$":
|
||||
|
||||
if (this.CurrentPageIndex < this.PageCount - 1)
|
||||
this.CurrentPageIndex++;
|
||||
|
||||
this.Updated();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method checks of the amount of buttons
|
||||
/// </summary>
|
||||
private void CheckGrid()
|
||||
{
|
||||
switch (m_eKeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !this.EnablePaging)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows };
|
||||
}
|
||||
|
||||
if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !this.EnablePaging)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows };
|
||||
}
|
||||
|
||||
if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async override Task Render(MessageResult result)
|
||||
{
|
||||
if (!this.RenderNecessary)
|
||||
return;
|
||||
|
||||
//Check for rows and column limits
|
||||
CheckGrid();
|
||||
|
||||
this.RenderNecessary = false;
|
||||
|
||||
ButtonForm form = this.DataSource.PickItems(CurrentPageIndex * ItemRowsPerPage, ItemRowsPerPage, (this.EnableSearch ? this.SearchQuery : null));
|
||||
|
||||
|
||||
//if (this.EnableSearch && this.SearchQuery != null && this.SearchQuery != "")
|
||||
//{
|
||||
// form = form.FilterDuplicate(this.SearchQuery, true);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// form = form.Duplicate();
|
||||
//}
|
||||
|
||||
if (this.EnablePaging)
|
||||
{
|
||||
IntegratePagingView(form);
|
||||
}
|
||||
|
||||
if (this.HeadLayoutButtonRow != null && HeadLayoutButtonRow.Count > 0)
|
||||
{
|
||||
form.InsertButtonRow(0, this.HeadLayoutButtonRow);
|
||||
}
|
||||
|
||||
if (this.SubHeadLayoutButtonRow != null && SubHeadLayoutButtonRow.Count > 0)
|
||||
{
|
||||
if (this.IsNavigationBarVisible)
|
||||
{
|
||||
form.InsertButtonRow(2, this.SubHeadLayoutButtonRow);
|
||||
}
|
||||
else
|
||||
{
|
||||
form.InsertButtonRow(1, this.SubHeadLayoutButtonRow);
|
||||
}
|
||||
}
|
||||
|
||||
Message m = null;
|
||||
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
//Reply Keyboard could only be updated with a new keyboard.
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
|
||||
if (form.Count == 0)
|
||||
{
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
this.MessageId = null;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//if (this.MessageId != null)
|
||||
//{
|
||||
// if (form.Count == 0)
|
||||
// {
|
||||
// await this.Device.HideReplyKeyboard();
|
||||
// this.MessageId = null;
|
||||
// return;
|
||||
// }
|
||||
//}
|
||||
|
||||
//if (form.Count == 0)
|
||||
// return;
|
||||
|
||||
|
||||
var rkm = (ReplyKeyboardMarkup)form;
|
||||
rkm.ResizeKeyboard = this.ResizeKeyboard;
|
||||
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
|
||||
m = await this.Device.Send(this.Title, rkm, disableNotification: true, parseMode: MessageParseMode, MarkdownV2AutoEscape: false);
|
||||
|
||||
//Prevent flicker of keyboard
|
||||
if (this.DeletePreviousMessage && this.MessageId != null)
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
break;
|
||||
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
//Try to edit message if message id is available
|
||||
//When the returned message is null then the message has been already deleted, resend it
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
m = await this.Device.Edit(this.MessageId.Value, this.Title, (InlineKeyboardMarkup)form);
|
||||
if (m != null)
|
||||
{
|
||||
this.MessageId = m.MessageId;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//When no message id is available or it has been deleted due the use of AutoCleanForm re-render automatically
|
||||
m = await this.Device.Send(this.Title, (InlineKeyboardMarkup)form, disableNotification: true, parseMode: MessageParseMode, MarkdownV2AutoEscape: false);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (m != null)
|
||||
{
|
||||
this.MessageId = m.MessageId;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void IntegratePagingView(ButtonForm dataForm)
|
||||
{
|
||||
//No Items
|
||||
if (dataForm.Rows == 0)
|
||||
{
|
||||
dataForm.AddButtonRow(new ButtonBase(NoItemsLabel, "$"));
|
||||
}
|
||||
|
||||
if (this.IsNavigationBarVisible)
|
||||
{
|
||||
//🔍
|
||||
ButtonRow row = new ButtonRow();
|
||||
row.Add(new ButtonBase(PreviousPageLabel, "$previous$"));
|
||||
row.Add(new ButtonBase(String.Format(Localizations.Default.Language["ButtonGrid_CurrentPage"], this.CurrentPageIndex + 1, this.PageCount), "$site$"));
|
||||
row.Add(new ButtonBase(NextPageLabel, "$next$"));
|
||||
|
||||
if (this.EnableSearch)
|
||||
{
|
||||
row.Insert(2, new ButtonBase("🔍 " + (this.SearchQuery ?? ""), "$search$"));
|
||||
}
|
||||
|
||||
dataForm.InsertButtonRow(0, row);
|
||||
|
||||
dataForm.AddButtonRow(row);
|
||||
}
|
||||
}
|
||||
|
||||
public bool PagingNecessary
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.KeyboardType == eKeyboardType.InlineKeyBoard && TotalRows > Constants.Telegram.MaxInlineKeyBoardRows)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.KeyboardType == eKeyboardType.ReplyKeyboard && TotalRows > Constants.Telegram.MaxReplyKeyboardRows)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsNavigationBarVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.NavigationBarVisibility == eNavigationBarVisibility.always | (this.NavigationBarVisibility == eNavigationBarVisibility.auto && PagingNecessary))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum number of rows
|
||||
/// </summary>
|
||||
public int MaximumRow
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
return Constants.Telegram.MaxInlineKeyBoardRows;
|
||||
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
return Constants.Telegram.MaxReplyKeyboardRows;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of all rows (layout + navigation + content);
|
||||
/// </summary>
|
||||
public int TotalRows
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.LayoutRows + DataSource.RowCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Contains the Number of Rows which are used by the layout.
|
||||
/// </summary>
|
||||
private int LayoutRows
|
||||
{
|
||||
get
|
||||
{
|
||||
int layoutRows = 0;
|
||||
|
||||
if (this.NavigationBarVisibility == eNavigationBarVisibility.always | this.NavigationBarVisibility == eNavigationBarVisibility.auto)
|
||||
layoutRows += 2;
|
||||
|
||||
if (this.HeadLayoutButtonRow != null && this.HeadLayoutButtonRow.Count > 0)
|
||||
layoutRows++;
|
||||
|
||||
if (this.SubHeadLayoutButtonRow != null && this.SubHeadLayoutButtonRow.Count > 0)
|
||||
layoutRows++;
|
||||
|
||||
return layoutRows;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of item rows per page.
|
||||
/// </summary>
|
||||
public int ItemRowsPerPage
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.MaximumRow - this.LayoutRows;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the number of pages.
|
||||
/// </summary>
|
||||
public int PageCount
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataSource.RowCount == 0)
|
||||
return 1;
|
||||
|
||||
//var bf = this.DataSource.PickAllItems(this.EnableSearch ? this.SearchQuery : null);
|
||||
|
||||
var max = this.DataSource.CalculateMax(this.EnableSearch ? this.SearchQuery : null);
|
||||
|
||||
//if (this.EnableSearch && this.SearchQuery != null && this.SearchQuery != "")
|
||||
//{
|
||||
// bf = bf.FilterDuplicate(this.SearchQuery);
|
||||
//}
|
||||
|
||||
if (max == 0)
|
||||
return 1;
|
||||
|
||||
return (int)Math.Ceiling((decimal)((decimal)max / (decimal)ItemRowsPerPage));
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task Hidden(bool FormClose)
|
||||
{
|
||||
//Prepare for opening Modal, and comming back
|
||||
if (!FormClose)
|
||||
{
|
||||
this.Updated();
|
||||
}
|
||||
else
|
||||
{
|
||||
//Remove event handler
|
||||
this.Device.MessageDeleted -= Device_MessageDeleted;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tells the control that it has been updated.
|
||||
/// </summary>
|
||||
public void Updated()
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
}
|
||||
|
||||
public async override Task Cleanup()
|
||||
{
|
||||
if (this.MessageId == null)
|
||||
return;
|
||||
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (this.HideKeyboardOnCleanup)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
}
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls.Hybrid
|
||||
{
|
||||
[DebuggerDisplay("{Count} columns")]
|
||||
public class ButtonRow
|
||||
{
|
||||
|
||||
List<ButtonBase> __buttons = new List<ButtonBase>();
|
||||
|
||||
public ButtonRow()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public ButtonRow(params ButtonBase[] buttons)
|
||||
{
|
||||
__buttons = buttons.ToList();
|
||||
}
|
||||
|
||||
|
||||
public ButtonBase this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return __buttons[index];
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return __buttons.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(ButtonBase button)
|
||||
{
|
||||
__buttons.Add(button);
|
||||
}
|
||||
|
||||
public void AddRange(ButtonBase button)
|
||||
{
|
||||
__buttons.Add(button);
|
||||
}
|
||||
|
||||
public void Insert(int index, ButtonBase button)
|
||||
{
|
||||
__buttons.Insert(index, button);
|
||||
}
|
||||
|
||||
public IEnumerator<ButtonBase> GetEnumerator()
|
||||
{
|
||||
return __buttons.GetEnumerator();
|
||||
}
|
||||
|
||||
public ButtonBase[] ToArray()
|
||||
{
|
||||
return __buttons.ToArray();
|
||||
}
|
||||
|
||||
public List<ButtonBase> ToList()
|
||||
{
|
||||
return __buttons.ToList();
|
||||
}
|
||||
|
||||
public bool Matches(String text, bool useText = true)
|
||||
{
|
||||
foreach (var b in __buttons)
|
||||
{
|
||||
if (useText && b.Text.Trim().Equals(text, StringComparison.InvariantCultureIgnoreCase))
|
||||
return true;
|
||||
|
||||
if (!useText && b.Value.Equals(text, StringComparison.InvariantCultureIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the button inside of the row which matches.
|
||||
/// </summary>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="useText"></param>
|
||||
/// <returns></returns>
|
||||
public ButtonBase GetButtonMatch(String text, bool useText = true)
|
||||
{
|
||||
foreach (var b in __buttons)
|
||||
{
|
||||
if (useText && b.Text.Trim().Equals(text, StringComparison.InvariantCultureIgnoreCase))
|
||||
return b;
|
||||
if (!useText && b.Value.Equals(text, StringComparison.InvariantCultureIgnoreCase))
|
||||
return b;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static implicit operator ButtonRow(List<ButtonBase> list)
|
||||
{
|
||||
return new ButtonRow() { __buttons = list };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,874 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.SymbolStore;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Datasources;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Exceptions;
|
||||
using TelegramBotBase.Form;
|
||||
using static TelegramBotBase.Base.Async;
|
||||
|
||||
namespace TelegramBotBase.Controls.Hybrid
|
||||
{
|
||||
public class CheckedButtonList : Base.ControlBase
|
||||
{
|
||||
|
||||
public String Title { get; set; } = Localizations.Default.Language["ButtonGrid_Title"];
|
||||
|
||||
public String ConfirmationText { get; set; } = "";
|
||||
|
||||
private bool RenderNecessary = true;
|
||||
|
||||
private static readonly object __evButtonClicked = new object();
|
||||
|
||||
private static readonly object __evCheckedChanged = new object();
|
||||
|
||||
private readonly EventHandlerList Events = new EventHandlerList();
|
||||
|
||||
[Obsolete("This property is obsolete. Please use the DataSource property instead.")]
|
||||
public ButtonForm ButtonsForm
|
||||
{
|
||||
get
|
||||
{
|
||||
return DataSource.ButtonForm;
|
||||
}
|
||||
set
|
||||
{
|
||||
DataSource = new ButtonFormDataSource(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data source of the items.
|
||||
/// </summary>
|
||||
public ButtonFormDataSource DataSource { get; set; }
|
||||
|
||||
List<int> CheckedRows { get; set; } = new List<int>();
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
|
||||
/// Source: https://core.telegram.org/bots/api#replykeyboardmarkup
|
||||
/// </summary>
|
||||
public bool ResizeKeyboard { get; set; } = false;
|
||||
|
||||
public bool OneTimeKeyboard { get; set; } = false;
|
||||
|
||||
public bool HideKeyboardOnCleanup { get; set; } = true;
|
||||
|
||||
public bool DeletePreviousMessage { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Removes the reply message from a user.
|
||||
/// </summary>
|
||||
public bool DeleteReplyMessage { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Parsemode of the message.
|
||||
/// </summary>
|
||||
public ParseMode MessageParseMode { get; set; } = ParseMode.Markdown;
|
||||
|
||||
/// <summary>
|
||||
/// Enables automatic paging of buttons when the amount of rows is exceeding the limits.
|
||||
/// </summary>
|
||||
public bool EnablePaging { get; set; } = false;
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// Enabled a search function.
|
||||
///// </summary>
|
||||
//public bool EnableSearch { get; set; } = false;
|
||||
|
||||
//public String SearchQuery { get; set; }
|
||||
|
||||
public eNavigationBarVisibility NavigationBarVisibility { get; set; } = eNavigationBarVisibility.always;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current page
|
||||
/// </summary>
|
||||
public int CurrentPageIndex { get; set; } = 0;
|
||||
|
||||
public String PreviousPageLabel = Localizations.Default.Language["ButtonGrid_PreviousPage"];
|
||||
|
||||
public String NextPageLabel = Localizations.Default.Language["ButtonGrid_NextPage"];
|
||||
|
||||
public String NoItemsLabel = Localizations.Default.Language["ButtonGrid_NoItems"];
|
||||
|
||||
//public String SearchLabel = Localizations.Default.Language["ButtonGrid_SearchFeature"];
|
||||
|
||||
public String CheckedIconLabel { get; set; } = "✅";
|
||||
|
||||
public String UncheckedIconLabel { get; set; } = "◻️";
|
||||
|
||||
/// <summary>
|
||||
/// Layout of the buttons which should be displayed always on top.
|
||||
/// </summary>
|
||||
public ButtonRow HeadLayoutButtonRow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Layout of columns which should be displayed below the header
|
||||
/// </summary>
|
||||
public ButtonRow SubHeadLayoutButtonRow { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines which type of Button Keyboard should be rendered.
|
||||
/// </summary>
|
||||
public eKeyboardType KeyboardType
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_eKeyboardType;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_eKeyboardType != value)
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
|
||||
Cleanup().Wait();
|
||||
|
||||
m_eKeyboardType = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private eKeyboardType m_eKeyboardType = eKeyboardType.ReplyKeyboard;
|
||||
|
||||
public CheckedButtonList()
|
||||
{
|
||||
this.DataSource = new ButtonFormDataSource();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public CheckedButtonList(eKeyboardType type) : this()
|
||||
{
|
||||
m_eKeyboardType = type;
|
||||
}
|
||||
|
||||
|
||||
public CheckedButtonList(ButtonForm form)
|
||||
{
|
||||
this.DataSource = new ButtonFormDataSource(form);
|
||||
}
|
||||
|
||||
public event AsyncEventHandler<ButtonClickedEventArgs> ButtonClicked
|
||||
{
|
||||
add
|
||||
{
|
||||
this.Events.AddHandler(__evButtonClicked, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.Events.RemoveHandler(__evButtonClicked, value);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnButtonClicked(ButtonClickedEventArgs e)
|
||||
{
|
||||
var handler = this.Events[__evButtonClicked]?.GetInvocationList().Cast<AsyncEventHandler<ButtonClickedEventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<ButtonClickedEventArgs>(h, this, e);
|
||||
}
|
||||
}
|
||||
|
||||
public event AsyncEventHandler<CheckedChangedEventArgs> CheckedChanged
|
||||
{
|
||||
add
|
||||
{
|
||||
this.Events.AddHandler(__evCheckedChanged, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.Events.RemoveHandler(__evCheckedChanged, value);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnCheckedChanged(CheckedChangedEventArgs e)
|
||||
{
|
||||
var handler = this.Events[__evCheckedChanged]?.GetInvocationList().Cast<AsyncEventHandler<CheckedChangedEventArgs>>();
|
||||
if (handler == null)
|
||||
return;
|
||||
|
||||
foreach (var h in handler)
|
||||
{
|
||||
await Async.InvokeAllAsync<CheckedChangedEventArgs>(h, this, e);
|
||||
}
|
||||
}
|
||||
|
||||
public async override Task Load(MessageResult result)
|
||||
{
|
||||
if (this.KeyboardType != eKeyboardType.ReplyKeyboard)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
if (result.MessageText == null || result.MessageText == "")
|
||||
return;
|
||||
|
||||
var matches = new List<ButtonRow>();
|
||||
ButtonRow match = null;
|
||||
int index = -1;
|
||||
|
||||
if (HeadLayoutButtonRow?.Matches(result.MessageText) ?? false)
|
||||
{
|
||||
match = HeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
if (SubHeadLayoutButtonRow?.Matches(result.MessageText) ?? false)
|
||||
{
|
||||
match = SubHeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
var br = DataSource.FindRow(result.MessageText);
|
||||
if (br != null)
|
||||
{
|
||||
match = br.Item1;
|
||||
index = br.Item2;
|
||||
}
|
||||
|
||||
|
||||
//var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||
|
||||
// bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||
|
||||
//var index = bf.FindRowByButton(button);
|
||||
|
||||
|
||||
|
||||
check:
|
||||
|
||||
|
||||
//Remove button click message
|
||||
if (this.DeleteReplyMessage)
|
||||
await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
if (match == null)
|
||||
{
|
||||
if (result.MessageText == PreviousPageLabel)
|
||||
{
|
||||
if (this.CurrentPageIndex > 0)
|
||||
this.CurrentPageIndex--;
|
||||
|
||||
this.Updated();
|
||||
}
|
||||
else if (result.MessageText == NextPageLabel)
|
||||
{
|
||||
if (this.CurrentPageIndex < this.PageCount - 1)
|
||||
this.CurrentPageIndex++;
|
||||
|
||||
this.Updated();
|
||||
}
|
||||
else if (result.MessageText.EndsWith(CheckedIconLabel))
|
||||
{
|
||||
var s = result.MessageText.Split(' ', '.');
|
||||
index = int.Parse(s[0]) - 1;
|
||||
|
||||
if (!this.CheckedRows.Contains(index))
|
||||
return;
|
||||
|
||||
this.CheckedRows.Remove(index);
|
||||
|
||||
this.Updated();
|
||||
|
||||
await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, false));
|
||||
|
||||
}
|
||||
else if (result.MessageText.EndsWith(UncheckedIconLabel))
|
||||
{
|
||||
var s = result.MessageText.Split(' ', '.');
|
||||
index = int.Parse(s[0]) - 1;
|
||||
|
||||
if (this.CheckedRows.Contains(index))
|
||||
return;
|
||||
|
||||
|
||||
this.CheckedRows.Add(index);
|
||||
|
||||
this.Updated();
|
||||
|
||||
await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, true));
|
||||
|
||||
}
|
||||
//else if (this.EnableSearch)
|
||||
//{
|
||||
// if (result.MessageText.StartsWith("🔍"))
|
||||
// {
|
||||
// //Sent note about searching
|
||||
// if (this.SearchQuery == null)
|
||||
// {
|
||||
// await this.Device.Send(this.SearchLabel);
|
||||
// }
|
||||
|
||||
// this.SearchQuery = null;
|
||||
// this.Updated();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// this.SearchQuery = result.MessageText;
|
||||
|
||||
// if (this.SearchQuery != null && this.SearchQuery != "")
|
||||
// {
|
||||
// this.CurrentPageIndex = 0;
|
||||
// this.Updated();
|
||||
// }
|
||||
|
||||
//}
|
||||
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.MessageText), index, match));
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
//await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
////Remove button click message
|
||||
//if (this.DeletePreviousMessage)
|
||||
// await Device.DeleteMessage(result.MessageId);
|
||||
|
||||
//result.Handled = true;
|
||||
|
||||
}
|
||||
|
||||
public async override Task Action(MessageResult result, string value = null)
|
||||
{
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
if (!result.IsFirstHandler)
|
||||
return;
|
||||
|
||||
//Find clicked button depending on Text or Value (depending on markup type)
|
||||
if (this.KeyboardType != eKeyboardType.InlineKeyBoard)
|
||||
return;
|
||||
|
||||
await result.ConfirmAction(this.ConfirmationText ?? "");
|
||||
|
||||
ButtonRow match = null;
|
||||
int index = -1;
|
||||
|
||||
if (HeadLayoutButtonRow?.Matches(result.RawData, false) ?? false)
|
||||
{
|
||||
match = HeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
if (SubHeadLayoutButtonRow?.Matches(result.RawData, false) ?? false)
|
||||
{
|
||||
match = SubHeadLayoutButtonRow;
|
||||
goto check;
|
||||
}
|
||||
|
||||
var br = DataSource.FindRow(result.RawData, false);
|
||||
if (br != null)
|
||||
{
|
||||
match = br.Item1;
|
||||
index = br.Item2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//var bf = DataSource.ButtonForm;
|
||||
|
||||
//var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||
// ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||
|
||||
//var index = bf.FindRowByButton(button);
|
||||
|
||||
check:
|
||||
if (match != null)
|
||||
{
|
||||
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.RawData, false), index, match));
|
||||
|
||||
result.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
//if (button != null)
|
||||
//{
|
||||
// await OnButtonClicked(new ButtonClickedEventArgs(button, index));
|
||||
|
||||
// result.Handled = true;
|
||||
// return;
|
||||
//}
|
||||
|
||||
switch (result.RawData)
|
||||
{
|
||||
case "$previous$":
|
||||
|
||||
if (this.CurrentPageIndex > 0)
|
||||
this.CurrentPageIndex--;
|
||||
|
||||
this.Updated();
|
||||
|
||||
break;
|
||||
case "$next$":
|
||||
|
||||
if (this.CurrentPageIndex < this.PageCount - 1)
|
||||
this.CurrentPageIndex++;
|
||||
|
||||
this.Updated();
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
var s = result.RawData.Split('$');
|
||||
|
||||
|
||||
switch (s[0])
|
||||
{
|
||||
case "check":
|
||||
|
||||
index = int.Parse(s[1]);
|
||||
|
||||
if (!this.CheckedRows.Contains(index))
|
||||
{
|
||||
this.CheckedRows.Add(index);
|
||||
|
||||
this.Updated();
|
||||
|
||||
await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, true));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "uncheck":
|
||||
|
||||
index = int.Parse(s[1]);
|
||||
|
||||
if (this.CheckedRows.Contains(index))
|
||||
{
|
||||
this.CheckedRows.Remove(index);
|
||||
|
||||
this.Updated();
|
||||
|
||||
await OnCheckedChanged(new CheckedChangedEventArgs(ButtonsForm[index], index, false));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method checks of the amount of buttons
|
||||
/// </summary>
|
||||
private void CheckGrid()
|
||||
{
|
||||
switch (m_eKeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !this.EnablePaging)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows };
|
||||
}
|
||||
|
||||
if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !this.EnablePaging)
|
||||
{
|
||||
throw new MaximumRowsReachedException() { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows };
|
||||
}
|
||||
|
||||
if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols)
|
||||
{
|
||||
throw new MaximumColsException() { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols };
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async override Task Render(MessageResult result)
|
||||
{
|
||||
if (!this.RenderNecessary)
|
||||
return;
|
||||
|
||||
//Check for rows and column limits
|
||||
CheckGrid();
|
||||
|
||||
this.RenderNecessary = false;
|
||||
|
||||
Message m = null;
|
||||
|
||||
ButtonForm form = this.DataSource.PickItems(CurrentPageIndex * ItemRowsPerPage, ItemRowsPerPage, null);
|
||||
|
||||
//if (this.EnableSearch && this.SearchQuery != null && this.SearchQuery != "")
|
||||
//{
|
||||
// form = form.FilterDuplicate(this.SearchQuery, true);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
//form = form.Duplicate();
|
||||
//}
|
||||
|
||||
if (this.EnablePaging)
|
||||
{
|
||||
IntegratePagingView(form);
|
||||
}
|
||||
else
|
||||
{
|
||||
form = PrepareCheckableLayout(form);
|
||||
}
|
||||
|
||||
if (this.HeadLayoutButtonRow != null && HeadLayoutButtonRow.Count > 0)
|
||||
{
|
||||
form.InsertButtonRow(0, this.HeadLayoutButtonRow.ToArray());
|
||||
}
|
||||
|
||||
if (this.SubHeadLayoutButtonRow != null && SubHeadLayoutButtonRow.Count > 0)
|
||||
{
|
||||
if (this.IsNavigationBarVisible)
|
||||
{
|
||||
form.InsertButtonRow(2, this.SubHeadLayoutButtonRow.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
form.InsertButtonRow(1, this.SubHeadLayoutButtonRow.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
//Reply Keyboard could only be updated with a new keyboard.
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (form.Count == 0)
|
||||
{
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
this.MessageId = null;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//if (form.Count == 0)
|
||||
// return;
|
||||
|
||||
|
||||
var rkm = (ReplyKeyboardMarkup)form;
|
||||
rkm.ResizeKeyboard = this.ResizeKeyboard;
|
||||
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
|
||||
m = await this.Device.Send(this.Title, rkm, disableNotification: true, parseMode: MessageParseMode, MarkdownV2AutoEscape: false);
|
||||
|
||||
//Prevent flicker of keyboard
|
||||
if (this.DeletePreviousMessage && this.MessageId != null)
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
break;
|
||||
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
m = await this.Device.Edit(this.MessageId.Value, this.Title, (InlineKeyboardMarkup)form);
|
||||
}
|
||||
else
|
||||
{
|
||||
m = await this.Device.Send(this.Title, (InlineKeyboardMarkup)form, disableNotification: true, parseMode: MessageParseMode, MarkdownV2AutoEscape: false);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (m != null)
|
||||
{
|
||||
this.MessageId = m.MessageId;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void IntegratePagingView(ButtonForm dataForm)
|
||||
{
|
||||
//No Items
|
||||
if (dataForm.Rows == 0)
|
||||
{
|
||||
dataForm.AddButtonRow(new ButtonBase(NoItemsLabel, "$"));
|
||||
}
|
||||
|
||||
ButtonForm bf = new ButtonForm();
|
||||
|
||||
bf = PrepareCheckableLayout(dataForm);
|
||||
|
||||
|
||||
if (this.IsNavigationBarVisible)
|
||||
{
|
||||
//🔍
|
||||
ButtonRow row = new ButtonRow();
|
||||
row.Add(new ButtonBase(PreviousPageLabel, "$previous$"));
|
||||
row.Add(new ButtonBase(String.Format(Localizations.Default.Language["ButtonGrid_CurrentPage"], this.CurrentPageIndex + 1, this.PageCount), "$site$"));
|
||||
row.Add(new ButtonBase(NextPageLabel, "$next$"));
|
||||
|
||||
|
||||
dataForm.InsertButtonRow(0, row);
|
||||
|
||||
dataForm.AddButtonRow(row);
|
||||
}
|
||||
}
|
||||
|
||||
private ButtonForm PrepareCheckableLayout(ButtonForm dataForm)
|
||||
{
|
||||
var bf = new ButtonForm();
|
||||
for (int i = 0; i < dataForm.Rows; i++)
|
||||
{
|
||||
int it = (this.CurrentPageIndex * (this.MaximumRow - LayoutRows)) + i;
|
||||
|
||||
//if (it > dataForm.Rows - 1)
|
||||
// break;
|
||||
|
||||
var r = dataForm[i];
|
||||
|
||||
String s = CheckedRows.Contains(it) ? this.CheckedIconLabel : this.UncheckedIconLabel;
|
||||
|
||||
//On reply keyboards we need a unique text.
|
||||
if (this.KeyboardType == eKeyboardType.ReplyKeyboard)
|
||||
{
|
||||
s = $"{it + 1}. " + s;
|
||||
}
|
||||
|
||||
if (CheckedRows.Contains(it))
|
||||
{
|
||||
r.Insert(0, new ButtonBase(s, "uncheck$" + it.ToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
r.Insert(0, new ButtonBase(s, "check$" + it.ToString()));
|
||||
}
|
||||
|
||||
bf.AddButtonRow(r);
|
||||
}
|
||||
|
||||
return bf;
|
||||
}
|
||||
|
||||
public bool PagingNecessary
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.KeyboardType == eKeyboardType.InlineKeyBoard && TotalRows > Constants.Telegram.MaxInlineKeyBoardRows)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.KeyboardType == eKeyboardType.ReplyKeyboard && TotalRows > Constants.Telegram.MaxReplyKeyboardRows)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsNavigationBarVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.NavigationBarVisibility == eNavigationBarVisibility.always | (this.NavigationBarVisibility == eNavigationBarVisibility.auto && PagingNecessary))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum number of rows
|
||||
/// </summary>
|
||||
public int MaximumRow
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
return Constants.Telegram.MaxInlineKeyBoardRows;
|
||||
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
return Constants.Telegram.MaxReplyKeyboardRows;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of all rows (layout + navigation + content);
|
||||
/// </summary>
|
||||
public int TotalRows
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.LayoutRows + DataSource.RowCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Contains the Number of Rows which are used by the layout.
|
||||
/// </summary>
|
||||
private int LayoutRows
|
||||
{
|
||||
get
|
||||
{
|
||||
int layoutRows = 0;
|
||||
|
||||
if (this.NavigationBarVisibility == eNavigationBarVisibility.always | this.NavigationBarVisibility == eNavigationBarVisibility.auto)
|
||||
layoutRows += 2;
|
||||
|
||||
if (this.HeadLayoutButtonRow != null && this.HeadLayoutButtonRow.Count > 0)
|
||||
layoutRows++;
|
||||
|
||||
if (this.SubHeadLayoutButtonRow != null && this.SubHeadLayoutButtonRow.Count > 0)
|
||||
layoutRows++;
|
||||
|
||||
return layoutRows;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of item rows per page.
|
||||
/// </summary>
|
||||
public int ItemRowsPerPage
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.MaximumRow - this.LayoutRows;
|
||||
}
|
||||
}
|
||||
|
||||
public int PageCount
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataSource.RowCount == 0)
|
||||
return 1;
|
||||
|
||||
//var bf = this.DataSource.PickAllItems(this.EnableSearch ? this.SearchQuery : null);
|
||||
|
||||
var max = this.DataSource.RowCount;
|
||||
|
||||
//if (this.EnableSearch && this.SearchQuery != null && this.SearchQuery != "")
|
||||
//{
|
||||
// bf = bf.FilterDuplicate(this.SearchQuery);
|
||||
//}
|
||||
|
||||
if (max == 0)
|
||||
return 1;
|
||||
|
||||
return (int)Math.Ceiling((decimal)((decimal)max / (decimal)ItemRowsPerPage));
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task Hidden(bool FormClose)
|
||||
{
|
||||
//Prepare for opening Modal, and comming back
|
||||
if (!FormClose)
|
||||
{
|
||||
this.Updated();
|
||||
}
|
||||
}
|
||||
|
||||
public List<ButtonBase> CheckedItems
|
||||
{
|
||||
get
|
||||
{
|
||||
List<ButtonBase> lst = new List<ButtonBase>();
|
||||
|
||||
foreach (var c in CheckedRows)
|
||||
{
|
||||
lst.Add(this.ButtonsForm[c][0]);
|
||||
}
|
||||
|
||||
return lst;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Tells the control that it has been updated.
|
||||
/// </summary>
|
||||
public void Updated()
|
||||
{
|
||||
this.RenderNecessary = true;
|
||||
}
|
||||
|
||||
public async override Task Cleanup()
|
||||
{
|
||||
if (this.MessageId == null)
|
||||
return;
|
||||
|
||||
switch (this.KeyboardType)
|
||||
{
|
||||
case eKeyboardType.InlineKeyBoard:
|
||||
|
||||
await this.Device.DeleteMessage(this.MessageId.Value);
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
case eKeyboardType.ReplyKeyboard:
|
||||
|
||||
if (this.HideKeyboardOnCleanup)
|
||||
{
|
||||
await this.Device.HideReplyKeyboard();
|
||||
}
|
||||
|
||||
this.MessageId = null;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Args;
|
||||
using TelegramBotBase.Base;
|
||||
|
||||
namespace TelegramBotBase.Controls.Hybrid
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This Control is for having a basic form content switching control.
|
||||
/// </summary>
|
||||
public abstract class MultiView : Base.ControlBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Index of the current View.
|
||||
/// </summary>
|
||||
public int SelectedViewIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_iSelectedViewIndex;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_iSelectedViewIndex = value;
|
||||
|
||||
//Already rendered? Re-Render
|
||||
if (_Rendered)
|
||||
ForceRender().Wait();
|
||||
}
|
||||
}
|
||||
|
||||
private int m_iSelectedViewIndex = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Hold if the View has been rendered already.
|
||||
/// </summary>
|
||||
private bool _Rendered = false;
|
||||
|
||||
private List<int> Messages { get; set; }
|
||||
|
||||
|
||||
public MultiView()
|
||||
{
|
||||
Messages = new List<int>();
|
||||
}
|
||||
|
||||
|
||||
private void Device_MessageSent(object sender, MessageSentEventArgs e)
|
||||
{
|
||||
if (e.Origin == null || !e.Origin.IsSubclassOf(typeof(MultiView)))
|
||||
return;
|
||||
|
||||
this.Messages.Add(e.MessageId);
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
Device.MessageSent += Device_MessageSent;
|
||||
}
|
||||
|
||||
public override async Task Load(MessageResult result)
|
||||
{
|
||||
_Rendered = false;
|
||||
}
|
||||
|
||||
|
||||
public override async Task Render(MessageResult result)
|
||||
{
|
||||
//When already rendered, skip rendering
|
||||
if (_Rendered)
|
||||
return;
|
||||
|
||||
await CleanUpView();
|
||||
|
||||
await RenderView(new RenderViewEventArgs(this.SelectedViewIndex));
|
||||
|
||||
_Rendered = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Will get invoked on rendering the current controls view.
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
public virtual async Task RenderView(RenderViewEventArgs e)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
async Task CleanUpView()
|
||||
{
|
||||
|
||||
var tasks = new List<Task>();
|
||||
|
||||
foreach (var msg in this.Messages)
|
||||
{
|
||||
tasks.Add(this.Device.DeleteMessage(msg));
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
this.Messages.Clear();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces render of control contents.
|
||||
/// </summary>
|
||||
public async Task ForceRender()
|
||||
{
|
||||
await CleanUpView();
|
||||
|
||||
await RenderView(new RenderViewEventArgs(this.SelectedViewIndex));
|
||||
|
||||
_Rendered = true;
|
||||
}
|
||||
|
||||
public override async Task Cleanup()
|
||||
{
|
||||
Device.MessageSent -= Device_MessageSent;
|
||||
|
||||
await CleanUpView();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+19
-17
@@ -7,11 +7,11 @@ using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
using TelegramBotBase.Tools;
|
||||
using static TelegramBotBase.Tools.Arrays;
|
||||
using static TelegramBotBase.Tools.Time;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class CalendarPicker : Base.ControlBase
|
||||
{
|
||||
@@ -27,7 +27,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
private int? MessageId { get; set; }
|
||||
|
||||
public String Title { get; set; } = "Pick Date";
|
||||
public String Title { get; set; } = Localizations.Default.Language["CalendarPicker_Title"];
|
||||
|
||||
public eMonthPickerMode PickerMode { get; set; }
|
||||
|
||||
@@ -36,15 +36,17 @@ namespace TelegramBotBase.Controls
|
||||
public bool EnableMonthView { get; set; } = true;
|
||||
|
||||
public bool EnableYearView { get; set; } = true;
|
||||
|
||||
public CalendarPicker()
|
||||
|
||||
public CalendarPicker(CultureInfo culture)
|
||||
{
|
||||
this.SelectedDate = DateTime.Today;
|
||||
this.VisibleMonth = DateTime.Today;
|
||||
this.FirstDayOfWeek = DayOfWeek.Monday;
|
||||
this.Culture = new CultureInfo("de-de");
|
||||
this.Culture = culture;
|
||||
this.PickerMode = eMonthPickerMode.day;
|
||||
}
|
||||
|
||||
public CalendarPicker() : this(new CultureInfo("en-en")) { }
|
||||
|
||||
|
||||
|
||||
@@ -55,7 +57,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
switch (result.RawData)
|
||||
{
|
||||
case "next":
|
||||
case "$next$":
|
||||
|
||||
switch (this.PickerMode)
|
||||
{
|
||||
@@ -74,7 +76,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
|
||||
break;
|
||||
case "prev":
|
||||
case "$prev$":
|
||||
|
||||
switch (this.PickerMode)
|
||||
{
|
||||
@@ -93,7 +95,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
break;
|
||||
|
||||
case "monthtitle":
|
||||
case "$monthtitle$":
|
||||
|
||||
if (this.EnableMonthView)
|
||||
{
|
||||
@@ -102,7 +104,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
break;
|
||||
|
||||
case "yeartitle":
|
||||
case "$yeartitle$":
|
||||
|
||||
if (this.EnableYearView)
|
||||
{
|
||||
@@ -110,7 +112,7 @@ namespace TelegramBotBase.Controls
|
||||
}
|
||||
|
||||
break;
|
||||
case "yearstitle":
|
||||
case "$yearstitle$":
|
||||
|
||||
if (this.EnableMonthView)
|
||||
{
|
||||
@@ -124,13 +126,13 @@ namespace TelegramBotBase.Controls
|
||||
default:
|
||||
|
||||
int day = 0;
|
||||
if (result.RawData.StartsWith("d-") && int.TryParse(result.RawData.Split('-')[1], out day))
|
||||
if (result.RawData.StartsWith("d-") && TryParseDay(result.RawData.Split('-')[1], this.SelectedDate, out day))
|
||||
{
|
||||
this.SelectedDate = new DateTime(this.VisibleMonth.Year, this.VisibleMonth.Month, day);
|
||||
}
|
||||
|
||||
int month = 0;
|
||||
if (result.RawData.StartsWith("m-") && int.TryParse(result.RawData.Split('-')[1], out month))
|
||||
if (result.RawData.StartsWith("m-") && TryParseMonth(result.RawData.Split('-')[1], out month))
|
||||
{
|
||||
this.SelectedDate = new DateTime(this.VisibleMonth.Year, month, 1);
|
||||
this.VisibleMonth = this.SelectedDate;
|
||||
@@ -142,7 +144,7 @@ namespace TelegramBotBase.Controls
|
||||
}
|
||||
|
||||
int year = 0;
|
||||
if (result.RawData.StartsWith("y-") && int.TryParse(result.RawData.Split('-')[1], out year))
|
||||
if (result.RawData.StartsWith("y-") && TryParseYear(result.RawData.Split('-')[1], out year))
|
||||
{
|
||||
this.SelectedDate = new DateTime(year, SelectedDate.Month, SelectedDate.Day);
|
||||
this.VisibleMonth = this.SelectedDate;
|
||||
@@ -179,7 +181,7 @@ namespace TelegramBotBase.Controls
|
||||
string[] dayNamesNormal = this.Culture.DateTimeFormat.ShortestDayNames;
|
||||
string[] dayNamesShifted = Shift(dayNamesNormal, (int)this.FirstDayOfWeek);
|
||||
|
||||
bf.AddButtonRow(new ButtonBase("<<", "prev"), new ButtonBase(this.Culture.DateTimeFormat.MonthNames[month.Month - 1] + " " + month.Year.ToString(), "monthtitle"), new ButtonBase(">>", "next"));
|
||||
bf.AddButtonRow(new ButtonBase(Localizations.Default.Language["CalendarPicker_PreviousPage"], "$prev$"), new ButtonBase(this.Culture.DateTimeFormat.MonthNames[month.Month - 1] + " " + month.Year.ToString(), "$monthtitle$"), new ButtonBase(Localizations.Default.Language["CalendarPicker_NextPage"], "$next$"));
|
||||
|
||||
bf.AddButtonRow(dayNamesShifted.Select(a => new ButtonBase(a, a)).ToList());
|
||||
|
||||
@@ -223,7 +225,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
case eMonthPickerMode.month:
|
||||
|
||||
bf.AddButtonRow(new ButtonBase("<<", "prev"), new ButtonBase(this.VisibleMonth.Year.ToString("0000"), "yeartitle"), new ButtonBase(">>", "next"));
|
||||
bf.AddButtonRow(new ButtonBase(Localizations.Default.Language["CalendarPicker_PreviousPage"], "$prev$"), new ButtonBase(this.VisibleMonth.Year.ToString("0000"), "$yeartitle$"), new ButtonBase(Localizations.Default.Language["CalendarPicker_NextPage"], "$next$"));
|
||||
|
||||
var months = this.Culture.DateTimeFormat.MonthNames;
|
||||
|
||||
@@ -235,7 +237,7 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
case eMonthPickerMode.year:
|
||||
|
||||
bf.AddButtonRow(new ButtonBase("<<", "prev"), new ButtonBase("Year", "yearstitle"), new ButtonBase(">>", "next"));
|
||||
bf.AddButtonRow(new ButtonBase(Localizations.Default.Language["CalendarPicker_PreviousPage"], "$prev$"), new ButtonBase("Year", "$yearstitle$"), new ButtonBase(Localizations.Default.Language["CalendarPicker_NextPage"], "$next$"));
|
||||
|
||||
var starti = Math.Floor(this.VisibleMonth.Year / 10f) * 10;
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Enums;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class MonthPicker : CalendarPicker
|
||||
{
|
||||
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class MultiToggleButton : ControlBase
|
||||
{
|
||||
/// <summary>
|
||||
/// This contains the selected icon.
|
||||
/// </summary>
|
||||
public String SelectedIcon { get; set; } = Localizations.Default.Language["MultiToggleButton_SelectedIcon"];
|
||||
|
||||
/// <summary>
|
||||
/// This will appear on the ConfirmAction message (if not empty)
|
||||
/// </summary>
|
||||
public String ChangedString { get; set; } = Localizations.Default.Language["MultiToggleButton_Changed"];
|
||||
|
||||
/// <summary>
|
||||
/// This holds the title of the control.
|
||||
/// </summary>
|
||||
public String Title { get; set; } = Localizations.Default.Language["MultiToggleButton_Title"];
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
private bool RenderNecessary = true;
|
||||
|
||||
private static readonly object __evToggled = new object();
|
||||
|
||||
private readonly EventHandlerList Events = new EventHandlerList();
|
||||
|
||||
/// <summary>
|
||||
/// This will hold all options available.
|
||||
/// </summary>
|
||||
public List<ButtonBase> Options { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// This will set if an empty selection (null) is allowed.
|
||||
/// </summary>
|
||||
public bool AllowEmptySelection { get; set; } = true;
|
||||
|
||||
|
||||
public MultiToggleButton()
|
||||
{
|
||||
Options = new List<ButtonBase>();
|
||||
}
|
||||
|
||||
public event EventHandler Toggled
|
||||
{
|
||||
add
|
||||
{
|
||||
this.Events.AddHandler(__evToggled, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.Events.RemoveHandler(__evToggled, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnToggled(EventArgs e)
|
||||
{
|
||||
(this.Events[__evToggled] as EventHandler)?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public override async Task Action(MessageResult result, String value = null)
|
||||
{
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
await result.ConfirmAction(this.ChangedString);
|
||||
|
||||
switch (value ?? "unknown")
|
||||
{
|
||||
default:
|
||||
|
||||
var s = value.Split('$');
|
||||
|
||||
if (s[0] == "check" && s.Length > 1)
|
||||
{
|
||||
int index = 0;
|
||||
if (!int.TryParse(s[1], out index))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(SelectedOption== null || SelectedOption != this.Options[index])
|
||||
{
|
||||
this.SelectedOption = this.Options[index];
|
||||
OnToggled(new EventArgs());
|
||||
}
|
||||
else if(this.AllowEmptySelection)
|
||||
{
|
||||
this.SelectedOption = null;
|
||||
OnToggled(new EventArgs());
|
||||
}
|
||||
|
||||
RenderNecessary = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
RenderNecessary = false;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult result)
|
||||
{
|
||||
if (!RenderNecessary)
|
||||
return;
|
||||
|
||||
var bf = new ButtonForm(this);
|
||||
|
||||
var lst = new List<ButtonBase>();
|
||||
foreach (var o in this.Options)
|
||||
{
|
||||
var index = this.Options.IndexOf(o);
|
||||
if (o == this.SelectedOption)
|
||||
{
|
||||
lst.Add(new ButtonBase(SelectedIcon + " " + o.Text, "check$" + index));
|
||||
continue;
|
||||
}
|
||||
|
||||
lst.Add(new ButtonBase(o.Text, "check$" + index));
|
||||
}
|
||||
|
||||
bf.AddButtonRow(lst);
|
||||
|
||||
if (this.MessageId != null)
|
||||
{
|
||||
var m = await this.Device.Edit(this.MessageId.Value, this.Title, bf);
|
||||
}
|
||||
else
|
||||
{
|
||||
var m = await this.Device.Send(this.Title, bf, disableNotification: true);
|
||||
if (m != null)
|
||||
{
|
||||
this.MessageId = m.MessageId;
|
||||
}
|
||||
}
|
||||
|
||||
this.RenderNecessary = false;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public ButtonBase SelectedOption
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
/// <summary>
|
||||
/// A simple control for show and managing progress.
|
||||
+9
-7
@@ -7,20 +7,22 @@ using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class ToggleButton : ControlBase
|
||||
{
|
||||
|
||||
public String UncheckedIcon { get; set; } = "⚪";
|
||||
public String UncheckedIcon { get; set; } = Localizations.Default.Language["ToggleButton_OffIcon"];
|
||||
|
||||
public String CheckedIcon { get; set; } = "⚫";
|
||||
public String CheckedIcon { get; set; } = Localizations.Default.Language["ToggleButton_OnIcon"];
|
||||
|
||||
public String CheckedString { get; set; } = "On";
|
||||
public String CheckedString { get; set; } = Localizations.Default.Language["ToggleButton_On"];
|
||||
|
||||
public String UncheckedString { get; set; } = "Off";
|
||||
public String UncheckedString { get; set; } = Localizations.Default.Language["ToggleButton_Off"];
|
||||
|
||||
public String Title { get; set; } = "Toggle";
|
||||
public String ChangedString { get; set; } = Localizations.Default.Language["ToggleButton_Changed"];
|
||||
|
||||
public String Title { get; set; } = Localizations.Default.Language["ToggleButton_Title"];
|
||||
|
||||
public int? MessageId { get; set; }
|
||||
|
||||
@@ -68,7 +70,7 @@ namespace TelegramBotBase.Controls
|
||||
if (result.Handled)
|
||||
return;
|
||||
|
||||
await result.ConfirmAction();
|
||||
await result.ConfirmAction(this.ChangedString);
|
||||
|
||||
switch (value ?? "unknown")
|
||||
{
|
||||
+16
-14
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class TreeView : ControlBase
|
||||
{
|
||||
@@ -20,12 +20,12 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
private int? MessageId { get; set; }
|
||||
|
||||
public String MoveUpIcon { get; set; } = "🔼 up";
|
||||
public String MoveUpIcon { get; set; } = Localizations.Default.Language["TreeView_LevelUp"];
|
||||
|
||||
public TreeView()
|
||||
{
|
||||
this.Nodes = new List<TreeViewNode>();
|
||||
this.Title = "Select node";
|
||||
this.Title = Localizations.Default.Language["TreeView_Title"];
|
||||
}
|
||||
|
||||
|
||||
@@ -52,19 +52,21 @@ namespace TelegramBotBase.Controls
|
||||
|
||||
var n = (this.VisibleNode != null ? this.VisibleNode.FindNodeByValue(val) : this.Nodes.FirstOrDefault(a => a.Value == val));
|
||||
|
||||
if (n != null)
|
||||
{
|
||||
if (n.ChildNodes.Count > 0)
|
||||
{
|
||||
this.VisibleNode = n;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.SelectedNode = (this.SelectedNode != n ? n : null);
|
||||
}
|
||||
if (n == null)
|
||||
return;
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
if (n.ChildNodes.Count > 0)
|
||||
{
|
||||
this.VisibleNode = n;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.SelectedNode = (this.SelectedNode != n ? n : null);
|
||||
}
|
||||
|
||||
result.Handled = true;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TelegramBotBase.Controls
|
||||
namespace TelegramBotBase.Controls.Inline
|
||||
{
|
||||
public class TreeViewNode
|
||||
{
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user