-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from deploy-f/fix_photomsg_editing
Fix for photo message updating
- Loading branch information
Showing
12 changed files
with
414 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace Deployf.Botf; | ||
|
||
[System.Serializable] | ||
public class BotfException : System.Exception | ||
[Serializable] | ||
public class BotfException : Exception | ||
{ | ||
public BotfException() { } | ||
public BotfException(string message) : base(message) { } | ||
public BotfException(string message, System.Exception inner) : base(message, inner) { } | ||
public BotfException(string message, Exception inner) : base(message, inner) { } | ||
protected BotfException( | ||
System.Runtime.Serialization.SerializationInfo info, | ||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { } | ||
SerializationInfo info, | ||
StreamingContext context) : base(info, context) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Deployf.Botf/System/UpdateMessageStrategies/EditTextMessageStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Telegram.Bot; | ||
using Telegram.Bot.Types; | ||
|
||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
/// <summary> | ||
/// Situation: previous message has no media file and a new message does not have. | ||
/// </summary> | ||
public class EditTextMessageStrategy : IUpdateMessageStrategy | ||
{ | ||
private readonly BotfBot _bot; | ||
|
||
public EditTextMessageStrategy(BotfBot bot) | ||
{ | ||
_bot = bot; | ||
} | ||
|
||
public bool CanHandle(IUpdateMessageContext context) | ||
{ | ||
var newMessageFileIsEmpty = string.IsNullOrEmpty(context.MediaFile?.FileId) && | ||
string.IsNullOrEmpty(context.MediaFile?.Url); | ||
|
||
return context.PreviousMessage.Photo == null && newMessageFileIsEmpty; | ||
} | ||
|
||
public async Task<Message> UpdateMessage(IUpdateMessageContext context) | ||
{ | ||
return await _bot.Client.EditMessageTextAsync( | ||
context.ChatId, | ||
context.MessageId, | ||
context.MessageText, | ||
parseMode: context.ParseMode, | ||
replyMarkup: context.KeyboardMarkup, | ||
cancellationToken: context.CancelToken | ||
); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Deployf.Botf/System/UpdateMessageStrategies/IUpdateMessageStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Telegram.Bot.Types; | ||
|
||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
public interface IUpdateMessageStrategy | ||
{ | ||
public bool CanHandle(IUpdateMessageContext context); | ||
public Task<Message> UpdateMessage(IUpdateMessageContext context); | ||
} |
21 changes: 21 additions & 0 deletions
21
Deployf.Botf/System/UpdateMessageStrategies/IUpdateMessageStrategyFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
public interface IUpdateMessageStrategyFactory | ||
{ | ||
IUpdateMessageStrategy? GetStrategy(IUpdateMessageContext context); | ||
} | ||
|
||
public class UpdateMessageStrategyFactory : IUpdateMessageStrategyFactory | ||
{ | ||
private readonly IEnumerable<IUpdateMessageStrategy> _strategies; | ||
|
||
public UpdateMessageStrategyFactory(IEnumerable<IUpdateMessageStrategy> strategies) | ||
{ | ||
_strategies = strategies; | ||
} | ||
|
||
public IUpdateMessageStrategy? GetStrategy(IUpdateMessageContext context) | ||
{ | ||
return _strategies.FirstOrDefault(s => s.CanHandle(context)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Deployf.Botf/System/UpdateMessageStrategies/MediaToMediaFileStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Telegram.Bot; | ||
using Telegram.Bot.Types; | ||
|
||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
/// <summary> | ||
/// Situation: previous message has a media file and a new message has one. | ||
/// </summary> | ||
public class MediaToMediaFileStrategy : IUpdateMessageStrategy | ||
{ | ||
private readonly BotfBot _bot; | ||
|
||
public MediaToMediaFileStrategy(BotfBot bot) | ||
{ | ||
_bot = bot; | ||
} | ||
|
||
public bool CanHandle(IUpdateMessageContext context) | ||
{ | ||
var newMessageHasFile = !string.IsNullOrEmpty(context.MediaFile?.FileId) || | ||
!string.IsNullOrEmpty(context.MediaFile?.Url); | ||
|
||
return context.PreviousMessage.Photo != null && newMessageHasFile; | ||
} | ||
|
||
public async Task<Message> UpdateMessage(IUpdateMessageContext context) | ||
{ | ||
var updateMessagePolicy = context.UpdateContext.GetCurrentUpdateMsgPolicy(); | ||
if (updateMessagePolicy is UpdateMessagePolicy.DeleteAndSend) | ||
{ | ||
await _bot.Client.DeleteMessageAsync(context.ChatId, context.PreviousMessage.MessageId, context.CancelToken); | ||
return await _bot.Client.SendPhotoAsync( | ||
context.ChatId, | ||
context.MediaFile!, | ||
context.MessageText, | ||
context.ParseMode, | ||
replyMarkup: context.KeyboardMarkup, | ||
cancellationToken: context.CancelToken, | ||
replyToMessageId: context.ReplyToMessageId); | ||
} | ||
else | ||
{ | ||
return await _bot.Client.EditMessageMediaAsync( | ||
context.ChatId, | ||
context.MessageId, | ||
new InputMediaPhoto(context.MediaFile!) | ||
{ | ||
Caption = context.MessageText | ||
}, | ||
replyMarkup: context.KeyboardMarkup, | ||
cancellationToken: context.CancelToken | ||
); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Deployf.Botf/System/UpdateMessageStrategies/MediaToPlainTextStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Telegram.Bot; | ||
using Telegram.Bot.Types; | ||
|
||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
/// <summary> | ||
/// Situation: previous message has media file, but a new message does not have. | ||
/// </summary> | ||
public class MediaToPlainTextStrategy : IUpdateMessageStrategy | ||
{ | ||
private readonly BotfBot _bot; | ||
|
||
public MediaToPlainTextStrategy(BotfBot bot) | ||
{ | ||
_bot = bot; | ||
} | ||
|
||
public bool CanHandle(IUpdateMessageContext context) | ||
{ | ||
var newMessageFileIsEmpty = string.IsNullOrEmpty(context.MediaFile?.FileId) && | ||
string.IsNullOrEmpty(context.MediaFile?.Url); | ||
|
||
return context.PreviousMessage.Photo != null && newMessageFileIsEmpty; | ||
} | ||
|
||
public async Task<Message> UpdateMessage(IUpdateMessageContext context) | ||
{ | ||
await _bot.Client.DeleteMessageAsync(context.ChatId, context.PreviousMessage.MessageId, context.CancelToken); | ||
return await _bot.Client.SendTextMessageAsync( | ||
context.ChatId, | ||
context.MessageText, | ||
context.ParseMode, | ||
replyMarkup: context.KeyboardMarkup, | ||
cancellationToken: context.CancelToken, | ||
replyToMessageId: context.ReplyToMessageId); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Deployf.Botf/System/UpdateMessageStrategies/PlainTextToMediaStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Telegram.Bot; | ||
using Telegram.Bot.Types; | ||
|
||
namespace Deployf.Botf.System.UpdateMessageStrategies; | ||
|
||
/// <summary> | ||
/// Situation: previous message has no media file, but a new message has one. | ||
/// </summary> | ||
public class PlainTextToMediaStrategy : IUpdateMessageStrategy | ||
{ | ||
private readonly BotfBot _bot; | ||
|
||
public PlainTextToMediaStrategy(BotfBot bot) | ||
{ | ||
_bot = bot; | ||
} | ||
|
||
public bool CanHandle(IUpdateMessageContext context) | ||
{ | ||
var newMessageHasFile = !string.IsNullOrEmpty(context.MediaFile?.FileId) || | ||
!string.IsNullOrEmpty(context.MediaFile?.Url); | ||
|
||
return context.PreviousMessage.Photo == null && newMessageHasFile; | ||
} | ||
|
||
public async Task<Message> UpdateMessage(IUpdateMessageContext context) | ||
{ | ||
await _bot.Client.DeleteMessageAsync(context.ChatId, context.PreviousMessage.MessageId, context.CancelToken); | ||
return await _bot.Client.SendPhotoAsync( | ||
context.ChatId, | ||
context.MediaFile!, | ||
context.MessageText, | ||
context.ParseMode, | ||
replyMarkup: context.KeyboardMarkup, | ||
cancellationToken: context.CancelToken, | ||
replyToMessageId: context.ReplyToMessageId); | ||
} | ||
} |
Oops, something went wrong.