问题描述
我一直在写一个关于生成密码的 Telegram Bot。但稍后我想根据用户之前所做的事情生成按钮。 我把代码写得更清楚我在说什么:
import telebot
from telebot import types
import random
TOKEN = '**********************************'
bot = telebot.TeleBot(TOKEN)
#diccionaris i llistes
minuscules = "abcdefghijklmnopqrstvwxyzç"
majuscules = "ABCDEFGHIJKLMnopQRSTVWXYZ"
simbols = "[]{};/&%$€?!"
numeros = "0123456789"
all = minuscules + majuscules + simbols + numeros
user = {}
length = {}
passw = {}
@bot.message_handler(commands=['generate_password'])
def pass_length(missatge):
resposta = bot.send_message(missatge.chat.id,"Write the length of your future password")
bot.register_next_step_handler(resposta,pass_creation)
def pass_creation(missatge):
length = int(missatge.text)
len[missatge.from_user.username]=length
password = "".join(random.sample(all,length))
passw[missatge.from_user.username]=password
bot.send_message(missatge.chat.id,password)
markup = types.ReplyKeyboardMarkup(row_width=2,one_time_keyboard=True)
markup.add(types.KeyboardButton("Yes"),types.KeyboardButton("No"))
resposta=bot.send_message(missatge.chat.id,"Do you want to save this password?",reply_markup=markup)
bot.register_next_step_handler(resposta,saving)
def saving(missatge):
if missatge.text == "Yes":
resposta = bot.send_message(missatge.chat.id,"Well! Now you will have to match this password,write down where you will use it")
bot.register_next_step_handler(resposta,saving_2)
else:
resposta = bot.send_message(missatge.chat.id,"We will try to give you another password,you have to do /generate_password again")
bot.register_next_step_handler(resposta,pass_length)
def saving_2(missatge):
if user == {}:
user[missatge.from_user.username]=[]
y = [missatge.text]
d = y.append(passw[missatge.from_user.username])
x = user[missatge.from_user.username].append(y)
elif user != {} and missatge.from_user.username not in user.keys():
user[missatge.from_user.username]=[]
y = [missatge.text]
d = y.append(passw[missatge.from_user.username])
x = user[missatge.from_user.username].append(y)
elif user[missatge.from_user.username] != []:
y = [missatge.text]
d = y.append(passw[missatge.from_user.username])
x = user[missatge.from_user.username].append(y)
@bot.message_handler(commands=['see_password'])
def see_password(missatge):
resposta = bot.send_message(missatge.chat.id,"This user have passwords for")
for item in user[missatge.from_user.username]:
bot.send_message(missatge.chat.id,user[missatge.from_user.username][user[missatge.from_user.username].index(item)][0])
bot.register_next_step_handler(resposta,choose_passw)
def choose_passw(missatge):
markup=types.InlineKeyboardMarkup(row_width=2,one_time_keyboard=True)
for i in user[missatge.from_user.username]:
markup.add(types.InlineKeyboardButton(user[missatge.from_user.username][user[missatge.from_user.username].index(i)][0][0]))
bot.send_message(missatge.chat.id,"Choose:",reply_markup=markup)
bot.polling()
在choose_passw 我尝试为列表中的元素创建一个InlineKeyboard,但它不起作用,我不知道为什么。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)