import aiohttp import asyncio from aiogram import Bot , Dispatcher , executor , types async def get_new_posts ( access_token , group_id , last_post_id = None ) : api_version = '5.131' base_url = f'https://api.vk.com/method/wall.get?access_token={access_token}&v={api_version}' params = { 'owner_id' : f'-{group_id}' , 'count' : 1 } if last_post_id : params [ 'offset' ] = 1 params [ 'last_post_id' ] = last_post_id async with aiohttp . ClientSession ( ) as session : async with session . get ( base_url , params = params ) as response : data = await response . json ( ) if 'error' in data : print ( f"Error:{data['error']['error_msg']}" ) return None posts = data [ 'response' ] [ 'items' ] return posts async def send_post_to_telegram ( bot_token , chat_id , post ) : bot = Bot ( token = bot_token ) text = post [ 'text' ] attachments = post . get ( 'attachments' , [ ] ) message = f"New post from VK:\n\n{text}" for attachment in attachments : if attachment [ 'type' ] == 'photo' : photo = attachment [ 'photo' ] photo_url = sorted ( photo [ 'sizes' ] , key = lambda x : x [ 'width' ] ) [ - 1 ] [ 'url' ] await bot . send_photo ( chat_id = chat_id , photo = photo_url , caption = message ) elif attachment [ 'type' ] == 'video' : video = attachment [ 'video' ] video_url = video [ 'player' ] await bot . send_video ( chat_id = chat_id , video = video_url , caption = message ) async def main ( ) : access_token = '' group_id = '' bot_token = '' chat_id = '@' last_post_id = None while True : new_posts = await get_new_posts ( access_token , group_id , last_post_id ) if new_posts : for post in new_posts : if last_post_id is None or post [ 'id' ] > last_post_id : await send_post_to_telegram ( bot_token , chat_id , post ) last_post_id = post [ 'id' ] print ( last_post_id ) await asyncio . sleep ( 60 ) if __name__ == '__main__' : loop = asyncio . get_event_loop ( ) loop . run_until_complete ( main ( ) )