def test_bad_messages(self): result = list(Message.prepare_message("a\n" * (MAX_LENGHT // 2))) for r in result: self.check(r) self.assertEqual(len(result), 1) result = Message.prepare_message("a" * (MAX_LENGHT * 3)) for r in result: self.check(r) result = list(Message.prepare_message("a " * int(MAX_LENGHT * 2.9))) for r in result: self.check(r) self.assertEqual(len(result), 6) result = list(Message.prepare_message("a" * MAX_LENGHT + " a")) for r in result: self.check(r) self.assertEqual(len(result), 2) self.assertEqual(len(result[-1]), 1) result = list(Message.prepare_message("a" * MAX_LENGHT + " aaaa")) for r in result: self.check(r) self.assertEqual(len(result), 2) self.assertEqual(len(result[-1]), 4)
def test_long_messages(self): result = Message.prepare_message("a" * MAX_LENGHT) for r in result: self.check(r) result = Message.prepare_message(("a" * (MAX_LENGHT - 1) + "\n") * 2) for r in result: self.check(r) result = Message.prepare_message(("a" * MAX_LENGHT + "\n") * 2) for r in result: self.check(r)
async def process_longpoll_event(self, new_event): if not new_event: return event_id = new_event[0] if event_id != 4: evnt = LongpollEvent(self.api, event_id, new_event) return await self.process_event(evnt) data = MessageEventData() data.msg_id = new_event[1] data.attaches = new_event[6] data.time = int(new_event[4]) try: data.user_id = int(data.attaches['from']) data.chat_id = int(new_event[3]) - 2000000000 data.is_multichat = True del data.attaches['from'] except KeyError: data.user_id = int(new_event[3]) data.is_multichat = False # https://vk.com/dev/using_longpoll_2 flags = parse_msg_flags(new_event[2]) if flags['outbox']: if not self.settings.READ_OUT: return data.is_out = True data.full_text = new_event[5].replace('<br>', '\n') if "fwd" in data.attaches: data.forwarded = MessageEventData.parse_brief_forwarded_messages_from_lp( data.attaches["fwd"]) del data.attaches["fwd"] else: data.forwarded = [] msg = Message(self.api, data) if await self.check_event(data.user_id, data.chat_id, data.attaches): msg.is_event = True await self.process_message(msg)
async def callback_processor(self, request): try: data = await request.json() except (UnicodeDecodeError, json.decoder.JSONDecodeError): return web.Response(text="ok") data_type = data["type"] if data_type == "confirmation": return web.Response(text=self.settings.CONF_CODE) obj = data["object"] if "user_id" in obj: obj['user_id'] = int(obj['user_id']) if data_type == 'message_new': data = MessageEventData.from_message_body(obj) msg = Message(self.api, data) await self.process_message(msg) else: evnt = CallbackEvent(self.api, data_type, obj) await self.process_event(evnt) return web.Response(text="ok")
def test_simple_message(self): result = Message.prepare_message("hi") size = 0 for r in result: self.check(r) size += 1 self.assertEqual(size, 1)