Package Exports
- @grammyjs/conversations
- @grammyjs/conversations/out/mod.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@grammyjs/conversations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
grammY Conversations
WARNING: unstable. This is still a very early draft.
Here is an example of how you can use this package. It mainly exports Conversation
and ConversationFlavor
.
type MyContext = ConversationFlavor<Context>;
const bot = new Bot<MyContext>("");
async function conversation(t: Conversation<Context>, ctx: MyContext) {
await captcha(t, ctx);
await ctx.reply("Send a text message!");
ctx = await t.wait();
if (!ctx.message?.text) {
await ctx.reply("You failed. Bye.");
return;
}
const text0 = ctx.message.text;
do {
await ctx.reply("Send another text message!");
ctx = await t.wait();
} while (!ctx.message?.text);
const text1 = ctx.message.text;
await ctx.reply(`You first wrote ${text0} and then ${text1}`);
await ctx.reply("Thanks for participating!");
}
async function captcha(t: Conversation<Context>, ctx: MyContext) {
await ctx.reply("Prove you are human! What is the answer to everything?");
ctx = await t.wait();
while (ctx.message?.text !== "42") {
await ctx.reply("It does not look like you are human, try again!");
ctx = await t.wait();
}
await ctx.reply("Humanity saved, you may pass!");
}
bot.use(session({ initial: () => ({}) }));
bot.use(createConversation(conversation));
bot.command("start", (ctx) => ctx.reply("Hi! Send /enter"));
bot.command("enter", async (ctx) => {
await ctx.reply("Entering conversation!");
ctx.conversation.enter("conversation"); // enter the function "conversation" you declared
});
bot.start();