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

Formatting helpers #1690

Merged
merged 15 commits into from Oct 1, 2022
Merged

Formatting helpers #1690

merged 15 commits into from Oct 1, 2022

Conversation

MKRhere
Copy link
Member

@MKRhere MKRhere commented Sep 15, 2022

Ref: related conversation

Formatting is often troublesome, especially if you have many characters that need escaping. Escaping is messy, and it is much cleaner to create entities instead, but there were no utils to help with this in Telegraf so far. This PR adds support for telegraf/format. To be used like this:

import { fmt, bold, italic, underline, mention, link } from "telegraf/format";

ctx.reply(fmt`
Ground control to ${mention("Major Tom", 10000000)}
${bold`Lock your Soyuz hatch`} and ${italic`put your helmet on`}

${link(underline`David Bowie`), "https://en.wikipedia.org/wiki/David_Bowie")}
`);

This also just works with captions!

ctx.replyWithPhoto(
  file.id,
  { caption: fmt`${bold`File name:`} ${file.name}` },
);

For the future: emoji() may be a nice addition to formatting; however emojibase is quite large and would not be a suitable dependency for telegraf. Creating a separate @telegraf/format-emoji package could work.

@MKRhere MKRhere force-pushed the feat-format-helpers branch 3 times, most recently from 3284fe6 to 80494b7 Compare September 19, 2022 22:09
@MKRhere MKRhere marked this pull request as ready for review September 19, 2022 22:09
Copy link

@Kamrulhasan12345 Kamrulhasan12345 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok.

@KnorpelSenf
Copy link
Collaborator

So effectively you're slowly integrating the most useful plugins into the core lib? That sounds like a better strategy than hoping for permission to publish packages under @telegraf/ :D

@MKRhere
Copy link
Member Author

MKRhere commented Sep 20, 2022

@KnorpelSenf I have access to npm @telegraf/ now :) I don't have access to the GitHub org to create repositories yet. But this is the strategy for telegraf now - everything that's very useful and is lightweight (~100 loc) goes in the core.

I started this PR thinking of format being a tiny function in Markup.fmt which sounded very appropriate, but it's grown into a full file :D This is mostly because we don't want to clutter the main import space, but still want to import { bold, italic, ... } as top-level items with import suggestions.

The benefit of in-core here is that user doesn't have to mess with Context/API types or implementation - even external plugins (like a future @telegraf/format-emoji) can use the FmtString contract and telegraf will know what to do.

@MKRhere MKRhere added this to the v4.10.0 milestone Sep 20, 2022
src/telegram.ts Outdated Show resolved Hide resolved
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Use sendChatAction instead

Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
Unless necessary - when FmtString entities are present
@Viiprogrammer
Copy link

Viiprogrammer commented Sep 25, 2022

Interesting idea, but what about localization?

Many bots use it. I think the use of such solutions complicates the localization and translating of the text, even if i18n was not used now, may be needed in the future.

About escaping - telegram-escape

@MKRhere
Copy link
Member Author

MKRhere commented Sep 25, 2022

@Viiprogrammer That's a very good point - at the moment I don't think there's any way to reconcile fmt and i18n.

may be needed in the future.

Any solution in this regard will need to be rewritten when i18n is needed - so not many ways to be prematurely prepared for it. I think the two needs are orthographic.

@MKRhere MKRhere merged commit dfdab5d into v4 Oct 1, 2022
@MKRhere MKRhere deleted the feat-format-helpers branch October 1, 2022 12:07
@MKRhere MKRhere removed this from the v4.11.0 milestone Oct 3, 2022
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.

None yet

5 participants