Provide an example using Node.js (not TypeScript) where project files are organized to include a separate file for a scene that asks the user three questions. Each question should validate user input: one as text, one as a number, and one as a boolean. After all inputs are validated, the program should exit the scene and write the user inputs to a file. Include the command file that invokes the scene.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help create an example with separate files for a scene that collects and validates three different types of inputs. Here's how we can organize it:
const { Scenes } = require('telegraf');
const fs = require('fs/promises');
// Create survey scene
const surveyScene = new Scenes.BaseScene('survey');
// Initial entry point
surveyScene.enter((ctx) => {
ctx.session.surveyData = {};
ctx.reply('Please answer 3 questions. First question: What is your favorite color?');
});
// Handle text input for color
surveyScene.on('text', async (ctx) => {
if (!ctx.session.surveyData.color) {
if (ctx.message.text.length < 3) {
return ctx.reply('Please enter a valid color (at least 3 characters)');
}
ctx.session.surveyData.color = ctx.message.text;
return ctx.reply('Great! Second question: How many hours do you sleep per night? (enter a number)');
}
if (!ctx.session.surveyData.sleepHours) {
const hours = Number(ctx.message.text);
if (isNaN(hours) || hours < 0 || hours > 24) {
return ctx.reply('Please enter a valid number between 0 and 24');
}
ctx.session.surveyData.sleepHours = hours;
return ctx.reply('Last question: Do you exercise regularly? (yes/no)');
}
if (!ctx.session.surveyData.exercises) {
const answer = ctx.message.text.toLowerCase();
if (answer !== 'yes' && answer !== 'no') {
return ctx.reply('Please answer with yes or no');
}
ctx.session.surveyData.exercises = answer === 'yes';
// Save data to file
try {
const data = JSON.stringify(ctx.session.surveyData, null, 2);
await fs.writeFile(`survey_${ctx.from.id}.json`, data);
await ctx.reply('Thank you for completing the survey! Your responses have been saved.');
return ctx.scene.leave();
} catch (error) {
console.error('Error saving survey:', error);
await ctx.reply('There was an error saving your responses. Please try again.');
return ctx.scene.leave();
}
}
});
module.exports = surveyScene;
Users can start the survey by sending the /survey command. The scene will:
Ask for their favorite color (validates minimum 3 characters)
Ask for their sleep hours (validates number between 0-24)
Ask if they exercise (validates yes/no response)
After all questions are answered validly, the responses are saved to a JSON file named survey_[user_id].json and the scene exits.
The scene maintains state using the session middleware, and validation is performed at each step. If validation fails, the user is prompted to try again for that specific question. The data is only saved once all three questions have been answered correctly.
This implementation follows Telegraf's scene system while providing proper input validation and file organization. Each component is separated into its own file for better maintainability and clarity.