微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以在 python3 电报 api 中创建个性化的 InlineKeyboard 吗?

如何解决我可以在 python3 电报 api 中创建个性化的 InlineKeyboard 吗?

我一直在写一个关于生成密码的 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 举报,一经查实,本站将立刻删除。