-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using fmt in media captions (replyFmtWithPhoto, replyFmtWithMediaGroup, editMessageMedia) #22
Comments
I've been digging into this issue and managed to come up with a dirty (maybe) workaround. The idea is to create a custom middleware that extends the Context with new methods import { Context } from "grammy";
import { InputMediaPhoto } from "grammy/types";
import { FormattedString, type Stringable, ParseModeFlavor } from "@grammyjs/parse-mode";
type FmtFlavor<C extends Context> = ParseModeFlavor<C> & {
replyFmtWithPhoto: (
photo: string,
options: {
caption: Stringable;
reply_markup?: Parameters<C["replyWithPhoto"]>[1]["reply_markup"];
}
) => ReturnType<C["replyWithPhoto"]>;
replyFmtWithMediaGroup: (
media: {
type: "photo";
media: string;
caption: Stringable;
}[]
) => ReturnType<C["replyWithMediaGroup"]>;
editFmtMessageMedia: (
media: InputMediaPhoto & { caption: Stringable },
options?: {
reply_markup?: Parameters<C["editMessageMedia"]>[1]["reply_markup"];
}
) => ReturnType<C["editMessageMedia"]>;
editFmtMessageText: (
text: Stringable,
options?: {
reply_markup?: Parameters<C["editMessageText"]>[1]["reply_markup"];
}
) => ReturnType<C["editMessageText"]>;
};
function hydrateReplyFmt<C extends Context>(ctx: FmtFlavor<C>, next: Function) {
ctx.replyFmtWithPhoto = (photo, options) => {
const entities = options.caption instanceof FormattedString
? { caption_entities: options.caption.entities }
: undefined;
return ctx.replyWithPhoto(
photo,
{
caption: options.caption.toString(),
...entities,
reply_markup: options.reply_markup
}
) as ReturnType<C["replyWithPhoto"]>;
};
ctx.replyFmtWithMediaGroup = (media) => {
const inputMedia = media.map(item => ({
type: "photo",
media: item.media,
caption: item.caption.toString(),
caption_entities: item.caption instanceof FormattedString
? item.caption.entities
: undefined
}));
return ctx.replyWithMediaGroup(inputMedia) as ReturnType<C["replyWithMediaGroup"]>;
};
ctx.editFmtMessageMedia = (media, options = {}) => {
const caption_entities = media.caption instanceof FormattedString
? media.caption.entities
: undefined;
return ctx.editMessageMedia(
{
type: "photo",
media: media.media,
caption: media.caption.toString(),
caption_entities,
},
options
) as ReturnType<C["editMessageMedia"]>;
};
ctx.editFmtMessageText = (text, options = {}) => {
const entities = text instanceof FormattedString
? text.entities
: undefined;
return ctx.editMessageText(
text.toString(),
{
entities,
...options,
}
) as ReturnType<C["editMessageText"]>;
};
return next();
} To use this middleware: Define the FmtFlavor type and hydrateReplyFmt function as shown above. Create your bot instance with the FmtFlavor type: this.bot = new Bot<FmtFlavor<Context>>(token, {
// ... other options
}); Use the middleware: this.bot.use(hydrateReply as Middleware<Context>);
this.bot.use(hydrateReplyFmt); After this setup, you can use Given that I found an ongoing Pull Request #17 which is trying to make the usage of fmt simpler (but with no clear timeline), I'm not sure if this ugly workaround is worth submitting as a PR or creating as a separate plugin. I'm sharing it just to solve the urgent needs of others in the same situation as me (if your Telegraf project has countless fmt and is migrating to grammY). I hope this helps someone else facing the same issue. |
You're already pretty close to having a working pull request. The plugin works in a similar way as what you have implemented. #17 was closed because we want to add the functionality of this plugin to the core library for the next major version. There are many reasons for why we want to do this, but the bottom line is that it makes little sense to put a lot of effort into rewriting this plugin. Making people migrate to a new version only to have them migrate again right after that is a waste of time. Long story short, even though we aren't going to rework how this plugin works, it could definitely be enriched by better support for more methods, implemented in a similar way as what the current methods do. Thus, it would indeed make a lot of sense to add your code to the plugin and ship it in a new minor version. I'd be happy to review your pull request (or kidnap @KnightNiwrem and force him to do it for me) :) |
Thanks for the clarification. As I understand it, the plan is to discontinue this plugin (at least no so active maintained) and merge all its functionality into the grammY core library in next major version. From this perspective, I'll try go ahead and directly integrate the new functions I implemented into the existing parse-mode plugin, following the approach I outlined above. This should help everyone navigate through this transitional period. Will submit a PR based on this a little later :) |
You understood correctly. Thank you so much! Looking forward to the PQ |
@dimpurr can this be closed now? |
Currently, there are some limitations when using fmt with media messages in grammY:
It would be great to have a consistent way to use fmt for formatting text across all message types, including media captions. This would make the migration process from Telegraf to grammY smoother and provide a more intuitive formatting experience for developers.
Kindly are there any plans to add support for using fmt in media captions? If not, are there any recommended tricky workarounds?
The text was updated successfully, but these errors were encountered: