@pythonlover123

Что нужно сделать чтобы процедура password_generator выводилась в самом телеграме, а не в там где я её запустил?

#Import all libraries
import telebot
import time
import random

#Generator of passwords
def password_generator():
                chars_all = '1234567890qwertyuiopasdfghjklzxcvbnm!@#$%^&*()_+=-/?.,|<>/'
                chars_number = '1234567890'
                chars_letters = 'qwertyuiopasdfghjklzxcvbnm'

                print('1 - all chars')
                print('2 - only numbers')
                print('3 - only letters')

                type_of_choice = int(input('Type: '))

                num = int(input("Quantity of passwords: "))

                length = int(input("Length of password: "))

                if type_of_choice == 1:
                        for i in range(num):
                                password = ''

                                for j in range(length):
                                        password += random.choice(chars)
                        
                                return(password)

                if type_of_choice == 2:
                        for i in range(num):
                                password = ''

                                for j in range(length):
                                        password += random.choice(chars_number)
                        
                                return(password)

                if type_of_choice == 3:
                        for i in range(num):
                                password = ''

                                for j in range(length):
                                        password += random.choice(chars_letters)
                        
                                return(password)

#Bot token
bot = telebot.TeleBot('')

#Bot commands and their processing
@bot.message_handler(commands = ['start', 'help', 'info', 'time', 'password'])
def get_text_messages(message):
          
        if message.text == "/info":
            bot.send_message(message.from_user.id, "Привет, это мой первый бот. У него нет конкретной цели, но он может делать много чего интересного. ")

        if message.text == "/help":
            bot.send_message(message.from_user.id, "У этого бота есть несколько функций: 1 - делать краткие ссылки; 2 - проверить скорость интернета; 3 - полезные функции для работы с видео; 4 - мини-википедия; 5 - проверка погоды; Выберете из списка / то что вам нужно")
          
        if message.text == "/time":
            bot.send_message(message.from_user.id, time.asctime())

        if message.text == "/password":
            bot.send_message(message.from_user.id, password_generator())

#Bot reply on stickers
@bot.message_handler(content_types=['text', 'sticker'])
def get_sticker_messages(message):
          
        if message.text == "кчау":
            bot.send_sticker(message.from_user.id, "CAACAgIAAxkBAALgcmA1asqF7dNlbDFP8EC00v7Ej3cuAAIdAANvJ6c0V58DpQkwsgEeBA")
          
        if message.text == "всё равно":
                bot.send_sticker(message.from_user.id, "CAACAgIAAxkBAALhHmA2c0mRrfBuj2TtEqkX9lopsWWOAALxAAM8ilca2cUuuG2WefAeBA")

        if message.text == "что?":
                bot.send_sticker(message.from_user.id, "CAACAgIAAxkBAALhIWA2c9m2m3cMWBsY3tNbGu4bAcfjAAIEAQACPIpXGtW8yebeUgveHgQ")

        if message.text == "Привет":
                bot.send_sticker(message.from_user.id, "CAACAgIAAxkBAALhJGA2dGzFlfhtuC_re4wKYQWsrFBwAALDAAOuth4tJC--NTITiOgeBA")		

#def index_weight_growth():


bot.polling(none_stop=True, interval=0)
  • Вопрос задан
  • 70 просмотров
Решения вопроса 1
jerwright
@jerwright
while True: coding()
Как-то так:
def password_generator(message):
    global chars_all
    global chars_number
    global chars_letters
    chars_all = '1234567890qwertyuiopasdfghjklzxcvbnm!@#$%^&*()_+=-/?.,|<>/'
    chars_number = '1234567890'
    chars_letters = 'qwertyuiopasdfghjklzxcvbnm'
    mes='1 - all chars\n2 - only numbers\n3 - only letters'
    bot.send_message(message.from_user.id, text=mes)
    
    type_of_choice=bot.send_message(message.from_user.id, "Type:")
    bot.register_next_step_handler(type_of_choice, choosing_type)
def choosing_type(message):
    #Получаем Type пароля
    global type_of_choice
    type_of_choice=int(message.text)
    num=bot.send_message(message.from_user.id, "Quantity of passwords:")
    bot.register_next_step_handler(num, choosing_num)
def choosing_num(message):
    #Получаем количество паролей
    global num
    num = int(message.text)
    length=bot.send_message(message.from_user.id, "Length of password:")
    bot.register_next_step_handler(length, choosing_length)
def choosing_length(message):
    #Получаем длину пароля
    global length
    length=int(message.text)
    password = ''
    for i in range (num):
        if type_of_choice == 1:
            for j in range(length):
                    password += random.choice(chars_all)
        if type_of_choice == 2:
            for j in range(length):
                    password += random.choice(chars_number)
        if type_of_choice == 3:
            for j in range(length):
                    password += random.choice(chars_letters)
        password=password+'\n'
    #Получаем все пароли
    return bot.send_message(message.from_user.id, password)


Ваша проверка на команду теперь выглядет вот так:
@bot.message_handler(commands = ['start', 'help', 'info', 'time', 'password'])
def get_text_messages(message):
    if message.text == "/password":
        #Вызываем начало функции, где спрашивается Type
        password_generator(message)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы