Answer the question
In order to leave comments, you need to log in
Endless loop in simple Telegram Bot?
import telebot
import random
bot = telebot.TeleBot('TOKEN')
print("Bot started")
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, '/random')
chars = '1234567890'
number = 10
length = 6
for n in range(number):
password = ''
for i in range(length):
password += random.choice(chars)
@bot.message_handler(content_types=['text'])
def send_text(message):
if message.text.lower() == '/random':
bot.send_message(message.chat.id, 'Your code: ' + password)
bot.polling()
Answer the question
In order to leave comments, you need to log in
Redo the code a bit, since at the moment it generates random code only once.
In the function itself, add before
bot.send_message(message.chat.id, 'Your code: ' + password)
password = ''.join([str(random.randint(0, 9)) for x in range(length)])
for n in range(number):
password = ''
for i in range(length):
password += random.choice(chars)
import telebot
import random
bot = telebot.TeleBot('TOKEN')
print("Bot started")
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, '/random')
chars = '1234567890'
number = 10
length = 6
def password():
for n in range(number):
password = ''
for i in range(length):
password += random.choice(chars)
return password
@bot.message_handler(content_types=['text'])
def send_text(message):
if message.text.lower() == '/random':
bot.send_message(message.chat.id, 'Your code: ' + password())
bot.polling()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question