Skip to content

Instantly share code, notes, and snippets.

@shanginn
Created December 5, 2022 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanginn/e1fd62c1e4f51cca42ef5bf7581000fd to your computer and use it in GitHub Desktop.
Save shanginn/e1fd62c1e4f51cca42ef5bf7581000fd to your computer and use it in GitHub Desktop.
const { Telegraf } = require('telegraf')
const bot = new Telegraf('TOKEN')
// Create an object to store the user's information
const userData = {}
// Set up a command to start the conversation with the user
bot.command('start', ctx => {
// Define the userData object for the current user
userData[ctx.from.id] = {}
ctx.reply('Hello! I am a calorie calculator bot. To get started, please enter your age:')
})
// Listen for the user's age and store it in the userData object
bot.on('text', ctx => {
// Check if the user has already provided their age
if (!userData[ctx.from.id].age) {
userData[ctx.from.id].age = ctx.message.text
ctx.reply('Thanks! Now please enter your height in centimeters:')
} else if (!userData[ctx.from.id].height) {
userData[ctx.from.id].height = ctx.message.text
ctx.reply('Thanks! Now please enter your weight in kilograms:')
} else if (!userData[ctx.from.id].weight) {
userData[ctx.from.id].weight = ctx.message.text
ctx.reply('Thanks! Now please enter your gender (M or F):')
} else {
userData[ctx.from.id].gender = ctx.message.text
// Calculate the daily calorie usage using the user's information
const age = userData[ctx.from.id].age
const height = userData[ctx.from.id].height
const weight = userData[ctx.from.id].weight
const gender = userData[ctx.from.id].gender
// The formula for calculating daily calorie usage varies depending on the gender of the user
let calorieUsage
if (gender === 'M') {
calorieUsage = 66 + (13.7 * weight) + (5 * height) - (6.8 * age)
} else {
calorieUsage = 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age)
}
// Send the calculated calorie usage to the user
ctx.reply(`Your daily calorie usage is ${calorieUsage}.`)
}
})
bot.startPolling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment