我遇到一个错误 - “原始描述:请求太多:5 次后重试(此错误的背景信息:https: //core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this)” - 所以我无法通过机器人快速向用户发送消息?出现错误 429。我可以配置机器人,使其每秒发送一条消息,并且不会出现任何错误吗?
主页
/
user-596621
wanderer's questions
我正在制作一个日程通知机器人,我必须接触它们классы
,但我从未遇到过它们:
import sqlite3
class RequestsInBD:
def __init__(self):
self.connection = sqlite3.Connection('datebase_of_users.db')
self.cursor = self.connection.cursor()
def _commit_and_close_bd(self, connection):
connection.commit()
connection.close()
def check(self, user_id):
all_id = self.cursor.execute('SELECT id FROM grade_of_users').fetchall()
self._commit_and_close_bd(self.connection)
for item in all_id:
if user_id in item:
return False
return True
def add_user(self, user_id, grade):
self.cursor.execute(f'INSERT INTO grade_of_users (id, grade, notifications) VALUES (?, ?, ?)', (user_id, grade, 1))
self._commit_and_close_bd(self.connection)
def return_user_grade(self, user_id):
self.cursor.execute(f'SELECT id, grade FROM grade_of_users WHERE id = {user_id}')
grade = self.cursor.fetchone()[1]
self._commit_and_close_bd(self.connection)
return grade if grade else False
def get_users(self):
self.cursor.execute('SELECT id, grade, notifications FROM grade_of_users')
res = self.cursor.fetchall()
self._commit_and_close_bd(self.connection)
return res
def new_grade(self, grade, user_id):
self.cursor.execute(f'UPDATE grade_of_users SET grade = ? WHERE id = ?', (grade, user_id))
self._commit_and_close_bd(self.connection)
代码看起来可以工作,但是写得正确吗? Chatgpt 说原则上一切都很好,但我有点不确定他的话。 (此代码出现在与主程序分开的文件中,我从主程序导入此类,并且每次调用时我都会创建此类的对象(实例),我不确定这是否也是正确的)
我正在创建一个像游戏一样的机器人,您可以通过数据库更改昵称,但重点是当您第一次启动此功能时:
@router.callback_query(F.data == 'rename')
async def rename_user(callback: CallbackQuery, state: FSMContext):
await state.set_state(user_states.state_for_name)
await edit_message(path=r'other_photo//rename.png', caption='Введите новое имя в чат', callback=callback)
@router.message(user_states.state_for_name)
async def end_of_rename_user(message: Message, state: FSMContext, bot: Bot):
await state.set_state(None)
await state.set_data({})
new_user_name = message.text
bool_new_user_name = main_func_bd('check_name', message.from_user.id, user_name=new_user_name)
skin = main_func_bd('valid_skin', message.from_user.id)
if bool_new_user_name:
main_func_bd('rename', message.from_user.id, user_name=new_user_name)
text = main_func_bd('profile', message.from_user.id)
elif not bool_new_user_name:
profile = main_func_bd('profile', message.from_user.id)
text = f'Такой ник уже существует!\n{profile}'
await bot.edit_message_media(chat_id=message.chat.id, message_id= message.message_id - 1, media=InputMediaPhoto(media=FSInputFile(fr'skins/{skin}.png'), caption=text), reply_markup=profile_markup)
await bot.delete_message(message.chat.id, message.message_id)
昵称发生变化,再次运行时,会出现未找到编辑消息的错误:
aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: message to edit not found
除了这个 message_id 之外,其他一切都很完美。我试图从message_id中减去一个,就像最后一条消息应该在发送玩家昵称之前改变一样,但是当第一次启动该函数时就会发生错误,事实证明message_id不知何故保持不变,无论是否新消息是否发送(只是昵称玩家),但这只是我的猜测,告诉我如何解决这个问题
我似乎对 TelegramBotAPI 库有所了解,我决定研究 aiogram,但立即遇到了问题,我不知道如何在用户回答机器人的问题后立即捕获用户的响应。那么,在 telebot 中,该函数负责此操作。
bot.register_next_step_handler()
aiogram 中是否有具有相同操作的函数?我只是到处都找不到它。