-
Notifications
You must be signed in to change notification settings - Fork 118
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
Native support for templating messages #669
Comments
Doesn’t https://github.com/grammyjs/i18n/ already do this? |
The parse-mode plugin has the I'd like to support some sort of formatted strings natively in the library so that no plugin is needed for it. That way, other plugins can depend on the utilities. This should enable us to improve the formatting support inside other plugins. |
@rojvv will do that for predefined messages, which you have in your code, also with translations. This requires
The main "feature" here is that i want to allow users, to send a telegram message to the bot and use it as a template, |
@KnorpelSenf I'm using Currently I have implemented this, (no variables/templating support) // ~pseudo-code
.on('msg', async (ctx) => {
template.message = ctx.msg;
});
.command('/send', async (ctx) => {
const templateId = ctx.text.split(' ')[1]
// run a function every second for 30 users
const text = template.message.caption || template.message.text;
const entities = template.message.caption_entities?.length ?template.message.caption_entities : template.message.entities;
// check if it contains photo or media send media
// My plan is to replace placeholders in the template at this point
const finaltext = liquidjs(text, { ...variables })
bot.api.sendMessage(chatId, text, {
entities // entities may be invalid here
}) I'm thinking about a function which will fix any text shifts providing only const newEntities = fix(originalText, outputText, oldEntities) If using custom template replacing logic, it should be easy to fix the entities, something like this (AI generated code) function normalizeEntities(template, variables, entities) {
let resultText = "";
let offsetAdjustment = 0;
const newEntities = [];
const placeholderRegex = /{{(.*?)}}/g;
let lastIndex = 0;
template.replace(placeholderRegex, (match, key, index) => {
// Append text before the placeholder
resultText += template.slice(lastIndex, index);
// Add the replacement value
const replacement = variables[key] || "";
resultText += replacement;
// Calculate the length difference
const lengthDifference = replacement.length - match.length;
// Adjust entities affected by this replacement
entities.forEach((entity) => {
const entityEnd = entity.offset + entity.length;
if (entity.offset >= index + offsetAdjustment) {
// Entity starts after the replacement
entity.offset += lengthDifference;
} else if (entityEnd > index + offsetAdjustment) {
// Entity overlaps with the replacement
entity.length += lengthDifference;
}
});
// Update offsetAdjustment and lastIndex
offsetAdjustment += lengthDifference;
lastIndex = index + match.length;
});
// Append remaining text
resultText += template.slice(lastIndex);
// Return normalized text and updated entities
return { text: resultText, entities };
} |
I see. There is currently no library that works like It would be cool to have this as a plugin, though! This use case isn't really covered in any way yet. If you end up writing something like this, please drop us a link here or in https://t.me/grammyjs so can consider making it an official plugin. In any case, it can be listed as a third-party plugin on the website. |
We have a custom message distribution service, which should send messages to a segment of our users. I'm planning to add an option for templating, for example:
This is easy task to do using some templating libraries or i18n libraries, if the text don't contain formatting. But with formatting it will break. The reason for that, is that depending on the length of the variables inside the template, the offset of entities may become invalid/wrong.
Note: the message "templates" are stored as json, serialized message object which bot will get as a message.
So I have multiple questions here:
Thank you very much for your great work on this project. This library is one of my most beloved libraries in javascript ecosystem.
The text was updated successfully, but these errors were encountered: