bug: conversation.external() interrupts conversation (possibly related to Webhooks?)
vitorleo opened this issue · 3 comments
vitorleo commented
The code below breaks when replacing the line
let futures = await util.fetchData('^VIX');
with
let futures = await conversation.external(() => fetchFutures());
returning the error:
Error: Request timed out after 10000 ms
at Timeout._onTimeout (.........\node_modules\grammy\out\convenience\webhook.js:55:24)
require('dotenv').config()
const { TELEGRAM_BOT_TOKEN } = process.env
const { Bot, Context, session, webhookCallback, InputFile } = require("grammy");
const { conversations, createConversation, ConversationForm } = require("@grammyjs/conversations");
const axios = require('axios');
const bot = new Bot(TELEGRAM_BOT_TOKEN);
// Create multi session
bot.use(session({
initial: () => ({
currentAlert: null,
alerts: []
})
}));
// Simulates extenal API to be used with conversation.external()
const fetchFutures = async function() {
let url = 'https://www.cboe.com/us/futures/api/get_quotes_combined/?symbol=VX&rootsymbol=null';
await axios.get(url)
.then(response => {
obj = response.data;
})
.catch(error => {
console.log('error');
obj = error
});
return obj;
}
// Function to interrupt and exit the conversation and remover keyboard
const endConversation = async function (ctx) {
const msg = 'Goodbye. Type /thl, to start over.'
await ctx.reply(msg, {
reply_markup: { remove_keyboard: true },
});
}
// -------- CONVERSATION start ---------
const conversationTHL = async function (conversation, ctx) {
await ctx.reply('Calls or puts?', {
reply_markup: {
one_time_keyboard: true,
keyboard: [[{text:'calls'}], [{text:'puts'}]],
resize_keyboard: true
}
})
let resposta = await conversation.form.select(['calls', 'puts', 'fim']);
// Without conversation.external() it works.
// let futures = await util.fetchData('^VIX');
// ADDING conversation.external() interrups the conversation.
let futures = await conversation.external(() => fetchFutures());
console.log(futures)
if (resposta.toLowerCase() == 'fim') { endConversation(ctx); return }
// LOWER strike question
await ctx.reply(`LOWER strike with ${resposta}?`, {
reply_markup: {
one_time_keyboard: true,
keyboard: [[{text:'1'}, {text:'2'}, {text:'3'}, {text:'4'}]],
resize_keyboard: true
}
});
resposta = await conversation.form.select(['1', '2', '3', '4', 'fim']);
if (resposta.toLowerCase() == 'fim') { endConversation(ctx); return }
// HIGHER strike question
await ctx.reply(`HIGHER strike with ${resposta}?`, {
reply_markup: {
one_time_keyboard: true,
keyboard: [[{text:'4'}, {text:'3'}, {text:'2'}, {text:'1'}]],
resize_keyboard: true
}
});
resposta = await conversation.form.select(['1', '2', '3', '4', 'fim']);
if (resposta.toLowerCase() == 'fim') { endConversation(ctx); return }
await ctx.reply('Ok. If you got here that means the conversation works!');
}
// -------- CONVERSATION end --------
// Add conversation to bot
bot.use(conversations());
bot.use(createConversation(conversationTHL));
bot.command("thl", async (ctx) => {
await ctx.conversation.enter("conversationTHL");
});
// Webhooks callback handling
const handleUpdate = webhookCallback(bot, 'express')
// AZURE functions HTTP trigger
module.exports = async function (context, req) {
console.log('-----------------------------');
context.log('JavaScript HTTP trigger function processed a request.');
// Mapping objects to work with express adapter
req.header = function(k) {
return context.res.headers[k]
}
context.res.end = function() {
return context.res = {
status: 200,
body: ''
}
}
try {
await handleUpdate(req, context.res)
} catch (err) {
console.error(err)
}
console.log('END -----------------------------');
}
yn4v4s commented
#32 I can confirm this is happening, I'm facing the same when I read data from DB using conversation.external()
yn4v4s commented
Just tested this again. It's not related to webhooks, conversations break using grammy/runner as well if you use conversation.external()
KnorpelSenf commented
Fixed by #40