Projektdateien hinzufügen.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
//public class ActionResult : ResultBase
|
||||
//{
|
||||
// public Telegram.Bot.Args.CallbackQueryEventArgs RawCallbackData { get; set; }
|
||||
|
||||
// public override long DeviceId
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// return this.RawCallbackData?.CallbackQuery.Message?.Chat.Id ?? 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
// public String Command
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// return this.RawCallbackData.CallbackQuery.Message.Text ?? "";
|
||||
// }
|
||||
// }
|
||||
|
||||
// public String RawData
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// return this.RawCallbackData.CallbackQuery.Data;
|
||||
// }
|
||||
// }
|
||||
|
||||
// public T GetData<T>()
|
||||
// where T : class
|
||||
// {
|
||||
// T cd = null;
|
||||
// try
|
||||
// {
|
||||
// cd = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(this.RawData);
|
||||
|
||||
// return cd;
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// Bestätigt den Erhalt der Aktion.
|
||||
// /// </summary>
|
||||
// /// <param name="message"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task ConfirmAction(String message = "")
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// await this.Client.TelegramClient.AnswerCallbackQueryAsync(this.RawCallbackData.CallbackQuery.Id, message);
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// public override async Task DeleteMessage()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// await this.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, this.RawCallbackData.CallbackQuery.Message.MessageId);
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// public ActionResult(Telegram.Bot.Args.CallbackQueryEventArgs callback)
|
||||
// {
|
||||
// this.RawCallbackData = callback;
|
||||
// this.Message = callback.CallbackQuery.Message;
|
||||
// }
|
||||
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class ButtonClickedEventArgs : EventArgs
|
||||
{
|
||||
public ButtonBase Button { get; set; }
|
||||
|
||||
|
||||
public ButtonClickedEventArgs()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ButtonClickedEventArgs(ButtonBase button)
|
||||
{
|
||||
this.Button = button;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class MessageClient
|
||||
{
|
||||
|
||||
public String APIKey { get; set; }
|
||||
|
||||
public Telegram.Bot.TelegramBotClient TelegramClient { get; set; }
|
||||
|
||||
private EventHandlerList __Events { get; set; } = new EventHandlerList();
|
||||
|
||||
private static object __evOnMessage = new object();
|
||||
|
||||
private static object __evCallbackQuery = new object();
|
||||
|
||||
|
||||
public MessageClient(String APIKey)
|
||||
{
|
||||
this.APIKey = APIKey;
|
||||
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey);
|
||||
|
||||
Prepare();
|
||||
}
|
||||
|
||||
public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client)
|
||||
{
|
||||
this.APIKey = APIKey;
|
||||
this.TelegramClient = Client;
|
||||
|
||||
Prepare();
|
||||
}
|
||||
|
||||
|
||||
public void Prepare()
|
||||
{
|
||||
this.TelegramClient.Timeout = new TimeSpan(0, 0, 30);
|
||||
|
||||
|
||||
this.TelegramClient.OnMessage += TelegramClient_OnMessage;
|
||||
this.TelegramClient.OnCallbackQuery += TelegramClient_OnCallbackQuery;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private async void TelegramClient_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
|
||||
{
|
||||
//Skip empty messages by default
|
||||
if (e.Message == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var mr = new MessageResult(e);
|
||||
mr.Client = this;
|
||||
OnMessage(mr);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private async void TelegramClient_OnCallbackQuery(object sender, Telegram.Bot.Args.CallbackQueryEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ar = new MessageResult(e);
|
||||
ar.Client = this;
|
||||
OnAction(ar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region "Events"
|
||||
|
||||
public event EventHandler<MessageResult> Message
|
||||
{
|
||||
add
|
||||
{
|
||||
this.__Events.AddHandler(__evOnMessage, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.__Events.RemoveHandler(__evOnMessage, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMessage(MessageResult result)
|
||||
{
|
||||
(this.__Events[__evOnMessage] 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);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class MessageResult : ResultBase
|
||||
{
|
||||
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
|
||||
|
||||
public Telegram.Bot.Args.CallbackQueryEventArgs RawCallbackData { get; set; }
|
||||
|
||||
public override long DeviceId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Chat.Id ?? this.RawCallbackData?.CallbackQuery.Message?.Chat.Id ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Die Id der Nachricht
|
||||
/// </summary>
|
||||
public new int MessageId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Message?.MessageId ?? this.RawCallbackData?.CallbackQuery?.Message?.MessageId ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
public String Command
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Text ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
public String MessageText
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawMessageData?.Message?.Text ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ist diese eine Aktion? (z.B.: Button Klick)
|
||||
/// </summary>
|
||||
public bool IsAction
|
||||
{
|
||||
get
|
||||
{
|
||||
return (this.RawCallbackData != null);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Handled { get; set; } = false;
|
||||
|
||||
public String RawData
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.RawCallbackData.CallbackQuery.Data;
|
||||
}
|
||||
}
|
||||
|
||||
public T GetData<T>()
|
||||
where T : class
|
||||
{
|
||||
T cd = null;
|
||||
try
|
||||
{
|
||||
cd = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(this.RawData);
|
||||
|
||||
return cd;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bestätigt den Erhalt der Aktion.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ConfirmAction(String message = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.Client.TelegramClient.AnswerCallbackQueryAsync(this.RawCallbackData.CallbackQuery.Id, message);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task DeleteMessage()
|
||||
{
|
||||
try
|
||||
{
|
||||
await base.DeleteMessage(this.MessageId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MessageResult(Telegram.Bot.Args.MessageEventArgs rawdata)
|
||||
{
|
||||
this.RawMessageData = rawdata;
|
||||
this.Message = rawdata.Message;
|
||||
}
|
||||
|
||||
public MessageResult(Telegram.Bot.Args.CallbackQueryEventArgs callback)
|
||||
{
|
||||
this.RawCallbackData = callback;
|
||||
this.Message = callback.CallbackQuery.Message;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class MessageSentEventArgs
|
||||
{
|
||||
public int MessageId { get; set; }
|
||||
|
||||
public Message Message { get; set; }
|
||||
|
||||
public MessageSentEventArgs()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MessageSentEventArgs(int MessageId, Message message)
|
||||
{
|
||||
this.MessageId = MessageId;
|
||||
this.Message = message;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class ResultBase
|
||||
{
|
||||
public MessageClient Client { get; set; }
|
||||
|
||||
public virtual long DeviceId { get; set; }
|
||||
|
||||
public int MessageId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Message.MessageId;
|
||||
}
|
||||
}
|
||||
|
||||
public Telegram.Bot.Types.Message Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Löscht die aktuelle Nachricht, oder die übergebene
|
||||
/// </summary>
|
||||
/// <param name="messageId"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task DeleteMessage()
|
||||
{
|
||||
await DeleteMessage(this.MessageId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Löscht die aktuelle Nachricht, oder die übergebene
|
||||
/// </summary>
|
||||
/// <param name="messageId"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task DeleteMessage(int messageId = -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, (messageId == -1 ? this.MessageId : messageId));
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class SessionBeginResult : EventArgs
|
||||
{
|
||||
public long DeviceId { get; set; }
|
||||
|
||||
public DeviceSession Device { get; set; }
|
||||
|
||||
public SessionBeginResult(long DeviceId, DeviceSession Device)
|
||||
{
|
||||
this.DeviceId = DeviceId;
|
||||
this.Device = Device;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class SystemCallEventArgs : EventArgs
|
||||
{
|
||||
public String Command { get; set; }
|
||||
|
||||
public long DeviceId { get; set; }
|
||||
|
||||
public DeviceSession Device {get;set;}
|
||||
|
||||
|
||||
public SystemCallEventArgs()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public SystemCallEventArgs(String Command, long DeviceId, DeviceSession Device)
|
||||
{
|
||||
this.Command = Command;
|
||||
this.DeviceId = DeviceId;
|
||||
this.Device = Device;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class SystemExceptionEventArgs : EventArgs
|
||||
{
|
||||
|
||||
public String Command { get; set; }
|
||||
|
||||
public long DeviceId { get; set; }
|
||||
|
||||
public DeviceSession Device { get; set; }
|
||||
|
||||
public Exception Error { get; set; }
|
||||
|
||||
public SystemExceptionEventArgs()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public SystemExceptionEventArgs(String Command, long DeviceId, DeviceSession Device, Exception error)
|
||||
{
|
||||
this.Command = Command;
|
||||
this.DeviceId = DeviceId;
|
||||
this.Device = Device;
|
||||
this.Error = error;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Sessions;
|
||||
|
||||
namespace TelegramBotBase.Base
|
||||
{
|
||||
public class UnhandledCallEventArgs : EventArgs
|
||||
{
|
||||
public String Command { get; set; }
|
||||
|
||||
public long DeviceId { get; set; }
|
||||
|
||||
public DeviceSession Device {get;set;}
|
||||
|
||||
public String RawData { get; set; }
|
||||
|
||||
public int MessageId { get; set; }
|
||||
|
||||
public Message Message { get; set; }
|
||||
|
||||
public bool Handled { get; set; }
|
||||
|
||||
|
||||
public UnhandledCallEventArgs()
|
||||
{
|
||||
this.Handled = false;
|
||||
|
||||
}
|
||||
|
||||
public UnhandledCallEventArgs(String Command,String RawData, long DeviceId, int MessageId, Message message, DeviceSession Device) : this()
|
||||
{
|
||||
this.Command = Command;
|
||||
this.RawData = RawData;
|
||||
this.DeviceId = DeviceId;
|
||||
this.MessageId = MessageId;
|
||||
this.Message = message;
|
||||
this.Device = Device;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user