无法在Telegram机器人中嵌入具有链接的按钮。

huangapple go评论69阅读模式
英文:

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?

What I want to achieve:
无法在Telegram机器人中嵌入具有链接的按钮。

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)
############################

huangapple
  • 本文由 发表于 2023年3月3日 20:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定