问题描述
我正在使用 Python 和 Firebase 制作电报机器人。 我被一个 KeyError 卡住了:
2021-02-12 12:39:38,687 - telegram.ext.dispatcher - ERROR - No error handlers are registered,logging exception.
Traceback (most recent call last):
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\dispatcher.py",line 442,in process_update
handler.handle_update(update,self,check,context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\conversationhandler.py",line 549,in handle_update
new_state = handler.handle_update(update,dispatcher,check_result,context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\handler.py",line 160,in handle_update
return self.callback(update,context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\bot.py",line 61,in start
print(user.val()["chat_id"])
KeyError: 'chat_id'
这是我的代码:
def start(update: Update,context: CallbackContext) -> int:
global PauseKey,PauseStart,PauseState,PauseAllKey
timeNow = datetime.Now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com','12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
user_status = "none"
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
print(user_status)
if user_status == "exist":
text_back = "You was logged in,Welcome back "
else:
data = {"name": chat_user_client,"chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%s')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',reply_markup=markup2
)
return CHOOSING
错误块:
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
并且此打印 print(user.val()["chat_id"])
在控制台中显示 690953230
。这是我的 Firebase 中存储的正确聊天 ID。
对于这一行,我尝试将 await 与 asyncio 库一起使用:
all_users = db.child("users").get()
async def start()
...
all_user = await db.child("users").get()
...
使用 async 和 await 错误正在离开,但代码没有做预期的事情。
代码应执行以下操作: 浏览所有用户数据并检查 user_chat_id 是否已经在数据库中。 如果 chat_id 已经在数据库中,那么用户存在并且我正在使用他的表键。如果用户不存在,我会创建他的表并获取他的用户密钥。
我被这个错误困了几个小时,无法自己处理,请帮忙。
Edit
现在我发现如果我的用户存在,代码就可以正常工作。 我的意思是如果我的 chat_id 已经在数据库中,我不会收到任何错误, 如果我从数据库中删除 im 数据,我会收到 keyError
解决方法
我建议你在从字典中获取元素时使用 get ;因为虽然 dict["key"] 给出错误,但当键不存在时 dict.get("key") 不会给出错误。我的建议是将错误块更新为:
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
此外,您应该进行一些重构以增强可读性。例如在上面的代码块中,下面的赋值看起来是多余的:
userKey = user.key()
您还可以提取用户存在检查作为另一种方法:
def user_exists(all_users,msg_chat_id):
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == msg_chat_id:
return true
return false
然后在 start 方法中使用它:
def start(update: Update,context: CallbackContext) -> int:
global PauseKey,PauseStart,PauseState,PauseAllKey
timenow = datetime.now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com','12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
if user_exists(all_users,update.message.chat_id):
text_back = "You was logged in,Welcome back "
else:
data = {"name": chat_user_client,"chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',reply_markup=markup2
)
return CHOOSING