telebot prompt

example of code fot tg bot prompt

 

 import telebot

from telebot import types

bot = telebot.TeleBot('5880261952:AAHBDo6Tf5pDpBVVmf27Q-2dATVVi_rhjoU')

# Define a dictionary to store user data
user_data = {}

# Define a function to validate the user's age
def is_valid_age(age):
try:
int(age)
return True
except ValueError:
return False

# Define a function to validate the user's email
def is_valid_email(email):
if '@' in email and '.' in email:
return True
else:
return False

# Define a handler for the '/start' command
@bot.message_handler(commands=['start'])
def start_handler(message):
chat_id = message.chat.id
# Ask the user for their name
bot.send_message(chat_id, "Hi! What's your name?")
bot.register_next_step_handler(message, get_name)

# Define a function to ask the user for their age
def get_name(message):
chat_id = message.chat.id
name = message.text
user_data[chat_id] = {'name': name}
# Ask the user for their age
bot.send_message(chat_id, f"Nice to meet you, {name}! How old are you?")
bot.register_next_step_handler(message, get_age)

# Define a function to ask the user for their email
def get_age(message):
chat_id = message.chat.id
age = message.text
if not is_valid_age(age):
bot.send_message(chat_id, "Sorry, that's not a valid age. Please enter a number.")
bot.register_next_step_handler(message, get_age)
return
user_data[chat_id]['age'] = age
# Ask the user for their email
bot.send_message(chat_id, "Great! What's your email address?")
bot.register_next_step_handler(message, get_email)

# Define a function to finish collecting user data and display it back to the user
def get_email(message):
chat_id = message.chat.id
email = message.text
if not is_valid_email(email):
bot.send_message(chat_id, "Sorry, that's not a valid email address. Please try again.")
bot.register_next_step_handler(message, get_email)
return
user_data[chat_id]['email'] = email
# Display the user's data back to them
data_string = f"Name: {user_data[chat_id]['name']}\nAge: {user_data[chat_id]['age']}\nEmail: {user_data[chat_id]['email']}"
bot.send_message(chat_id, f"Thanks for sharing your info!\n\n{data_string}")

# Start the bot
bot.polling()

Post a Comment

0 Comments