grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
1.98k stars 105 forks source link

Session key is undefined on payment. #161

Closed Satont closed 2 years ago

Satont commented 2 years ago

Hello. It looks as if the session key cannot be resolved for payment. This happens after I click "pay 120 rub" in the dialog box from telegram, after I filled my card on a third-party service (UKassa)

image

Middlewres:

session:

export const middleware = () =>
  session({
    initial: () => ({
      user: {
        isRegistered: false,
      },
    }),
    storage,
  });

i18n:

export const middleware = () =>
  useFluent({
    fluent,
    localeNegotiator: (ctx: Context) =>
      ctx.session.user.languageCode || ctx?.from?.language_code,
  } as GrammyFluentOptions & {
    localeNegotiator: LocaleNegotiator<Context>;
  });

Also composer code, if it will be helpful:

composer.command('test', async ctx => {
await ctx.replyWithInvoice(
        ctx.t('subscription_fill_up_balance_title', { count }),
        ctx.t('subscription_fill_up_balance_title', { count }),
        JSON.stringify({
          unique_id: `${ctx.from!.id}_${Number(new Date())}`,
          provider_token: config.BOT_PAYMENT_PROVIDER_TOKEN,
        }),
        config.BOT_PAYMENT_PROVIDER_TOKEN,
        'RUB',
        [{ label: count.toString(), amount: count * 100 }]
      );
})

composer.on('pre_checkout_query', (ctx) => ctx.answerPreCheckoutQuery(true));
composer.on(':successful_payment', async (ctx, next) => {
  await ctx.reply('SuccessfulPayment');
});

Stack:

Error while handling update 479364603:
Unknown error: Error: Cannot access session data because the session key was undefined!
    at Context.get (/home/satont/Projects/funpay/apps/bot/node_modules/grammy/out/convenience/session.js:67:27)
    at localeNegotiator (/home/satont/Projects/funpay/apps/bot/src/middlewares/setup-i18n.middleware.ts:14:11)
    at negotiateLocale (/home/satont/Projects/funpay/apps/bot/node_modules/@moebius/grammy-fluent/src/middleware.ts:95:15)
    at fluentMiddleware (/home/satont/Projects/funpay/apps/bot/node_modules/@moebius/grammy-fluent/src/middleware.ts:81:11)
    at /home/satont/Projects/funpay/apps/bot/node_modules/grammy/out/composer.js:61:41
    at /home/satont/Projects/funpay/apps/bot/node_modules/grammy/out/composer.js:71:19
    at /home/satont/Projects/funpay/apps/bot/src/middlewares/setup-logger.middleware.ts:16:10
    at /home/satont/Projects/funpay/apps/bot/node_modules/grammy/out/composer.js:61:41
    at /home/satont/Projects/funpay/apps/bot/node_modules/grammy/out/composer.js:71:19
    at AsyncLocalStorage.run (node:async_hooks:320:14)

Node: 16 Grammy: 1.6.2

Satont commented 2 years ago

With session key binded to from?.id it works, so not a bug? (default is chat?.id)

function getSessionKey(ctx: Context): string | undefined {
  return ctx.from?.id.toString();
}
EdJoPaTo commented 2 years ago

the discussion in #89 might be relevant

KnorpelSenf commented 2 years ago

Correct, this is not a bug, and correct again, #89 explains why we should not change the default behaviour here. (TL;DR: potential data loss on webhooks.)

Satont commented 2 years ago

Is this documented anywhere? If so, I have no further questions.

KnorpelSenf commented 2 years ago

Is what exactly documented? Things about the session key are here: https://grammy.dev/plugins/session.html#session-keys

KnorpelSenf commented 2 years ago

Closing because of inactivity.