Skip to content

Commit

Permalink
new/Добавлена возможность отвечать на сообщения
Browse files Browse the repository at this point in the history
new/Добавлена возможность прикреплять клавиатуры
  • Loading branch information
Байкин Егор authored and Байкин Егор committed Oct 27, 2024
1 parent 4c5d96f commit 99c84a5
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 109 deletions.
36 changes: 17 additions & 19 deletions Telegram bots/Chat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text;
using System.Text.Json;

namespace Telegram_bots
{
Expand Down Expand Up @@ -118,38 +119,35 @@ public static Chat FromJSON(string jsonString)
}
#endregion

/// <summary>
/// Turns it into a string
/// </summary>
/// <returns>A string representing the chat</returns>
/// <inheritdoc/>
public override string ToString()
{
string str = string.Empty;
str += "Id: ";
str += Id;
str += ", Type: ";
str += Type;
StringBuilder builder = new();
builder.Append("Id: ");
builder.Append(Id);
builder.Append(", Type: ");
builder.Append(Type);
if (Title != null)
{
str += ", Title: ";
str += Title;
builder.Append(", Title: ");
builder.Append(Title);
}
if (FirstName != null)
{
str += ", First name: ";
str += FirstName;
builder.Append(", First name: ");
builder.Append(FirstName);
}
if (LastName != null)
{
str += ", Last name: ";
str += LastName;
builder.Append(", Last name: ");
builder.Append(LastName);
}
if (Username != null)
{
str += ", Username: ";
str += Username;
builder.Append(", Username: ");
builder.Append(Username);
}
return str;
return builder.ToString();
}
}

Expand Down
13 changes: 13 additions & 0 deletions Telegram bots/Keyboards/IKeyboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Telegram_bots.Keyboards
{
/// <summary>
/// Keyboard
/// </summary>
public interface IKeyboard
{
/// <summary>
/// Keyboard's buttons
/// </summary>
IKeyboardButton[][] Keyboard { get; set; }
}
}
19 changes: 19 additions & 0 deletions Telegram bots/Keyboards/IKeyboardButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;
using Telegram_bots.Keyboards;

namespace Telegram_bots
{
/// <summary>
/// Keyboard's button
/// </summary>
[JsonDerivedType(typeof(InlineKeyboardButton))]
[JsonDerivedType(typeof(KeyboardButton))]
public interface IKeyboardButton
{
/// <summary>
/// Button's text
/// </summary>
[JsonPropertyName("text")]
string Text { get; set; }
}
}
23 changes: 23 additions & 0 deletions Telegram bots/Keyboards/InlineKeyboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections;
using System.Text.Json.Serialization;
using System.Xml.Serialization;

namespace Telegram_bots.Keyboards
{
/// <summary>
/// Inline keyboard
/// </summary>
/// <remarks>
/// Create an inline keyboard
/// </remarks>
/// <param name="buttons">Keyboard's buttons</param>
[Serializable]
public class InlineKeyboard(InlineKeyboardButton[][] buttons) : IKeyboard
{
/// <summary>
/// <inheritdoc/>
/// </summary>
[JsonPropertyName("inline_keyboard")]
public IKeyboardButton[][] Keyboard { get; set; } = buttons;
}
}
48 changes: 48 additions & 0 deletions Telegram bots/Keyboards/InlineKeyboardButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Telegram_bots
{
/// <summary>
/// The inline keyboard button
/// </summary>
[Serializable]
public class InlineKeyboardButton : IKeyboardButton
{
/// <summary>
/// <inheritdoc/>
/// </summary>
[JsonPropertyName("text")]
public string Text { get; set; }
/// <summary>
/// CallbackData of the inline keyboard buttons
/// </summary>
[JsonPropertyName("callback_data")]
public string CallbackData { get; set; }

/// <summary>
/// Create an inline keyboard button
/// </summary>
/// <param name="text">Button's text</param>
/// <param name="callbackData">CallbackData of the inline keyboard buttons</param>
public InlineKeyboardButton(string text, string callbackData)
{
Text = text;
CallbackData = callbackData;
}

/// <summary>
/// Create an inline keyboard button
/// </summary>
/// <param name="text">Button's text</param>
public InlineKeyboardButton(string text)
{
Text = text;
CallbackData = text;
}
}
}
21 changes: 21 additions & 0 deletions Telegram bots/Keyboards/KeyboardButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;

