Compare commits

..
9 Commits
Author SHA1 Message Date
FlorianDahn 19f3c9c195 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 16:57:15 +07:00
FlorianDahn c6666f72bc - Improvements to system calls
- system calls now async with await
- added start command to example
- added "handled" property to system call event args
2019-03-20 16:57:10 +07:00
Florian Dahn bf7f6f851a Update README.md 2019-03-20 14:00:11 +07:00
FlorianDahn 816bf4cbbe Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 13:44:48 +07:00
FlorianDahn da80af86e3 Small fix in Systemcall management 2019-03-20 13:44:44 +07:00
Florian Dahn 929db3ae88 Update README.md 2019-03-20 11:18:03 +07:00
FlorianDahn 582f005b35 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-03-20 11:14:41 +07:00
FlorianDahn bbf90deccc - Added easy to use Proxy functionality into constructor
- improved DeviceSession
- added IsGroup property to DeviceSession
2019-03-20 11:14:38 +07:00
Florian Dahn 1d8b367bea Update README.md 2019-03-18 14:40:16 +07:00
8 changed files with 198 additions and 43 deletions
+17 -11
View File
@@ -6,21 +6,14 @@
[![license](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
[![downloads](https://img.shields.io/nuget/dt/TelegramBotBase.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase)
Hey guys,
here we are. After some time and thoughts i give my TelegramBot framework to public.
It is based on C#.
It is a module which is based on the original [TelegramBotLibrary](https://github.com/TelegramBots/Telegram.Bot) you will find in nuget.
It gives you features which will look/feel like WinForms or have a good way to create apps with actions and forms.
Test the Testproject: [@TGBaseBot](https://t.me/TGBaseBot)
---
## Index
- [Introduction](#introduction)
- [How to Start](#how-to-start)
- [Message Handling](#message-handling)
* [Example #0 - System Calls](#add-some-system-calls-example-0---system-calls)
@@ -34,6 +27,19 @@ It gives you features which will look/feel like WinForms or have a good way to c
* [Example #4 - Registration Formular](#registration-example-example-4---registration-form-test)
---
## Introduction
Hey guys,
here we are. After some time and thoughts i give my TelegramBot framework to public.
It is based on C#.
It is a module which is based on the original [TelegramBotLibrary](https://github.com/TelegramBots/Telegram.Bot) you will find in nuget.
It gives you features which will look/feel like WinForms or have a good way to create apps with actions and forms.
---
## How to start:
@@ -132,7 +138,7 @@ Inside of the BotFather you are able to add "Commands" to your TelegramBot. The
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.
If so, a special event Handler will get raised where you are easier able to manage the action behind.
Below we have 3 options.
Below we have 4 options.
/start - opens the Startformular
+10 -2
View File
@@ -21,13 +21,19 @@ namespace TelegramBaseTest
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/params");
bb.SystemCall += async (s, en) =>
{
switch (en.Command)
{
case "/start":
var start = new Start();
await en.Device.ActiveForm.NavigateTo(start);
break;
case "/form1":
var form1 = new TestForm();
@@ -48,7 +54,9 @@ namespace TelegramBaseTest
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
await en.Device.Send("Your parameters are " + m, replyTo: en.Device.LastMessage);
await en.Device.Send("Your parameters are: " + m, replyTo: en.Device.LastMessageId);
en.Handled = true;
break;
}
+19 -1
View File
@@ -44,6 +44,10 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if gets navigated to this form
/// </summary>
/// <returns></returns>
public virtual async Task Opened()
{
@@ -62,17 +66,31 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if the form gets loaded and on every message belongs to this context
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Load(MessageResult message)
{
}
/// <summary>
/// Gets invoked if the user has clicked a button.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Action(MessageResult message)
{
}
/// <summary>
/// Gets invoked at the end of the cycle to "Render" text, images, buttons, etc...
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Render(MessageResult message)
{
+39
View File
@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@@ -32,6 +34,43 @@ namespace TelegramBotBase.Base
Prepare();
}
public MessageClient(String APIKey, HttpClient proxy)
{
this.APIKey = APIKey;
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
Prepare();
}
public MessageClient(String APIKey, Uri proxyUrl)
{
this.APIKey = APIKey;
var proxy = new WebProxy(proxyUrl);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
Prepare();
}
/// <summary>
/// Initializes the client with a proxy
/// </summary>
/// <param name="APIKey"></param>
/// <param name="proxyHost">i.e. 127.0.0.1</param>
/// <param name="proxyPort">i.e. 10000</param>
public MessageClient(String APIKey, String proxyHost, int proxyPort)
{
this.APIKey = APIKey;
var proxy = new WebProxy(proxyHost, proxyPort);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
Prepare();
}
public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client)
{
this.APIKey = APIKey;
+5 -1
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
@@ -22,6 +23,8 @@ namespace TelegramBotBase.Base
public bool Handled { get; set; } = false;
public Message OriginalMessage { get; set; }
public SystemCallEventArgs()
{
@@ -29,10 +32,11 @@ namespace TelegramBotBase.Base
}
public SystemCallEventArgs(String Command, List<String> Parameters, long DeviceId, DeviceSession Device)
public SystemCallEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
{
this.Command = Command;
this.Parameters = Parameters;
this.OriginalMessage = Message;
this.DeviceId = DeviceId;
this.Device = Device;
}
+67 -18
View File
@@ -42,6 +42,8 @@ namespace TelegramBotBase
private static object __evSystemCall = new object();
public delegate Task SystemCallEventHandler(object sender, SystemCallEventArgs e);
private static object __evException = new object();
private static object __evUnhandledCall = new object();
@@ -78,6 +80,60 @@ namespace TelegramBotBase
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, System.Net.Http.HttpClient proxy)
{
this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey, proxy);
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
/// </summary>
@@ -154,20 +210,21 @@ namespace TelegramBotBase
{
ds = await this.Sessions.StartSession<T>(e.DeviceId);
ds.LastMessage = e.MessageId;
ds.LastMessage = e.Message;
OnSessionBegins(new SessionBeginResult(e.DeviceId, ds));
}
ds.LastAction = DateTime.Now;
ds.LastMessage = e.MessageId;
ds.LastMessage = e.Message;
//Is this a systemcall ?
if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand))
{
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, ds.DeviceId, ds);
OnSystemCall(sce);
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, e.Message, ds.DeviceId, ds);
await OnSystemCall(sce);
if (sce.Handled)
return;
}
@@ -223,7 +280,7 @@ namespace TelegramBotBase
ds.LastAction = DateTime.Now;
ds.LastMessage = e.MessageId;
ds.LastMessage = e.Message;
FormBase activeForm = null;
@@ -325,21 +382,13 @@ namespace TelegramBotBase
/// <summary>
/// Will be called if a system call gets raised
/// </summary>
public event EventHandler<SystemCallEventArgs> SystemCall
{
add
{
this.__Events.AddHandler(__evSystemCall, value);
}
remove
{
this.__Events.RemoveHandler(__evSystemCall, value);
}
}
public event SystemCallEventHandler SystemCall;
public void OnSystemCall(SystemCallEventArgs e)
public async Task OnSystemCall(SystemCallEventArgs e)
{
(this.__Events[__evSystemCall] as EventHandler<SystemCallEventArgs>)?.Invoke(this, e);
if (this.SystemCall != null)
await SystemCall(this, e);
}
/// <summary>
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.1.2")]
[assembly: AssemblyFileVersion("1.3.1.2")]
+37 -6
View File
@@ -27,11 +27,31 @@ namespace TelegramBotBase.Sessions
/// </summary>
public long DeviceId { get; set; }
/// <summary>
/// When did any last action happend (message received or button clicked)
/// </summary>
public DateTime LastAction { get; set; }
/// <summary>
/// Returns the form where the user/group is at the moment.
/// </summary>
public FormBase ActiveForm { get; set; }
public int LastMessage { get; set; }
/// <summary>
/// Returns the ID of the last received message.
/// </summary>
public int LastMessageId
{
get
{
return this.LastMessage?.MessageId ?? -1;
}
}
/// <summary>
/// Returns the last received message.
/// </summary>
public Message LastMessage { get; set; }
private MessageClient Client
{
@@ -41,13 +61,24 @@ namespace TelegramBotBase.Sessions
}
}
/// <summary>
/// Returns if the messages is posted within a group.
/// </summary>
public bool IsGroup
{
get
{
return this.LastMessage != null && this.LastMessage.Chat.Id != this.LastMessage.From.Id;
}
}
public EventHandlerList __Events = new EventHandlerList();
private static object __evMessageSent = new object();
public DeviceSession()
{
this.LastMessage = 0;
}
public DeviceSession(long DeviceId)
@@ -119,7 +150,7 @@ namespace TelegramBotBase.Sessions
{
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
}
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
catch (ApiRequestException ex)
{
return null;
}
@@ -153,7 +184,7 @@ namespace TelegramBotBase.Sessions
{
m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
}
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
catch (ApiRequestException ex)
{
return null;
}
@@ -192,7 +223,7 @@ namespace TelegramBotBase.Sessions
{
m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
}
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
catch (ApiRequestException ex)
{
return null;
}
@@ -316,7 +347,7 @@ namespace TelegramBotBase.Sessions
return true;
}
catch(ApiRequestException ex)
catch (ApiRequestException ex)
{
}