提问人:Precious TK 提问时间:11/12/2023 最后编辑:mkrieger1Precious TK 更新时间:11/12/2023 访问量:166
from aiogram.dispatcher import Dispatcher ImportError: cannot import name 'Dispatcher' from 'aiogram.dispatcher'
from aiogram.dispatcher import Dispatcher ImportError: cannot import name 'Dispatcher' from 'aiogram.dispatcher'
问:
我正在制作这个电报脚本,以便能够将一些电报用户名输入我的电报帐户。这是我的代码如下:
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
API_TOKEN = '675*********z9Q'
GROUP_CHAT_ID = -100123456789 # Replace with your group chat ID
USERNAMES_TO_ADD = ['Username1', 'Username2', 'Username3']
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
async def add_members_to_group(usernames):
chat_id = GROUP_CHAT_ID
added_usernames = []
for username in usernames:
try:
user = await bot.get_chat_member(chat_id, username)
await bot.promote_chat_member(chat_id, user.user.id)
added_usernames.append(username)
except Exception as e:
print(f"Error adding {username}: {e}")
return added_usernames
@dp.message_handler(commands=['addmembers'])
async def handle_add_members(message: types.Message):
added_usernames = await add_members_to_group(USERNAMES_TO_ADD)
if added_usernames:
response_message = f'Members added successfully: {", ".join(added_usernames)}'
else:
response_message = 'No members added.'
await message.reply(response_message)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
如何更改代码以便能够将这些成员添加到我的组中?
还是有其他选择?
答:
0赞
veb_abhi
11/12/2023
#1
使用代替 .from aiogram import Dispatcher
from aiogram.dispatcher import Dispatcher
也被删除,所以它不起作用。executor
评论