Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using VkNet.Enums.SafetyEnums;
  4. using VkNet.Model;
  5. using VkNet.Utils;
  6.  
  7. namespace VkBotNet.Bot.Builder
  8. {
  9.     /// <summary>
  10.     /// Строитель клавиатур
  11.     /// </summary>
  12.     public class MessageKeyboardBuilder
  13.     {
  14.         private bool _isOneTime;
  15.  
  16.         private readonly List<List<MessageKeyboardButton>> _fullKeyboard = new List<List<MessageKeyboardButton>>();
  17.         private List<MessageKeyboardButton> _currentLine = new List<MessageKeyboardButton>();
  18.         private readonly string _type;
  19.  
  20.         /// <summary>
  21.         /// Конструктор без типа кнопки, надо задавать везде вручную
  22.         /// </summary>
  23.         public MessageKeyboardBuilder()
  24.         {
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Конструктор с типом кнопок
  29.         /// </summary>
  30.         /// <param name="type">Тип кнопки</param>
  31.         public MessageKeyboardBuilder(string type)
  32.         {
  33.             _type = type;
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Делает клавиатуру одноразовой
  38.         /// </summary>
  39.         public MessageKeyboardBuilder SetOneTime()
  40.         {
  41.             _isOneTime = true;
  42.             return this;
  43.         }
  44.  
  45.         /// <summary>
  46.         /// Добавить кнопку
  47.         /// </summary>
  48.         /// <param name="label">Надписть на кнопке</param>
  49.         /// <param name="extra">Дополнительная информация о кнопке</param>
  50.         /// <param name="type">Основная информация о кнопке</param>
  51.         /// <param name="color">Цвет кнопки</param>
  52.         public MessageKeyboardBuilder AddButton(string label, string extra,
  53.             KeyboardButtonColor color, string type = null)
  54.         {
  55.             var
  56.                 keyboardButtonColor = KeyboardButtonColor.Default;
  57.             type = type ?? _type;
  58.             _currentLine.Add(new MessageKeyboardButton
  59.             {
  60.                 Color = keyboardButtonColor,
  61.                 Action = new MessageKeyboardButtonAction
  62.                 {
  63.                     Label = label,
  64.                     Payload = $"{{\"{type}\":\"{extra}\"}}",
  65.                     Type = KeyboardButtonActionType.Text
  66.                 }
  67.             });
  68.             return this;
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Переход на следующую линию
  73.         /// </summary>
  74.         public MessageKeyboardBuilder Line()
  75.         {
  76.             _fullKeyboard.Add(_currentLine);
  77.             _currentLine = new List<MessageKeyboardButton>();
  78.             return this;
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Получить объект клавиатуры для сообщения
  83.         /// </summary>
  84.         public MessageKeyboard Get()
  85.         {
  86.             _fullKeyboard.Add(_currentLine);
  87.             var messageKeyboard = new MessageKeyboard
  88.             {
  89.                 OneTime = _isOneTime,
  90.                 Buttons = _fullKeyboard.Select(e => e.ToReadOnlyCollection()).ToReadOnlyCollection()
  91.             };
  92.  
  93.             return messageKeyboard;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement