提问人:Vlad F 提问时间:8/24/2023 更新时间:8/24/2023 访问量:79
有没有办法在 Aiogram 中使用来自机器人和用户的消息的 while 和 for 循环?
Are there ways to use while and for loops with messages from bot and user in Aiogram?
问:
我正在使用 Aiogram 将一个在控制台中工作的程序转移到电报机器人,我不知道从哪里开始一个函数。在这里:
def check():
print('Here are your dictionaries:')
retrieve_dictionaries()
dict_name = input('Select a dictionary: ''')
cur = conn.cursor()
cur.execute('''SELECT * from {tab};'''.format(tab = dict_name))
columns = list(map(lambda x: x[0], cur.description))
print('Here are possible directions:','\n','1.',columns[1],'--',columns[2],'\n','2.',columns[2],'--',columns[1])
direction = input ('From where do we start? Choose 1 or 2: ')
if direction == '1':
box = int(input('From which box do we take the cards? Choose from 1 to 4: '))
cur.execute('''SELECT * from {tab} WHERE box12 == {box_value} ORDER BY RANDOM()'''.format(tab = dict_name, box_value = box))
checkwords = cur.fetchall()
checked = len(checkwords)
print(checked)
print('Excellent! There are',checked, 'words to learn. Let us start!')
print(checkwords)
id_correct = []
id_forgotten = []
id_remembered = []
while checked > 0:
for i in checkwords:
if i[0] in id_correct:
continue
elif i[0] in id_forgotten:
print(i[1])
answer = input('Insert answer: ')
if answer == i[2]:
print('Correct!')
checked = checked - 1
id_remembered.append(i[0])
else:
print('Nope, the correct answer is:', i[2])
else:
print(i[1])
answer = input('Insert answer: ')
if answer == i[2]:
print('Correct!')
checked = checked - 1
id_correct.append(i[0])
else:
print('Nope, the correct answer is:', i[2])
id_forgotten.append(i[0])
print(id_correct)
print(id_forgotten)
for i in id_correct:
cur.execute('''UPDATE {tab} SET box12 = box12 + 1 WHERE id == {word_id}'''.format(tab = dict_name, word_id = i))
if direction == '2':
box = int(input('From which box do we take the cards? Choose from 1 to 4: '))
cur.execute('''SELECT * from {tab} WHERE box21 == {box_value} ORDER BY RANDOM()'''.format(tab = dict_name, box_value = box))
checkwords = cur.fetchall()
checked = len(checkwords)
print(checked)
print('Excellent! There are',checked, 'words to learn. Let us start!')
print(checkwords)
id_correct = []
id_forgotten = []
id_remembered = []
while checked > 0:
for i in checkwords:
if i[0] in id_correct:
continue
elif i[0] in id_forgotten:
print(i[2])
answer = input('Insert answer: ')
if answer == i[1]:
print('Correct!')
checked = checked - 1
id_remembered.append(i[0])
else:
print('Nope, the correct answer is:', i[2])
else:
print(i[2])
answer = input('Insert answer: ')
if answer == i[1]:
print('Correct!')
checked = checked - 1
id_correct.append(i[0])
else:
print('Nope, the correct answer is:', i[1])
id_forgotten.append(i[0])
print(id_correct)
print(id_forgotten)
for i in id_correct:
cur.execute('''UPDATE {tab} SET box21 = box21 + 1 WHERE id == {word_id}'''.format(tab = dict_name, word_id = i))
conn.commit()
cur.close()
有各种各样的循环,我不明白 FSM 是否可以用它做任何事情(如果可以,我很乐意得到一些建议)。也许还有其他一些方法可以确保按顺序执行该功能?for
while
我试图将它作为一个状态执行,并将其划分为不同的状态,但是没有成功。
答:
0赞
dotheM
8/24/2023
#1
如果不访问您的完整代码库,我很难将所有这些转换为 Aiogram 中的功能 - 但以下是我可以根据经验为您提供的建议:
首先,创建一个 models.py 以包含所有数据库函数和所有查询。请记住,一切都需要异步。
我check()将是一个命令/check。通过为每个任务创建一个小函数来使其更干净,而不是使用这样的管道终结点。我以前曾经有过这个习惯,而我以前没有,这是一个 PITA 来调试错误或将来拥有模块化。assume
检查的绝对开始是这样的:
@dp.message_handler(commands=['check'])
async def check(message: types.Message):
例如,您的数据库方法如下:
def get_dictionaries():
return
def get_words_from_box(dict_name, box_column, box_value):
return
def update_word_box(dict_name, word_id, box_column):
return
对于这样的行:
if direction == '1':
// LOTS OF CODE
例如,做
if direction == '1':
directionOne()
对不起,我没有为你做这一切,但我想帮助你开始。我希望这能让你在如何为你的 Aiogram 机器人将代码拆分为更模块化和更干净的风格方面找到正确的方向。
编辑:如果我缺少什么来回答你,请告诉我
评论