0

I'm trying to make a function in telegram bot which will add a button with a link to any text message, here is a main part of a code:

message_user_send = {}
name_of_url = {}
url_user_send = {}

############################ 
@bot.message_handler(commands=['aaa'])
def send(message):
    msg1 = bot.send_message(message.chat.id, 'Put main text')
    bot.register_next_step_handler(msg1, name_of_link)

def name_of_link(message):
    message_user_send[message.text] = message.text
    msg1 = bot.send_message(message.chat.id, 'Put name of url')
    bot.register_next_step_handler(msg1, send_alll)

def send_alll(message):
    name_of_url[message.text] = message.text
    msg1 = bot.send_message(message.chat.id, 'Put url')
    bot.register_next_step_handler(msg1, send_all_userss)   

def send_all_userss(message):
    url_user_send[message.text] = message.text
    markup = types.InlineKeyboardMarkup()
    b1 = types.InlineKeyboardButton(name_of_url[message.text], url= url_user_send[message.text])
    markup.add(b1)
    keyboard_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    bot.send_message(message.chat.id, message_user_send, reply_markup=markup)
############################

But when I am trying to put name_of_url[message.text] (the text which produses on button b1) in button it does not work and gives an error:

line 85, in send_all_userss
    b1 = types.InlineKeyboardButton(name_of_url[message.text], url= url_user_send[message.text])
                                    ~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv'

However, if I use just text in "" it works, what am I doing wrong?

What I want to achieve: enter image description here

instead of name_of_url should be text which user send before.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
  • What's in `name_of_url`? Because from the code you posted it looks like it's an empty `dict`, and that's why you're getting a `KeyError`, which basically means that there is no `https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv` in you dictionary. – Mattia Righetti Mar 03 '23 at 12:59
  • Can you also edit your original post and explain what you would expect the button to look like? – Mattia Righetti Mar 03 '23 at 13:03
  • @MattiaRighetti `name_of_url` is the text which has to be shown on the button – WinnieNotThePooh Mar 03 '23 at 13:08
  • Note that `name_of_url` takes the `message.text` from the `send_alll` function, then you proceed with `send_all_userss` which takes a different `message.text` and you use that to access `name_of_url`. If those `message.text` are different, which in this case they are, you're going to get `KeyError`. – Mattia Righetti Mar 03 '23 at 13:10
  • But, the text on the button and the url text should be different, because There has to be some particular text on the button and some link which will be opened after pressing the button. Or I do not understand something? – WinnieNotThePooh Mar 03 '23 at 13:22
  • They are different, but you're accessing `name_of_url` with the last message that the user is sending to the bot in `send_all_userss`. – Mattia Righetti Mar 03 '23 at 13:23
  • No (as I think) the `name_of_url` is global dictionary and it takes text from `send_alll` function and save it till the `send_all_userss` function where should be printed on button, or I am wrong? – WinnieNotThePooh Mar 03 '23 at 13:33
  • @MattiaRighetti I get it, I have to use `name_of_url[message.from_user.id]` instead of `name_of_url[message.text]`, it is dictionary omg, anyway, thank You for help – WinnieNotThePooh Mar 03 '23 at 13:47
  • Correct, that would make much more sense – Mattia Righetti Mar 03 '23 at 13:49

1 Answers1

0
message_user_send = {}
name_of_url = {}
url_user_send = {}

############################
@bot.message_handler(commands=['aaa'])
def send(message):
    msg1 = bot.send_message(message.chat.id, 'Введи текст который нужно отправить')
    bot.register_next_step_handler(msg1, name_of_link)

def name_of_link(message):
    message_user_send[message.from_user.id] = message.text # <--
    msg1 = bot.send_message(message.chat.id, 'Введи название ссылки')
    bot.register_next_step_handler(msg1, send_alll)

def send_alll(message):
    name_of_url[message.from_user.id] = message.text # <--
    msg1 = bot.send_message(message.chat.id, 'Введи url')
    bot.register_next_step_handler(msg1, send_all_userss)   

def send_all_userss(message):
    url_user_send[message.text] = message.text
    markup = types.InlineKeyboardMarkup()
    b1 = types.InlineKeyboardButton(name_of_url[message.from_user.id], url= url_user_send[message.text])
    markup.add(b1)
    keyboard_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    bot.send_message(message.chat.id, message_user_send[message.from_user.id], reply_markup=markup)
############################
Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '23 at 06:22