Skip to content

Commit

Permalink
Добавлена возможность редактировать сообщения
Browse files Browse the repository at this point in the history
Создан метод TelegramBot.EditMessageText(object messageText, long messageId, long? chatId = null, Keyboards.InlineKeyboard? keyboard = null)
  • Loading branch information
egor5f421 committed Oct 31, 2024
1 parent 46d966d commit d3a7b9f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Telegram bots/Telegram bots.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<SignAssembly>False</SignAssembly>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
</PropertyGroup>

</Project>
48 changes: 48 additions & 0 deletions Telegram bots/TelegramBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public async Task<Message> SendMessage(object messageText,
/// </summary>
/// <param name="messageId">Identifier of the message to delete</param>
/// <param name="chatId">Unique identifier for the target chat</param>
/// <exception cref="ArgumentNullException">It is thrown if one of the arguments is null</exception>
/// <exception cref="Exceptions.IncorrectRequestException">It is thrown if an incorrect request was made</exception>
/// <returns>True on success</returns>
public async Task<bool> DeleteMessage(long messageId, long? chatId = null)
{
Expand Down Expand Up @@ -164,6 +166,52 @@ public async Task<bool> DeleteMessage(long messageId, long? chatId = null)
}
#endregion

#region EditMessageText
/// <summary>
/// Use this method to edit text messages
/// </summary>
/// <param name="messageText">New text of the message</param>
/// <param name="messageId">Identifier of the message to edit</param>
/// <param name="chatId">Unique identifier for the target chat</param>
/// <param name="keyboard">Inline keyboard</param>
/// <exception cref="ArgumentNullException">It is thrown if one of the arguments is null</exception>
/// <exception cref="Exceptions.IncorrectRequestException">It is thrown if an incorrect request was made</exception>
/// <returns></returns>
public async Task<Message> EditMessageText(object messageText,
long messageId,
long? chatId = null,
Keyboards.InlineKeyboard? keyboard = null)
{
ArgumentNullException.ThrowIfNull(messageText);
ArgumentNullException.ThrowIfNull(messageId);

chatId ??= lastChatId;

Dictionary<string, string?> contentData = [];

contentData["text"] = messageText.ToString();
contentData["message_id"] = messageId.ToString();
contentData["chat_id"] = chatId.ToString();
if (keyboard != null)
{
contentData["reply_markup"] = JsonSerializer.Serialize(keyboard, typeof(Keyboards.InlineKeyboard));
}

HttpContent content = new FormUrlEncodedContent(contentData);

HttpResponseMessage response = await httpClient.PostAsync("editMessageText", content);
string jsonString = await response.Content.ReadAsStringAsync();

JsonDocument json = JsonDocument.Parse(jsonString);

Exceptions.IncorrectRequestException.ThrowIfNotOk(json);

Message message = Message.FromJSON(json.RootElement.GetProperty("result"));

return message;
}
#endregion

#region AnswerCallbackQuery
/// <summary>
/// This method must be called after receiving the callback query request.
Expand Down

0 comments on commit d3a7b9f

Please sign in to comment.