Ejemplo n.º 1
0
    def test_wait_error_code_1(self):
        TestDriver.message = {'failed': 1, 'ts': 42}
        session = API(TestSession())
        lp = LongPoll(session, mode=0)

        response = yield from lp.wait()
        self.assertDictEqual(response, {'ts': -TestSession.TS})
Ejemplo n.º 2
0
 def test_get_pts_need_ts(self):
     session = API(TestSession())
     lp = LongPoll(session, mode=0)
     result = yield from lp.get_pts(need_ts=True)
     self.assertEqual(len(result), 2)
     self.assertEqual(result[0], TestSession.PTS)
     self.assertEqual(result[1], TestSession.TS)
Ejemplo n.º 3
0
    def test_wait_valid(self):
        TestDriver.message = {'ts': -TestSession.TS}
        session = API(TestSession())
        lp = LongPoll(session, mode=0)

        response = yield from lp.wait()
        self.assertDictEqual(response, TestDriver.message)
Ejemplo n.º 4
0
 def test_get_pts_cached_value(self):
     session = API(TestSession())
     lp = LongPoll(session, mode=0)
     yield from lp.get_pts()
     pts = yield from lp.get_pts()
     self.assertIsInstance(pts, type(TestSession.PTS))
     self.assertEqual(pts, TestSession.PTS)
Ejemplo n.º 5
0
    def test_wait_error_code_4(self):
        TestDriver.message = {'failed': 4}
        session = API(TestSession())
        lp = LongPoll(session, mode=0)

        with self.assertRaises(VkLongPollError):
            yield from lp.wait()
Ejemplo n.º 6
0
    def test_wait_valid_with_session_auto_auth(self):
        s = TestAuthSession(login=USER_LOGIN, password=USER_PASSWORD, app_id=APP_ID, scope='messages')

        api = API(s)
        lp = LongPoll(api, mode=2, wait=2)

        response = yield from lp.wait()
        s.close()
        self.assertTrue('ts' in response)
        self.assertTrue('updates' in response)
Ejemplo n.º 7
0
 def wait_user_input(self):
     # listen long poll user chat session
     longpoll = LongPoll(self.vk, mode=2)
     while True:
         result = yield from longpoll.wait()
         for update in result.get('updates', []):
             user_id, message = yield from self.parse_message_updates(*update)
             if user_id and message:
                 answer = yield from self.get_answer(user_id, message)
                 print('answer: %s' % answer)
                 events = yield from self.send_answer(user_id, answer)
         print('result: %s' % result)
Ejemplo n.º 8
0
    async def init_vk(self):
        vk_session = None
        if self.config['IMPLICIT']:
            vk_session = ImplicitSession(self.config['USER_LOGIN'], self.config['USER_PASSWORD'],
                                         self.config['APP_ID'], ['messages', 'wall'])
        else:
            # TODO(spark): implement TokenSession
            pass

        self.logger.info('Auth in VK...')
        await vk_session.authorize()

        self.vk_api = vk_api = API(vk_session)
        vk_lp = LongPoll(vk_api, mode=2)

        while self.running:
            # Main working loop
            response = await vk_lp.wait()

            for action in response['updates']:
                if action[0] is 4:
                    message_id = action[1]
                    sender = action[3]
                    sender_id = sender
                    message = str(action[6])
                    attachment = action[7]
                    self.logger.debug('Got message: {}'.format(message))

                    if sender > 2000000000:
                        # Groupchat
                        sender_id = int(attachment['from'])

                    f_flag = False
                    for f in self.filters:
                        f_res = await f(sender, sender_id, message, attachment)
                        if f_res is False:
                            f_flag = True
                            continue

                    if f_flag:
                        continue

                    if message.startswith(self.config['COMMAND_SYMBOL']) and message[1] is not ' ':
                        message = message[1:]
                        flag = False
                        for c in self.commands:
                            if message.startswith(c) and not flag:
                                flag = True
                                command = message.split(' ')[0]
                                if command in self.admin_commands and sender_id not in self.config['ADMINS']:
                                    await self.send_message(sender, 'Access denied')
                                else:
                                    await self.commands[command](sender, sender_id, message, attachment)
                        if flag is False:
                            await self.send_message(sender, 'Command not found')

        vk_session.close()
Ejemplo n.º 9
0
    async def test_wait_valid_with_session_auto_auth(self):
        s = TestInternalAuthSession(login='******',
                                    password='******',
                                    app_id='123',
                                    scope='messages')
        s.BASE_URL = self.base_url

        api = API(s)
        lp = LongPoll(api, mode=2, wait=2)

        response = await lp.wait()
        await s.close()
        self.assertTrue('ts' in response)
        self.assertTrue('updates' in response)
Ejemplo n.º 10
0
    async def test_wait_valid_with_token_session(self):
        s = TestInternalAuthSession(login='******',
                                    password='******',
                                    app_id='123',
                                    scope='messages')
        s.BASE_URL = self.base_url
        await s.authorize()
        await s.close()

        t = TestTokenSession(s.access_token, timeout=1000)
        t.BASE_URL = self.base_url
        api = API(t)
        lp = LongPoll(api, mode=2, wait=2)

        response = await lp.wait()
        await t.close()
        self.assertTrue('ts' in response)
        self.assertTrue('updates' in response)
Ejemplo n.º 11
0
 def test_init_with_api(self):
     session = API(TestSession())
     lp = LongPoll(session, mode=0)
     yield from lp._get_long_poll_server()