无国籍人问题
为在隐私模式下工作的Telegram用户创建无国籍问题
您想要保留用户的隐私 启用电报隐私模式( 默认), (中文(简体) ), 发送用户用自己的语言翻译问题, 不要保存状态 用户正在这样做吗 ?
这个图书馆想解决这个问题.
基本想法是把问题发给你
特别文本 在结尾.
此文本对于用户来说是看不见的, 但对于您的机器人来说仍然是可见的 。 当用户
对信件的答复被选中。 如果它包含这个特殊的
文本结尾是回答问题。 这样你就可以有很多
字符串用于与翻译时相同的问题。 你只需要这样
确保 uniqueIdentifier 在你的机器人里是独一无二的.
特别感谢 @Ramin-Bateni (英语) 拿着 看着 他的解释 如果你喜欢的话.
安装
npm install grammy @grammyjs/stateless-question
使用量
import {StatelessQuestion} from '@grammyjs/stateless-question';
const bot = new Bot(token);
const unicornQuestion = new StatelessQuestion('unicorns', ctx => {
console.log('User thinks unicorns are doing:', ctx.message)
})
// Dont forget to use the middleware
bot.use(unicornQuestion.middleware())
bot.command('rainbows', async ctx => {
let text
if (ctx.session.language === 'de') {
text = 'Was machen Einhörner?'
} else {
text = 'What are unicorns doing?'
}
return unicornQuestion.replyWithMarkdown(ctx, text)
})
// Or send your question manually (make sure to use a parse_mode and force_reply!)
bot.command('unicorn', async ctx => ctx.replyWithMarkdown('What are unicorns doing?' + unicornQuestion.messageSuffixMarkdown(), {parse_mode: 'Markdown', reply_markup: {force_reply: true}})
bot.command('unicorn', async ctx => ctx.replyWithHTML( 'What are unicorns doing?' + unicornQuestion.messageSuffixHTML(), {parse_mode: 'HTML', reply_markup: {force_reply: true}})
附加状态
当您的问题针对特定主题时,您可以使用
additionalState 记住那个无国籍的人 例如当
您想要知道一个事件正在哪个房间发生 你可以将事件设定为
additionalState。这在与
远程图像内线菜单 改为
存储返回菜单的路径.
const locationQuestion = new StatelessQuestion(
"target",
(ctx, additionalState) => {
console.log("Location of", additionalState, "is", ctx.message.text);
saveHeroLocation(additionalState, ctx.message.text);
},
);
// Dont forget to use the middleware
bot.use(locationQuestion.middleware());
bot.command("batman", async (ctx) => {
return locationQuestion.replyWithMarkdown(ctx, "Where is Batman?", "batman");
});
bot.command("superman", async (ctx) => {
return locationQuestion.replyWithMarkdown(
ctx,
"Where is superman?",
"superman",
);
});
selective ForceReply
分组使用时, 只要求用户回复可能更好
开始提问。 提供电报 selected 作为一种选择
强制收回。 。 。 .
为了让 selected 您需要回复信件的工作
(单位:千美元)reply_to_message_id)或需要提及应答复的用户
这个.
await ctx.replyWithHTML(
"What are unicorns doing?" + unicornQuestion.messageSuffixHTML(),
{
parse_mode: "HTML",
reply_to_message_id: ctx.message.message_id,
reply_markup: {
force_reply: true,
selective: true,
},
},
);
已知问题
强制回答问题总是在一些Telegram客户端中显示,甚至在问题已经回答很久之后
见 4号
至于设计选择 Telegram为创建ForceReply
Telegram 客户端知道一个问题是否仍然相关. 作为工作
我们可以发送
回覆键盘移动 作为
reply_markup 带有不需要其他信件的信件 reply_markup (仅限
一条信息是可能的).
const unicornQuestion = new StatelessQuestion("unicorns", async (ctx) => {
console.log("User thinks unicorns are doing:", ctx.message);
await ctx.reply("if you think so...", {
reply_markup: { remove_keyboard: true },
});
});
随询问消息一起发送中止/ 返回( 内置) 按钮
见 1号
电报只允许一个 reply_markup 在一个时候。 由于此库依赖于
对信件的答复 强制收回
已经用作 reply_markup。为了发送一个带
需要(内含)键盘标记的同样信息。 这将是一个
第二届 reply_markup 不幸的电报不支持它.