class TokenSession: API_VERSION = '5.52' REQUEST_URL = 'https://api.vk.com/method/' def __init__(self, access_token=None, timeout=10, driver=None): self.timeout = timeout self.access_token = access_token self.driver = HttpDriver(timeout) if driver is None else driver def close(self): self.driver.close() async def make_request(self, method_request, timeout=None): params = method_request._method_args return await self.send_api_request(method_request._method_name, params, timeout) async def send_api_request(self, method_name, params=None, timeout=None): if not timeout: timeout = self.timeout if not params: params = {} params['v'] = self.API_VERSION if self.access_token: params['access_token'] = self.access_token response = await self.driver.json(self.REQUEST_URL + method_name, params, timeout) error = response.get('error') if error: err_code = error.get('error_code') if err_code == CAPTCHA_IS_NEEDED: captcha_sid = error.get('captcha_sid') captcha_url = error.get('captcha_img') params['captcha_key'] = await self.enter_captcha(captcha_url, captcha_sid) params['captcha_sid'] = captcha_sid return await self.send_api_request(method_name, params, timeout) elif err_code == AUTHORIZATION_FAILED: await self.authorize() return await self.send_api_request(method_name, params, timeout) else: raise VkAPIError(error, self.REQUEST_URL + method_name) return response['response'] async def authorize(self): raise VkAuthError('invalid_token', 'User authorization failed') async def enter_captcha(self, url, sid): """ Override this method for processing captcha. :return captcha value """ raise VkCaptchaNeeded(url, sid)
def __init__(self, app_id, app_secret, redirect_uri, code, timeout=10, driver=None): super().__init__(access_token=None, timeout=timeout) self.code = code self.app_id = app_id self.app_secret = app_secret self.redirect_uri = redirect_uri self.driver = HttpDriver(timeout) if driver is None else driver
def vk(self): if not hasattr(self, 'vk_api'): loop = asyncio.get_event_loop() connector = aiohttp.TCPConnector(verify_ssl=False) session = aiohttp.ClientSession(connector=connector) driver = HttpDriver(loop=loop, session=session) self.session = TokenSession(access_token=self.credentials.token, driver=driver) self.vk_api = API(self.session) return self.vk_api
def __init__(self, access_token: str = None, timeout: int = 10, driver=None): """ :param access_token: see `User Token` block from `https://vk.com/dev/access_token` :param timeout: default time out for any request in current session :param driver: TODO add description """ self.timeout = timeout self.access_token = access_token self.driver = HttpDriver(timeout) if driver is None else driver
async def func(): connector = TCPConnector(ssl=False) session = ClientSession(connector=connector) driver = HttpDriver(loop=loop, session=session) async with TokenSession( access_token= 'e0b61b2781bb2e3ec1b262fe35af1fcca3c27d6d12806b12ba1bc65665c62cd93311f2b8cbb2f7cc1aebf', driver=driver) as session: lp = BotsLongPoll(session, group_id=156616044, mode=2) api = API(session) print(api) while True: data = await lp.wait() updates = data["updates"] print(updates) # TODO: add async for for i in updates: if i['type'] == 'message_new': try: search_string = i['object']['payload'] search_command = re.search('(\"command\":\")(.*)(\")', search_string) command = search_command.group(2) step = await Step.filter(command=command).first() await api.messages.send(message=step.message, peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard) except ContentTypeError as e: search_string = i['object']['payload'] search_command = re.search('(\"command\":\")(.*)(\")', search_string) command = search_command.group(2) step = await Step.filter(command=command).first() pattern = re.compile(r'([А-Я].{,800}[\.])', re.S) chunked_message = pattern.findall(step.message) for chunk in chunked_message[:-1]: await api.messages.send( message=chunk, peer_id=i['object']['from_id'], random_id=0) await api.messages.send( message=chunked_message[-1:][0], peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard) except Exception: step = await Step.filter(command='start').first() await api.messages.send(message='Что-то пошло не так', peer_id=i['object']['from_id'], random_id=0, keyboard=step.keyboard)
def get_driver(self): return HttpDriver()
def __init__(self, access_token=None, timeout=10, driver=None): self.timeout = timeout self.access_token = access_token self.driver = HttpDriver(timeout) if driver is None else driver