grammyjs/conversations

My external API call break conversation

kotaba opened this issue · 2 comments

I have external api call, which make request to my API via axios
Data successfuly fetched and returned after call

But now my code is stuck
No return from form select or conversation wait
If i remove external call, my code works nice

  public async create_order(conversation: MyConversation, ctx: CustomContext) {
    let firstName = '';
    let lastName = '';
    let phone = '';
    let deliveryType = '';
    let city = '';
    let street = '';
    let postStation = '';
    let postBox = '';
    let houseNumber = '';
    await ctx.reply("Введіть ваше ім'я");
    firstName = await conversation.form.text();
    await ctx.reply('Введіть ваше прізвище');
    lastName = await conversation.form.text();
    await ctx.reply('Введіть ваш номер телефону');
    phone = await conversation.form.text();

    // API Service
    const service = new BotService();
    // This call via axios library ruins ALL
    const deliveryMethodsGet = await conversation.external(() =>
      service.getDeliveryMethodsSync(ctx.session.token, ctx.session.bot_id),
    );

    // Sending message next work...
    await ctx.reply('Виберіть спосіб доставки', {
      reply_markup: {
        one_time_keyboard: true,
        resize_keyboard: true,
        keyboard: [
          [
            {
              text: 'Нова Пошта',
            },
            {
              text: `Укрпошта`,
            },
          ],
        ],
      },
    });

    // SUCCESS GET DATA FROM API
    console.log(deliveryMethodsGet);

     // Example 1. User success send target string but happing nothing
    /*const deliveryMethod = await conversation.form.select(
      ['Нова Пошта'],
      (ctx) => {
        ctx.reply('Виберіть метод доставки');
      },
    );*/
     // Example 2. Wait dont work too
    ctx = await conversation.wait();

    // NO EXECUTION AFTER THIS LINE
   console.log(ctx);
   const deliveryMethod = 'Нова Пошта';

    if (deliveryMethod === 'Нова Пошта') {
      await ctx.reply('Виберіть тип доставки', {
        reply_markup: {
          one_time_keyboard: true,
          resize_keyboard: true,
          keyboard: [
            [
              {
                text: 'На відділення',
              },
              {
                text: 'Поштомат',
              },
              {
                text: "Кур'єром",
              },
            ],
          ],
        },
      });
      deliveryType = await conversation.form.select(
        ['На відділення', 'Поштомат', "Кур'єром"],
        async (ctx) => {
          await ctx.reply('Потрібно вибрати тип доставки');
        },
      );
      if (deliveryType === 'На відділення') {
        await ctx.reply('Введіть місто');
        city = await conversation.form.text();
        await ctx.reply('Введіть відділення');
        postStation = await conversation.form.text();
      }
      if (deliveryType === 'Поштомат') {
        await ctx.reply('Введіть місто');
        city = await conversation.form.text();
        await ctx.reply('Введіть поштомат');
        postBox = await conversation.form.text();
      }
      if (deliveryType === "Кур'єром") {
        await ctx.reply('Введіть місто');
        city = await conversation.form.text();
        await ctx.reply('Введіть вулицю');
        street = await conversation.form.text();
        await ctx.reply('Введіть номер будинку');
        houseNumber = await conversation.form.text();
      }
    }
    console.log(
      firstName,
      lastName,
      phone,
      deliveryMethod,
      deliveryType,
      city,
      street,
      postStation,
      postBox,
      houseNumber,
    );
    await ctx.reply('Збережено!');
  }

Axios call

async getDeliveryMethodsSync(token, bot_id) {
    const response = await axios.get(`${process.env.API_URL}/bot/${bot_id}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    const data = response.data;
    const shop = data['shop'];

    return shop['delivery_methods'];
  }

I'm facing this issue to but whit a database read :(

Fixed by #40