using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Telegram.Bot.Exceptions; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.InputFiles; using Telegram.Bot.Types.ReplyMarkups; using TelegramBotBase.Base; using TelegramBotBase.Form; namespace TelegramBotBase.Sessions { /// /// Base class for a device/chat session /// public class DeviceSession { /// /// Device or chat id /// public long DeviceId { get; set; } /// /// When did any last action happend (message received or button clicked) /// public DateTime LastAction { get; set; } /// /// Returns the form where the user/group is at the moment. /// public FormBase ActiveForm { get; set; } /// /// Returns the ID of the last received message. /// public int LastMessageId { get { return this.LastMessage?.MessageId ?? -1; } } /// /// Returns the last received message. /// public Message LastMessage { get; set; } private MessageClient Client { get { return this.ActiveForm.Client; } } /// /// Returns if the messages is posted within a group. /// public bool IsGroup { get { return this.LastMessage != null && (this.LastMessage.Chat.Type == ChatType.Group | this.LastMessage.Chat.Type == ChatType.Supergroup); } } /// /// Returns if the messages is posted within a channel. /// public bool IsChannel { get { return this.LastMessage != null && this.LastMessage.Chat.Type == ChatType.Channel; } } private EventHandlerList __Events = new EventHandlerList(); private static object __evMessageSent = new object(); private static object __evMessageReceived = new object(); public DeviceSession() { } public DeviceSession(long DeviceId) { this.DeviceId = DeviceId; } public DeviceSession(long DeviceId, FormBase StartForm) { this.DeviceId = DeviceId; this.ActiveForm = StartForm; this.ActiveForm.Device = this; } /// /// Edits the text message /// /// /// /// /// public async Task Edit(int messageId, String text, ButtonForm buttons = null) { if (this.ActiveForm == null) return null; InlineKeyboardMarkup markup = null; if (buttons != null) { markup = buttons; } try { var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, replyMarkup: markup); return m; } catch { } return null; } /// /// Edits the text message /// /// /// /// /// public async Task Edit(Message message, ButtonForm buttons = null) { if (this.ActiveForm == null) return null; InlineKeyboardMarkup markup = null; if (buttons != null) { markup = buttons; } try { var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, message.MessageId, message.Text, replyMarkup: markup); return m; } catch { } return null; } /// /// Sends a simple text message /// /// /// /// /// /// public async Task Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { if (this.ActiveForm == null) return null; InlineKeyboardMarkup markup = null; if (buttons != null) { markup = buttons; } Message m = null; try { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } catch (ApiRequestException ex) { return null; } catch { return null; } return m; } /// /// Sends a simple text message /// /// /// /// /// /// public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false) { if (this.ActiveForm == null) return null; Message m = null; try { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } catch (ApiRequestException ex) { return null; } catch { return null; } return m; } /// /// Sends an image /// /// /// /// /// /// public async Task SendPhoto(InputOnlineFile file, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { if (this.ActiveForm == null) return null; InlineKeyboardMarkup markup = null; if (buttons != null) { markup = buttons; } Message m = null; try { m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); OnMessageSent(new MessageSentEventArgs(m)); } catch (ApiRequestException ex) { return null; } catch { return null; } return m; } /// /// Sends an image /// /// /// /// /// /// /// public async Task SendPhoto(Image image, String name, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { using (var fileStream = Tools.Images.ToStream(image, ImageFormat.Png)) { InputOnlineFile fts = new InputOnlineFile(fileStream, name); var m = await SendPhoto(fts, buttons, replyTo, disableNotification); return m; } } /// /// Sends an image /// /// /// /// /// /// /// public async Task SendPhoto(Bitmap image, String name, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { using (var fileStream = Tools.Images.ToStream(image, ImageFormat.Png)) { InputOnlineFile fts = new InputOnlineFile(fileStream, name); var m = await SendPhoto(fts, buttons, replyTo, disableNotification); return m; } } /// /// Sends an document /// /// /// /// /// /// /// /// public async Task SendDocument(String filename, byte[] document, String caption = "", ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { MemoryStream ms = new MemoryStream(document); InputOnlineFile fts = new InputOnlineFile(ms, filename); var m = await SendDocument(fts, caption, buttons, replyTo, disableNotification); return m; } /// /// Sends an document /// /// /// /// /// /// /// public async Task SendDocument(InputOnlineFile document, String caption = "", ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false) { InlineKeyboardMarkup markup = null; if (buttons != null) { markup = buttons; } var m = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo); OnMessageSent(new MessageSentEventArgs(m)); return m; } /// /// Set a chat action (showed to the user) /// /// /// public async Task SetAction(ChatAction action) { await this.Client.TelegramClient.SendChatActionAsync(this.DeviceId, action); } /// /// Requests the contact from the user. /// /// /// /// /// public async Task RequestContact(String buttonText = "Send your contact", String requestMessage = "Give me your phone number!", bool OneTimeOnly = true) { var rck = new ReplyKeyboardMarkup(KeyboardButton.WithRequestContact(buttonText)); rck.OneTimeKeyboard = OneTimeOnly; return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rck); } /// /// Requests the location from the user. /// /// /// /// /// public async Task RequestLocation(String buttonText = "Send your location", String requestMessage = "Give me your location!", bool OneTimeOnly = true) { var rcl = new ReplyKeyboardMarkup(KeyboardButton.WithRequestLocation(buttonText)); rcl.OneTimeKeyboard = OneTimeOnly; return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl); } /// /// Deletes a message /// /// /// public virtual async Task DeleteMessage(int messageId = -1) { try { await this.Client.TelegramClient.DeleteMessageAsync(this.DeviceId, messageId); return true; } catch (ApiRequestException ex) { } return false; } /// /// Deletes the given message /// /// /// public virtual async Task DeleteMessage(Message message) { return await DeleteMessage(message.MessageId); } /// /// Eventhandler for sent messages /// public event EventHandler MessageSent { add { this.__Events.AddHandler(__evMessageSent, value); } remove { this.__Events.RemoveHandler(__evMessageSent, value); } } public void OnMessageSent(MessageSentEventArgs e) { (this.__Events[__evMessageSent] as EventHandler)?.Invoke(this, e); } /// /// Eventhandler for received messages /// public event EventHandler MessageReceived { add { this.__Events.AddHandler(__evMessageReceived, value); } remove { this.__Events.RemoveHandler(__evMessageReceived, value); } } public void OnMessageReceived(MessageReceivedEventArgs e) { (this.__Events[__evMessageReceived] as EventHandler)?.Invoke(this, e); } } }