/// <summary> /// Sends a simple text message /// </summary> /// <param name="text"></param> /// <param name="markup"></param> /// <param name="replyTo"></param> /// <param name="disableNotification"></param> /// <returns></returns> public async Task <Message> Send(String text, ReplyMarkupBase markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default, bool MarkdownV2AutoEscape = true) { if (this.ActiveForm == null) { return(null); } if (text.Length > Constants.Telegram.MaxMessageLength) { throw new MaxLengthException(text.Length); } if (parseMode == ParseMode.MarkdownV2 && MarkdownV2AutoEscape) { text = text.MarkdownV2Escape(); } try { var t = API(a => a.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); var o = GetOrigin(new StackTrace()); OnMessageSent(new MessageSentEventArgs(await t, o)); return(await t); } catch { return(null); } }
/// <summary> /// Инициализация ноды /// </summary> /// <param name="message">сообщение, которое увидит пользователь</param> /// <param name="buttons">кнопки, которые отправятся пользователю</param> public MenuTreeNode(string message, ReplyMarkupBase buttons) { CurrentMessage = message; CurrentButtons = buttons; NextNodes = new List <MenuTreeNode>(); ButtonReaction = (x) => Console.WriteLine($"received {x.Message.Text}, action is empty"); }
/// <summary> /// Sends a simple text message /// </summary> /// <param name="text"></param> /// <param name="markup"></param> /// <param name="replyTo"></param> /// <param name="disableNotification"></param> /// <returns></returns> public async Task <Message> Send(String text, ReplyMarkupBase markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default) { if (this.ActiveForm == null) { return(null); } Message m = null; if (text.Length > Constants.Telegram.MaxMessageLength) { throw new MaxLengthException(text.Length); } try { m = await(this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); OnMessageSent(new MessageSentEventArgs(m)); } catch (ApiRequestException) { return(null); } catch { return(null); } return(m); }
/// <summary> /// Отправляет сообщение пользователю /// </summary> /// <param name="chatId">Идентификатор диалога с пользователем</param> /// <param name="text">Текст сообщения</param> /// <param name="buttons">Кнопки</param> public async void SendMessage(long chatId, string text, ReplyMarkupBase buttons) { await botClient.SendTextMessageAsync( chatId : chatId, text : text, replyMarkup : buttons); }
internal QueuedMessage(ChatId chatId, string text, bool disableWebPagePreview, bool disableNotification, int replyToMessageId, ReplyMarkupBase replyMarkup) { ChatId = chatId; Text = text; DisableWebPagePreview = disableWebPagePreview; DisableNotification = disableNotification; ReplyToMessageId = replyToMessageId; ReplyMarkup = replyMarkup; }
/// <summary> /// Sends a text message with a queue. Using this method you should never hit 429-Errors (Too many requests). /// If you do, please open a GitHub issue. /// Note that the parameters <paramref name="disableWebPagePreview"/> and <paramref name="disableNotification"/> /// will be overridden by the last merged message, if you enable message merging. If you want to use this method to send messages, /// you need to call the <see cref="StartQueue(TelegramBot, ParseMode, bool)"/> method to start it. /// </summary> /// <param name="Bot">The Telegram Bot object for which a message is enqueued</param> /// <param name="chatId">The id or channel username of the chat to send the message to</param> /// <param name="text">The text of the message</param> /// <param name="disableWebPagePreview">If this is true, no website preview will be shown</param> /// <param name="disableNotification">If this is true, users will not receive a notification or a silent one for this message</param> /// <param name="replyToMessageId">The message id of the message to reply to in this chat, if any</param> /// <param name="replyMarkup">A <see cref="ReplyMarkupBase"/>.</param> public static void SendMessageWithQueue(this TelegramBot Bot, ChatId chatId, string text, bool disableWebPagePreview = false, bool disableNotification = false, int replyToMessageId = -1, ReplyMarkupBase replyMarkup = null) { if (Bot._messageQueue.ContainsKey(chatId)) { Bot._messageQueue[chatId].Enqueue(new QueuedMessage(chatId, text, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup)); } else { Bot._messageQueue.Add(chatId, new Queue <QueuedMessage>(new[] { new QueuedMessage(chatId, text, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup) })); } }
/// <summary> /// Initializes a new <see cref="ReplyMarkupMaker"/> for the selected type /// </summary> /// <param name="type">The type of keyboard to make</param> public ReplyMarkupMaker(ReplyMarkupType type) { _markupType = type; switch (type) { case ReplyMarkupType.Force: _markup = new ForceReplyMarkup(); break; case ReplyMarkupType.Inline: _markup = new InlineKeyboardMarkup(); break; case ReplyMarkupType.Remove: _markup = new ReplyKeyboardRemove(); break; case ReplyMarkupType.Reply: _markup = new ReplyKeyboardMarkup(); break; } }
public Responce(ReplyMarkupBase keyboard, string textMessage) { Keyboard = keyboard ?? throw new ArgumentNullException(nameof(keyboard)); TextMessage = textMessage ?? throw new ArgumentNullException(nameof(textMessage)); }
private static void MessageQueue(this TelegramBot Bot) { while (Bot.IsMessageQueueing) { var chatIds = Bot._messageQueue.Keys.ToList().Where(x => !Bot._messageQueueTimeout.ContainsKey(x)); foreach (var chatId in chatIds) { var queue = Bot._messageQueue[chatId]; string final = ""; bool byteMax = false; int i = 0; bool disableWebPagePreview = false; bool disableNotification = false; int replyToMessageId = -1; ReplyMarkupBase replyMarkup = null; while (!byteMax && queue.Count > 0 && (Bot._messageQueueMerging || i == 0)) { i++; var m = queue.Peek(); if (i > 1 && ((replyToMessageId != -1 || m.ReplyToMessageId != -1) || replyMarkup != null || m.ReplyMarkup != null)) { break; } var temp = final + m.Text + Environment.NewLine + Environment.NewLine; if (Encoding.UTF8.GetByteCount(temp) > 512) { if (i > 1) { break; // we already have at least one message and yet it's below 512 bytes, so send that and keep this message for the next time. } else { Bot._messageQueueTimeout.Add(chatId, 3); // this is the first message and it's over 512 bytes, so send it and add some additional timeout. byteMax = true; } } queue.Dequeue(); //remove the message, we are sending it. final += m.Text + Environment.NewLine + Environment.NewLine; disableWebPagePreview = m.DisableWebPagePreview; disableNotification = m.DisableNotification; replyToMessageId = m.ReplyToMessageId; replyMarkup = m.ReplyMarkup; } if (!Bot.IsMessageQueueing) { return; } if (!string.IsNullOrEmpty(final)) { Bot.SendMessage(chatId, final, Bot._messageQueueParseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup); } if (queue.Count == 0) { Bot._messageQueue.Remove(chatId); } } if (!Bot.IsMessageQueueing) { return; } foreach (var timeout in Bot._messageQueueTimeout.Keys.ToList()) { if (!Bot.IsMessageQueueing) { return; } Bot._messageQueueTimeout[timeout]--; if (Bot._messageQueueTimeout[timeout] == 0) { Bot._messageQueueTimeout.Remove(timeout); } } Thread.Sleep(4000); } }