Socket
Socket
Sign inDemoInstall

@grammyjs/conversations

Package Overview
Dependencies
13
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @grammyjs/conversations

Conversational interfaces for grammY


Version published
Weekly downloads
2.9K
decreased by-15.24%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

grammY Conversations


WARNING: unstable. This is not completely stable yet. Please try it out and provide feedback, either by opening an issue or in the group chat. You can also look at a preview of the docs.

Here is an example for both Deno and Node of how you can use this package. Besides the main plugin function conversation, it exports createConversation for registering conversations. Also, it will give you the types Conversation, and ConversationFlavor.

The API reference is at https://doc.deno.land/https://deno.land/x/grammy_conversations/mod.ts.

Deno

import {
    Bot,
    Context,
    session,
} from "https://deno.land/x/grammy@v1.8.3/mod.ts";
import {
    type Conversation,
    type ConversationFlavor,
    conversations,
    createConversation,
} from "./src/mod.ts";

type MyContext = Context & ConversationFlavor;
type MyConversation = Conversation<MyContext>;

const bot = new Bot<MyContext>("");

async function example(conversation: MyConversation, ctx: MyContext) {
    await captcha(conversation, ctx);
    await ctx.reply("Send a text message!");
    ctx = await conversation.wait();
    if (!ctx.message?.text) {
        await ctx.reply("You failed. Bye.");
        return;
    }
    const text0 = ctx.message.text;
    conversation.log(text0);
    do {
        await ctx.reply("Send another text message!");
        ctx = await conversation.wait();
    } while (!ctx.message?.text);
    const text1 = ctx.message.text;
    conversation.log(text1);
    await ctx.reply(`You first wrote ${text0} and then ${text1}`);
    await ctx.reply("Thanks for participating!");
}

async function captcha(conversation: MyConversation, ctx: MyContext) {
    await ctx.reply("Prove you are human! What is the answer to everything?");
    ctx = await conversation.wait();
    while (ctx.message?.text !== "42") {
        await ctx.reply("It does not look like you are human, try again!");
        ctx = await conversation.wait();
    }
    await ctx.reply("Humanity saved, you may pass!");
}

bot.use(
    session({
        initial: () => ({}),
    }),
);

bot.use(conversations());
bot.use(createConversation(example));

bot.command("enter", async (ctx) => {
    await ctx.reply("Entering conversation!");
    await ctx.conversation.enter("example"); // enter the function "example" you declared
});

bot.command("start", (ctx) => ctx.reply("Hi! Send /enter"));
bot.use((ctx) => ctx.reply("What a nice update."));

bot.start();

Node

import { Bot, Context, session } from "grammy";
import {
    type Conversation,
    type ConversationFlavor,
    conversations,
    createConversation,
} from "@grammyjs/conversations";

type MyContext = Context & ConversationFlavor;
type MyConversation = Conversation<MyContext>;

const bot = new Bot<MyContext>("");

async function example(conversation: MyConversation, ctx: MyContext) {
    await captcha(conversation, ctx);
    await ctx.reply("Send a text message!");
    ctx = await conversation.wait();
    if (!ctx.message?.text) {
        await ctx.reply("You failed. Bye.");
        return;
    }
    const text0 = ctx.message.text;
    conversation.log(text0);
    do {
        await ctx.reply("Send another text message!");
        ctx = await conversation.wait();
    } while (!ctx.message?.text);
    const text1 = ctx.message.text;
    conversation.log(text1);
    await ctx.reply(`You first wrote ${text0} and then ${text1}`);
    await ctx.reply("Thanks for participating!");
}

async function captcha(conversation: MyConversation, ctx: MyContext) {
    await ctx.reply("Prove you are human! What is the answer to everything?");
    ctx = await conversation.wait();
    while (ctx.message?.text !== "42") {
        await ctx.reply("It does not look like you are human, try again!");
        ctx = await conversation.wait();
    }
    await ctx.reply("Humanity saved, you may pass!");
}

bot.use(
    session({
        initial: () => ({}),
    }),
);

bot.use(conversations());
bot.use(createConversation(example));

bot.command("enter", async (ctx) => {
    await ctx.reply("Entering conversation!");
    await ctx.conversation.enter("example"); // enter the function "example" you declared
});

bot.command("start", (ctx) => ctx.reply("Hi! Send /enter"));
bot.use((ctx) => ctx.reply("What a nice update."));

bot.start();

Keywords

FAQs

Last updated on 29 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc