Skip to content

Commit

Permalink
allow send message without reply
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed Oct 21, 2024
1 parent 400a64a commit 5951b32
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- <BaseOutputPath>.bin\</BaseOutputPath>-->
<!-- </PropertyGroup>-->
<PropertyGroup>
<Version>224.10.9058.35805</Version>
<Version>224.10.9058.40270</Version>
<Company>AZ Corp</Company>
<Authors>azhe403</Authors>
<Product>ZiziBot</Product>
Expand Down
49 changes: 29 additions & 20 deletions backend/ZiziBot.Application/Services/TelegramService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public async Task<BotResponseBase> SendMessageText(
TimeSpan deleteAfter = default
)
{
var replyParameters = new ReplyParameters() {
AllowSendingWithoutReply = true,
};

if (text.IsNullOrEmpty())
return Complete();

Expand All @@ -112,39 +116,46 @@ public async Task<BotResponseBase> SendMessageText(
if (threadId == -1)
threadId = _request.MessageThreadId;

var replyToMessageId = _request.ReplyMessage ? _request.ReplyToMessageId : -1;
replyParameters.MessageId = _request.ReplyMessage ? _request.ReplyToMessageId : -1;

if (_request.ReplyToMessage != null)
{
replyToMessageId = _request.ReplyToMessage.MessageId;
replyParameters.MessageId = _request.ReplyToMessage.MessageId;
}

logger.LogInformation("Sending message to chat {ChatId}", _request.ChatId);
logger.LogInformation("Sending message to chat {ChatId}", targetChatId);
try
{
SentMessage = await Bot.SendTextMessageAsync(
chatId: targetChatId,
messageThreadId: threadId,
text: text,
replyParameters: replyToMessageId,
replyParameters: replyParameters,
parseMode: ParseMode.Html,
replyMarkup: replyMarkup,
linkPreviewOptions: true
);
}
catch (Exception exception)
catch (Exception exception1)
{
if (exception.Message.Contains("thread not found"))
if (exception1.Message.Contains("thread not found"))
{
logger.LogWarning("Trying send message without thread to ChatId: {ChatId}", _request.ChatId);
SentMessage = await Bot.SendTextMessageAsync(
chatId: targetChatId,
text: text,
replyParameters: _request.ReplyMessage ? _request.ReplyToMessageId : -1,
parseMode: ParseMode.Html,
replyMarkup: replyMarkup,
linkPreviewOptions: true
);
try
{
logger.LogWarning("Trying send message without thread to ChatId: {ChatId}", targetChatId);
SentMessage = await Bot.SendTextMessageAsync(
chatId: targetChatId,
text: text,
replyParameters: _request.ReplyMessage ? _request.ReplyToMessageId : -1,
parseMode: ParseMode.Html,
replyMarkup: replyMarkup,
linkPreviewOptions: true
);
}
catch (Exception exception2)
{
logger.LogError(exception2, "Error when Sending message to {ChatId}", targetChatId);
}
}
}

Expand All @@ -157,15 +168,14 @@ await mediator.Send(new CreateChatActivityRequest() {
TransactionId = _request.TransactionId
});

logger.LogInformation("Message sent to chat {ChatId}", _request.ChatId);
logger.LogInformation("Message sent to chat {ChatId}", targetChatId);

var deleteAfterExec = deleteAfter != default ? deleteAfter : _request.DeleteAfter;

if (_request.CleanupTargets.Contains(CleanupTarget.None) || deleteAfterExec == default)
return Complete();

logger.LogDebug("Schedule delete message {MessageId} on ChatId: {ChatId} in {DeleteAfter} seconds",
SentMessage.MessageId, _request.ChatId, _request.DeleteAfter.TotalSeconds);
logger.LogDebug("Schedule delete message {MessageId} on ChatId: {ChatId} in {DeleteAfter} seconds", SentMessage.MessageId, targetChatId, _request.DeleteAfter.TotalSeconds);

mediatorService.Schedule(new DeleteMessageBotRequestModel {
BotToken = _request.BotToken,
Expand All @@ -185,8 +195,7 @@ await mediator.Send(new CreateChatActivityRequest() {
}, delayExecution: deleteAfterExec);
}

logger.LogInformation("Message {MessageId} scheduled for deletion in {DeleteAfter} seconds",
SentMessage.MessageId, _request.DeleteAfter.TotalSeconds);
logger.LogInformation("Message {MessageId} scheduled for deletion in {DeleteAfter} seconds", SentMessage.MessageId, _request.DeleteAfter.TotalSeconds);

return Complete();
}
Expand Down

0 comments on commit 5951b32

Please sign in to comment.