feat(bot): implement conversation flow for start command
add conversation handler for start command with name input and welcome message update locales for new conversation flow add @grammyjs/conversations dependency
This commit is contained in:
@@ -1 +1,2 @@
|
||||
start = Hello, how can I help you
|
||||
start-ask-name = Hello, what is your name?
|
||||
start-welcome = Welcome, { $user }!
|
||||
@@ -1 +1,2 @@
|
||||
start = Привет, как я могу тебе помочь
|
||||
start-ask-name = Привет, как тебя зовут
|
||||
start-welcome = Привет, { $user }!
|
||||
@@ -11,6 +11,7 @@
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@grammyjs/conversations": "^2.1.0",
|
||||
"@grammyjs/i18n": "^1.1.2",
|
||||
"@hono/node-server": "^1.15.0",
|
||||
"@repo/config": "*",
|
||||
|
||||
3
apps/bot/src/conversations/consts.ts
Normal file
3
apps/bot/src/conversations/consts.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export enum ConversationSlug {
|
||||
START = 'start',
|
||||
}
|
||||
2
apps/bot/src/conversations/index.ts
Normal file
2
apps/bot/src/conversations/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './consts';
|
||||
export * from './start.conversation';
|
||||
23
apps/bot/src/conversations/start.conversation.ts
Normal file
23
apps/bot/src/conversations/start.conversation.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Conversation, createConversation } from '@grammyjs/conversations';
|
||||
import { BotContext } from '../types';
|
||||
import { ConversationSlug } from './consts';
|
||||
|
||||
async function enterConversation(conversation: Conversation, ctx: BotContext) {
|
||||
const askNameText = await conversation.external((ctx: BotContext) =>
|
||||
ctx.t('start-ask-name')
|
||||
);
|
||||
|
||||
await ctx.reply(askNameText);
|
||||
|
||||
const { message } = await conversation.waitFor('message:text');
|
||||
const welcomeText = await conversation.external((ctx: BotContext) =>
|
||||
ctx.t('start-welcome', { user: message.text })
|
||||
);
|
||||
|
||||
await ctx.reply(welcomeText);
|
||||
}
|
||||
|
||||
export const startConversation = createConversation(
|
||||
enterConversation,
|
||||
ConversationSlug.START
|
||||
);
|
||||
@@ -8,6 +8,8 @@ import { BotContext } from './types';
|
||||
import { upsertUser } from './middlewares/upsert-user';
|
||||
import { onError } from './middlewares/on-error';
|
||||
import { I18n } from '@grammyjs/i18n';
|
||||
import { conversations } from '@grammyjs/conversations';
|
||||
import { ConversationSlug, startConversation } from './conversations';
|
||||
|
||||
const bot = new Bot<BotContext>(config.BOT_TOKEN);
|
||||
const storage = new PrismaStorageAdapter();
|
||||
@@ -20,6 +22,7 @@ const i18n = new I18n<BotContext>({
|
||||
defaultLocale: 'ru',
|
||||
useSession: true, // whether to store user language in session
|
||||
directory: 'locales', // Load all translation files from locales/.
|
||||
fluentBundleOptions: { useIsolating: false },
|
||||
localeNegotiator: async (ctx) => {
|
||||
const session = await ctx.session;
|
||||
return session.__language_code ?? ctx.from?.language_code ?? 'ru';
|
||||
@@ -27,9 +30,11 @@ const i18n = new I18n<BotContext>({
|
||||
});
|
||||
|
||||
bot.use(i18n);
|
||||
bot.use(conversations());
|
||||
bot.use(startConversation);
|
||||
|
||||
bot.command('start', async (ctx) => {
|
||||
await ctx.reply(ctx.t('start'));
|
||||
await ctx.conversation.enter(ConversationSlug.START);
|
||||
});
|
||||
|
||||
if (config.MODE === 'webhook') {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { ConversationFlavor } from '@grammyjs/conversations';
|
||||
import { I18nFlavor } from '@grammyjs/i18n';
|
||||
import { Context, LazySessionFlavor } from 'grammy';
|
||||
|
||||
// config your session data
|
||||
export type SessionData = { __language_code?: string };
|
||||
export type BotContext = Context & LazySessionFlavor<SessionData> & I18nFlavor;
|
||||
export type BotContext = ConversationFlavor<
|
||||
Context & LazySessionFlavor<SessionData> & I18nFlavor
|
||||
>;
|
||||
|
||||
Reference in New Issue
Block a user