首页 > 解决方案 > 注册下一步电报 api

问题描述

我正在研究 bot,它正在使用它自己的用户系统。我想设置一种登录和注册功能。在启动机器人时,它建议使用内联键盘选择其中一个功能,我希望我的机器人从回调处理程序开始登录或注册序列。它必须获取用户的位置、登录名和密码,但是当我开始制作登录功能时,我意识到我无法调用 register_next_step_handler 函数。有什么方法可以像我计划的那样使用它,或者任何其他方式?

代码:

@bot.message_handler(commands=['start'])
def send_welcome(message):
    markup = types.InlineKeyboardMarkup()
    buttonA = types.InlineKeyboardButton('LOG IN', callback_data='log_in')
    buttonB = types.InlineKeyboardButton('SIGN UP', callback_data='sign_up')
    markup.row(buttonA)
    markup.row(buttonB)
    bot.send_message(message.from_user.id, "Please, choose the option below", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def handle(call):
    if str(call.data) == "log_in":
        bot.send_message(call.message.chat.id, "LOG IN SEQUENCE")
        bot.send_message(call.message.chat.id, "What is your position?")
        bot.register_next_step_handler(call.message.chat, get_pos)
    elif str(call.data) == "sign_up":
        bot.send_message(call.message.chat.id, "SIGN UP SEQUENCE")
    bot.answer_callback_query(call.id)

def get_pos(call):
    global pos
    pos = call.data
    bot.send_message(call.message.chat.id, "Your position: " + pos)

错误:

Traceback (most recent call last):
  File "/home/firephoenix/PycharmProjects/pythonProject/main.py", line 35, in <module>
    bot.polling(none_stop=True, interval=0)
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/__init__.py", line 619, in polling
    self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/__init__.py", line 678, in __threaded_polling
    raise e
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/__init__.py", line 641, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/util.py", line 130, in raise_exceptions
    raise self.exception_info
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/util.py", line 82, in run
    task(*args, **kwargs)
  File "/home/firephoenix/PycharmProjects/pythonProject/main.py", line 21, in handle
    bot.register_next_step_handler(call.message.chat, get_pos)
  File "/home/firephoenix/.local/lib/python3.8/site-packages/telebot/__init__.py", line 2322, in register_next_step_handler
    chat_id = message.chat.id
AttributeError: 'Chat' object has no attribute 'chat'

标签: python

解决方案


推荐阅读