Skip to content
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

feat: support fmt in media captions #23

Merged
merged 2 commits into from
May 4, 2024
Merged

Conversation

dimpurr
Copy link

@dimpurr dimpurr commented Mar 30, 2024

According to Issue #22 , this PR adds support for using fmt in media captions, including:

  • replyFmtWithPhoto: send a photo with a formatted caption
  • replyFmtWithMediaGroup: send a media group with formatted captions
  • editFmtMessageMedia: edit media and formatted caption in a message
  • editFmtMessageText: edit formatted text in a message

Changes made in this PR:

  1. deps.deno.ts and deps.node.ts:

    • Import InputMedia* types from the correct paths.
    • Export InputMedia* types.
  2. hydrate.ts:

    • Add new methods to ParseModeFlavor type: replyFmtWithPhoto, replyFmtWithMediaGroup, editFmtMessageMedia, editFmtMessageText.
    • Implement the new methods in hydrateReplyFmt middleware.
    • Update replyFmtWithMediaGroup to accept any InputMedia* type.
  3. README.md:

    • Add documentation for the new methods in the "Using fmt in media captions" section.
    • Provide usage examples for the new methods.
    • Explain that editFmtMessageMedia allows changing the media type when editing a message.

Test: deno run --allow-net --allow-read --allow-env test.ts

import { Bot, Context } from "https://deno.land/x/[email protected]/mod.ts";
import {
    hydrateReply,
    hydrateReplyFmt,
    ParseModeFlavor,
    bold,
    code,
    fmt,
    italic,
} from "./mod.ts";

const BOT_TOKEN = "";

const bot = new Bot<ParseModeFlavor<Context>>(BOT_TOKEN);

bot.use(hydrateReply);
bot.use(hydrateReplyFmt);

bot.command("start", async (ctx) => {
    await ctx.reply("Bot started. Send /test to test the plugin.");
});

bot.command("test", async (ctx) => {
    await ctx.replyFmt(fmt`This is a ${bold("formatted")} message.`);

    await ctx.replyFmtWithPhoto("http://placehold.co/600x400/EEE/31343C.jpg", {
        caption: fmt`This is a ${bold("formatted")} caption`,
    });

    await ctx.replyFmtWithMediaGroup([
        {
            type: "photo",
            media: "http://placehold.co/600x400/CCC/31343C.jpg",
            caption: fmt`First photo with ${italic("italic")} text`
        },
        {
            type: "video",
            media: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4",
            caption: fmt`A ${code("video")} with formatted caption`
        }
    ]);
});

bot.catch((err) => {
    console.error(err);
});

bot.start();

src/hydrate.ts Outdated Show resolved Hide resolved
src/hydrate.ts Outdated Show resolved Hide resolved
src/hydrate.ts Outdated Show resolved Hide resolved
src/hydrate.ts Outdated Show resolved Hide resolved
@dimpurr
Copy link
Author

dimpurr commented Apr 8, 2024

@KnightNiwrem Thank you for the review! Changes made:

  • Merged hydrateReplyFmt into hydrateReply to avoid creating two separate middlewares.
  • Fixed the type definitions for args in replyFmtWithPhoto and replyFmtWithMediaGroup using the utility type.
  • Ensured the correct passing of remaining arguments in replyFmtWithPhoto.

Updated test code:

import { Bot, Context } from "https://deno.land/x/[email protected]/mod.ts";
import {
  hydrateReply,
  ParseModeFlavor,
  bold,
  code,
  fmt,
  italic,
} from "./mod.ts";

const BOT_TOKEN = "";
const bot = new Bot<ParseModeFlavor<Context>>(BOT_TOKEN);

bot.use(hydrateReply);

bot.command("start", async (ctx) => {
  await ctx.reply("Bot started. Send /test to test the plugin.");
});

bot.command("test", async (ctx) => {
  await ctx.replyFmt(fmt`This is a ${bold("formatted")} message.`);

  await ctx.replyFmtWithPhoto("http://placehold.co/600x400/EEE/31343C.jpg", {
    caption: fmt`This is a ${bold("formatted")} caption`,
  });

  await ctx.replyFmtWithMediaGroup([
    {
      type: "photo",
      media: "http://placehold.co/600x400/CCC/31343C.jpg",
      caption: fmt`First photo with ${italic("italic")} text`,
    },
    {
      type: "video",
      media: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4",
      caption: fmt`A ${code("video")} with formatted caption`,
    },
  ]);
});

bot.catch((err) => {
  console.error(err);
});

bot.start();

Test command:

deno run --allow-net --allow-read --allow-env test.ts

Please let me know if any further suggestions or concerns.

@KnightNiwrem KnightNiwrem merged commit feed102 into grammyjs:master May 4, 2024
@KnightNiwrem
Copy link
Collaborator

Thanks for the PQ! I'll make some small changes as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants