问题描述
当第二个人开始输入凭据时,Telegram bot (telebot) 停止响应。我相信问题出在 chat.input() 中,但我不确定也不知道如何解决。我试过的都没有奏效。当 ask_credentials() 为第二个人运行时它会冻结。顺便说一句,我是OOP新手,代码OOP够了吗?
主要代码部分:
class Chat:
global existing_chats
global local_order_count
def __init__(self,chat,request):
self.request = request
self.prevIoUs_answer = None
self.chat_params = chat
self.chat_id = chat.chat.id
self.answer = None
self.answernotrecieved = None
self.status = True
self.keep_waiting = True
for existing_chat in existing_chats:
if existing_chat.chat_id == self.chat_id:
existing_chat.keep_waiting = False
del existing_chat
def input(self,question):
if self.chat_params:
self.answer = bot_.send_message(self.chat_params.chat.id,question)
self.answernotrecieved = True
bot_.register_next_step_handler(self.answer,self.return_answer)
while self.answernotrecieved and self.keep_waiting:
time.sleep(1)
pass
if not self.answernotrecieved:
return self.answer.text
else:
return None
def return_answer(self,answer):
self.answer = answer
self.answernotrecieved = False
def say(self,message):
if self.chat_params:
bot_.send_message(self.chat_params.chat.id,message)
def __del__(self):
print('deleting chat')
class Order:
global existing_chats
global local_order_count
def __init__(self,chat):
self.chat = chat
self.context = {'city': ['Kiev',False,'^\b[a-zA-Z0-9_]+\b$'],'name': [None,'What is your full name?','\w\s+\w+'],'bdate': [None,'What is your date of birth (dd-mm-yyyy) split with "-"?','[0-9]{2}-[0-9]{2}-[0-9]{4}'],'nation': [None,'What is your nationality (e.g. Ukraine)?','\w'],'gender': [None,'What is your gender Male/Female?','passport': [None,'What is your passport number?','flight_date': [None,'What is your flight date (dd-mm-yyyy) split with "-"?','email': [None,'What is your email,we will send you the results?','^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'],'tdate': [None,False],"rdate": [None,False]}
self.notsent = True
self.ask_credentials()
def ask_credentials(self):
try:
for key,[credential,question,form] in self.context.items():
if not credential and question and self.chat.keep_waiting:
self.reply = self.chat.input(question)
if self.reply is not None:
if re.search(form,self.reply) or not form:
self.context[key] = [self.reply,form]
elif not re.search(form,self.reply):
self.ask_credentials()
elif not form:
pass
else:
break
else:
try:
for key,form] in self.context.items():
if (type(credential)==str) and question and form:
pass
except ValueError:
self.ask_credentials()
else:
self.check_credentials()
except:
pass
class Login(threading.Thread):
def __init__(self,chat_params):
threading.Thread.__init__(self)
self.chat = Chat(chat_params,self)
self.chat_params = chat_params
existing_chats.append(self.chat)
if self.chat.status:
self.chat.say(
f'Dear {chat_params.from_user.first_name},\n****')
self.chat.say('To get the AI Lab Password,contact us on instagram')
self.password = self.chat.input('Enter your AI Lab password:')
self.login(self.password)
def login(self,password):
global existing_chats
global local_order_count
if password in pass_list:
self.chat.say('Access Granted. Lets enter your credentials.')
print(f'New Order #{local_order_count} '
f'at {datetime.Now().strftime("%Y-%m-%d %H:%M")} '
f'by {self.chat_params.from_user.first_name} '
f'@{self.chat_params.from_user.username}')
local_order_count += 1
order = Order(self.chat)
else:
if password:
if '/' not in password:
self.chat.say('Access Declined. Press /start')
@bot_.message_handler(commands=["start"])
def start(chat_params):
Login(chat_params)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)