英文:
Cannot embed button with a link in Telegram Bot
问题
您试图在Telegram机器人中创建一个函数,该函数将在任何文本消息中添加一个带有链接的按钮。以下是代码的主要部分:
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)
但是,当我尝试在按钮 b1
中放置 name_of_url[message.text]
(在按钮 b1
上生成的文本)时,它不起作用并出现错误:
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'
但是,如果我只使用在""
之间的文本,它可以工作。我做错了什么?
我想要实现的效果是,而不是使用name_of_url
,应该使用用户之前发送的文本。
英文:
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?
instead of name_of_url
should be text which user send before.
答案1
得分: 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, 'Enter the text to send')
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, 'Enter the link name')
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, 'Enter the 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)
############################
英文:
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)
############################
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论