@grammyjs/stateless-question
Version:
Create stateless questions to Telegram users working in privacy mode
59 lines (58 loc) • 2.27 kB
JavaScript
import { getAdditionalState, isContextReplyToMessage, isReplyToQuestion, suffixHTML, suffixMarkdown, suffixMarkdownV2, } from './identifier.js';
export class StatelessQuestion {
constructor(uniqueIdentifier, answer) {
Object.defineProperty(this, "answer", {
enumerable: true,
configurable: true,
writable: true,
value: answer
});
Object.defineProperty(this, "uniqueIdentifier", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.uniqueIdentifier = encodeURIComponent(uniqueIdentifier);
}
middleware() {
return async (context, next) => {
if (isContextReplyToMessage(context)
&& isReplyToQuestion(context, this.uniqueIdentifier)) {
const additionalState = getAdditionalState(context, this.uniqueIdentifier);
return this.answer(context, additionalState);
}
await next();
};
}
messageSuffixHTML(additionalState) {
return suffixHTML(this.uniqueIdentifier, additionalState);
}
messageSuffixMarkdown(additionalState) {
return suffixMarkdown(this.uniqueIdentifier, additionalState);
}
messageSuffixMarkdownV2(additionalState) {
return suffixMarkdownV2(this.uniqueIdentifier, additionalState);
}
async replyWithHTML(context, text, additionalState) {
const textResult = text + this.messageSuffixHTML(additionalState);
return context.reply(textResult, {
reply_markup: { force_reply: true },
parse_mode: 'HTML',
});
}
async replyWithMarkdown(context, text, additionalState) {
const textResult = text + this.messageSuffixMarkdown(additionalState);
return context.reply(textResult, {
reply_markup: { force_reply: true },
parse_mode: 'Markdown',
});
}
async replyWithMarkdownV2(context, text, additionalState) {
const textResult = text + this.messageSuffixMarkdownV2(additionalState);
return context.reply(textResult, {
reply_markup: { force_reply: true },
parse_mode: 'MarkdownV2',
});
}
}