Здравствуйте, я создаю бота и в качестве инструмента использую
aiogram.
Вот его исходный код:
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
from bs4 import BeautifulSoup
import requests
from telegraph import Telegraph
import aiogram
# from Engine import Parser
TOKEN = '5168757710:AAEMKuCMCsi4ww7Yw8ye-GX44VM4aqET2l8'
Channel_id = '@bebrochka_bts'
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
btn1 = KeyboardButton('/Github')
btn2 = KeyboardButton('/Telegram')
mainMenu = ReplyKeyboardMarkup(resize_keyboard=True).add(btn1, btn2)
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
await bot.send_message(message.from_user.id, 'Привет ;)', reply_markup=mainMenu)
@dp.message_handler(commands=['habr_parse'], content_types=['text'])
async def habr_parse(message: types.Message):
URL = message.get_args()
page = requests.get(URL)
with open('E:/nohomolawwbaby.p/Python/JetArticles/Article/page.html', 'w', encoding='utf-8') as file:
file.write(page.text)
with open('E:/nohomolawwbaby.p/Python/JetArticles/Article/page.html', encoding='utf-8') as file:
src = file.read()
soup = BeautifulSoup(src, 'lxml')
post = soup.find("article", class_="tm-article-presenter__content tm-article-presenter__content_narrow")
title = post.find("h1", class_="tm-article-snippet__title tm-article-snippet__title_h1").text
article = open('E:/nohomolawwbaby.p/Python/JetArticles/Article/article.txt', 'w', encoding='utf-8')
text = post.find_all(['p', 'h4'])
for i in text:
article.write(f'{i}\n\n')
article.close()
telegraph = Telegraph()
telegraph.create_account(short_name='1337')
with open('E:/nohomolawwbaby.p/Python/JetArticles/Article/article.txt', encoding='utf-8') as f:
response = telegraph.create_page(
title,
html_content=f.read()
)
url = 'http://telegra.ph/{}'.format(response['path'])
msg = f'{title}\n\n{url}'
await bot.send_message(message.from_user.id, msg, parse_mode='HTML')
@dp.message_handler(commands=['Github'], content_types=['text'])
async def githublnk(message: types.Message):
await bot.send_message(message.from_user.id, 'www.example.com')
@dp.message_handler(commands=['Telegram'], content_types=['text'])
async def telegramlnk(message: types.Message):
await bot.send_message(message.from_user.id, 'www.example.com')
if __name__=='__main__':
executor.start_polling(dp, skip_updates=True)
Он парсит статью на habr.com и далее пересылает её в telegraph
Но при вводе команды
habr_parse бот выдаёт такую ошибку:
Updates were skipped successfully.
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<Dispatcher._process_polling_updates() done, defined at E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py:407> exception=TypeError("object str can't be used in 'await' expression")>
Traceback (most recent call last):
File "E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
File "E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 256, in process_update
return await self.message_handlers.notify(update.message)
File "E:\nohomolawwbaby.p\Python\JetArticles\venv\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "E:\nohomolawwbaby.p\Python\JetArticles\bot.py", line 33, in habr_parse
URL = await message.get_args()
TypeError: object str can't be used in 'await' expression
Что мне делать???