namespace Telegram_bots.Keyboards
{
/// <summary>
/// Keyboard button
/// </summary>
/// <remarks>
/// Create a keyboard button
/// </remarks>
/// <param name="text">Button's text</param>
[Serializable]
public class KeyboardButton(string text) : IKeyboardButton
{
/// <summary>
/// <inheritdoc/>
/// </summary>
[JsonPropertyName("text")]
public string Text { get; set; } = text;
}
}
26 changes: 26 additions & 0 deletions Telegram bots/Keyboards/ReplyKeyboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Telegram_bots.Keyboards
{
/// <summary>
/// Keyboard
/// </summary>
/// <remarks>
/// Create a keyboard
/// </remarks>
/// <param name="buttons">Keyboard's buttons</param>
[Serializable]
public class ReplyKeyboard(KeyboardButton[][] buttons) : IKeyboard
{
/// <summary>
/// <inheritdoc/>
/// </summary>
[JsonPropertyName("keyboard")]
public IKeyboardButton[][] Keyboard { get; set; } = buttons;
}
}
25 changes: 25 additions & 0 deletions Telegram bots/Keyboards/ReplyKeyboardRemove.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Telegram_bots.Keyboards
{
/// <summary>
/// Remove the keyboard
/// </summary>
[Serializable]
public class ReplyKeyboardRemove : IKeyboard
{
/// It's nothing
[JsonIgnore]
public IKeyboardButton[][] Keyboard { get; set; } = [[]];
/// <summary>
/// Should I remove the keyboard or not?
/// </summary>
[JsonPropertyName("remove_keyboard")]
public bool RemoveKeyboard { get; set; } = true;
}
}
65 changes: 46 additions & 19 deletions Telegram bots/Message.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text;
using System.Text.Json;

namespace Telegram_bots
{
Expand All @@ -21,13 +22,21 @@ public class Message
public required Chat Chat { get; set; }

/// <summary>
/// The text of the message
/// The Text of the message
/// </summary>
public string? Text { get; set; }
/// <summary>
/// The sender of the message
/// </summary>
public User? From { get; set; }
/// <summary>
/// Parameters for responding to a message
/// </summary>
public ReplyParameters? ReplyParameters { get; set; }
/// <summary>
/// The message that this message responds to
/// </summary>
public Message? ReplyMessage { get; set; }

#region bool Equals(object? obj)
/// <summary>
Expand Down Expand Up @@ -72,6 +81,14 @@ public static Message FromJSON(JsonDocument jsonDocument)
{
message.From = User.FromJSON(from);
}
if (json.TryGetProperty("reply_to_message", out JsonElement replyMessage))
{
message.ReplyMessage = FromJSON(replyMessage);
}
if (json.TryGetProperty("reply_parameters", out JsonElement replyParameters))
{
message.ReplyParameters = ReplyParameters.FromJSON(replyParameters);
}

return message;
}
Expand All @@ -95,31 +112,41 @@ public static Message FromJSON(string jsonString)
}
#endregion

/// <summary>
/// Turns it into a string
/// </summary>
/// <returns>The string representing the message</returns>
/// <inheritdoc/>
public override string ToString()
{
string str = "Id: ";
str += Id;
str += ", Date: ";
str += Datetime;
str += ", Chat: (";
str += Chat;
str += ")";
StringBuilder builder = new();
builder.Append("Id: ");
builder.Append(Id);
builder.Append(", Date: ");
builder.Append(Datetime);
builder.Append(", Chat: (");
builder.Append(Chat);
builder.Append(')');
if (Text != null)
{
str += ", Text: ";
str += Text;
builder.Append(", Text: ");
builder.Append(Text);
}
if (From != null)
{
str += ", From: (";
str += From;
str += ")";
builder.Append(", From: (");
builder.Append(From);
builder.Append(')');
}
if (ReplyParameters != null)
{
builder.Append(", Reply parameters: (");
builder.Append(ReplyParameters);
builder.Append(')');
}
if (ReplyMessage != null)
{
builder.Append(", Reply to message: (");
builder.Append(ReplyMessage);
builder.Append(')');
}
return str;
return builder.ToString();
}

/// <summary>
Expand Down
Loading

0 comments on commit 99c84a5

Please sign in to comment.