# Path: vkapi/incoming_message.py
# class IncomingMessage:
# 
#     def __init__(self, data, method=''):
#         self.id = data.get('id')
#         self.date = data['date']
#         self.body = data.get('text', '')
#         self.user_id = data['from_id']
#         if 'peer_id' in data:
#             self.chat_id = data['peer_id'] - CONF_START if data['peer_id'] > CONF_START else None
#         else:
#             self.chat_id = None
#         self.action = data.get('action')
#         self.attachments = data.get('attachments', [])
#         self._fwd_messages_raw = data.get('fwd_messages', [])
#         if 'reply_message' in data:
#             self._fwd_messages_raw.append(data['reply_message'])
#         self.method = method
# 
#         self.is_sticker = False
#         self.is_voice = False
#         for att in self.attachments:
#             if att['type'] == 'sticker':
#                 self.is_sticker = True
#             if att['type'] == 'doc' and att['doc']['type'] == doc_types.AUDIO:
#                 self.is_voice = True
# 
#     def _construct_forwarded_message(self, data):
#         return self.__class__(data)
# 
#     @property
#     def peer_id(self):
#         if self.chat_id is not None:
#             return CONF_START + self.chat_id
#         return self.user_id
# 
#     @property
#     def is_chat(self):
#         return self.chat_id is not None
# 
#     @cached_property
#     def fwd_messages(self):
#         fwd_messages = [self._construct_forwarded_message(data) for data in self._fwd_messages_raw]
#         del self._fwd_messages_raw
#         return fwd_messages
# 
# Path: vkapi/utils.py
# CONF_START = 2000000000
# 
# class LongpollMessage:
#     def __init__(self, record):
#         self.mid, self.flags, self.sender, self.ts, self.text, self.opt, self.extra = record


import html
import logging
import queue
import threading
import time
from .incoming_message import IncomingMessage
from .utils import CONF_START, LongpollMessage
        self.longpoll_queue = queue.Queue()
        self.longpoll_thread = threading.Thread(target=self.monitor, daemon=True)
        self.longpoll_thread.start()
        self.longpoll_callback = None
        self.last_message_id = 0
        self.last_get_dialogs = 0
        self.longpolled_messages = set()
        self.terminate_monitor = False
        self.message_class = message_class


    def monitor(self):
        while True:
            try:
                for i in self._getLongpoll():
                    self.longpoll_queue.put(i)
            except Exception:
                logger.exception('MessageReceiver error')
                time.sleep(5)

    def getMessages(self, get_dialogs=False):
        ctime = time.time()
        if not self.last_get_dialogs:
            self.last_get_dialogs = ctime - self.get_dialogs_interval + 1
        if (self.get_dialogs_interval >=0 and ctime - self.last_get_dialogs > self.get_dialogs_interval) or get_dialogs:
            self.last_get_dialogs = ctime
            res = []
            messages = self.api.messages.getConversations(count=200, filter='unread')
            try:
                messages = messages['items'][::-1]

# Path: vkapi/incoming_message.py
# class IncomingMessage:
# 
#     def __init__(self, data, method=''):
#         self.id = data.get('id')
#         self.date = data['date']
#         self.body = data.get('text', '')
#         self.user_id = data['from_id']
#         if 'peer_id' in data:
#             self.chat_id = data['peer_id'] - CONF_START if data['peer_id'] > CONF_START else None
#         else:
#             self.chat_id = None
#         self.action = data.get('action')
#         self.attachments = data.get('attachments', [])
#         self._fwd_messages_raw = data.get('fwd_messages', [])
#         if 'reply_message' in data:
#             self._fwd_messages_raw.append(data['reply_message'])
#         self.method = method
# 
#         self.is_sticker = False
#         self.is_voice = False
#         for att in self.attachments:
#             if att['type'] == 'sticker':
#                 self.is_sticker = True
#             if att['type'] == 'doc' and att['doc']['type'] == doc_types.AUDIO:
#                 self.is_voice = True
# 
#     def _construct_forwarded_message(self, data):
#         return self.__class__(data)
# 
#     @property
#     def peer_id(self):
#         if self.chat_id is not None:
#             return CONF_START + self.chat_id
#         return self.user_id
# 
#     @property
#     def is_chat(self):
#         return self.chat_id is not None
# 
#     @cached_property
#     def fwd_messages(self):
#         fwd_messages = [self._construct_forwarded_message(data) for data in self._fwd_messages_raw]
#         del self._fwd_messages_raw
#         return fwd_messages
# 
# Path: vkapi/utils.py
# CONF_START = 2000000000
# 
# class LongpollMessage:
#     def __init__(self, record):
#         self.mid, self.flags, self.sender, self.ts, self.text, self.opt, self.extra = record


# Path: vkapi/message_receiver.py
import html
import logging
import queue
import threading
import time
from .incoming_message import IncomingMessage
from .utils import CONF_START, LongpollMessage

        self.longpoll_queue = queue.Queue()
        self.longpoll_thread = threading.Thread(target=self.monitor, daemon=True)
        self.longpoll_thread.start()
        self.longpoll_callback = None
        self.last_message_id = 0
        self.last_get_dialogs = 0
        self.longpolled_messages = set()
        self.terminate_monitor = False
        self.message_class = message_class


    def monitor(self):
        while True:
            try:
                for i in self._getLongpoll():
                    self.longpoll_queue.put(i)
            except Exception:
                logger.exception('MessageReceiver error')
                time.sleep(5)

    def getMessages(self, get_dialogs=False):
        ctime = time.time()
        if not self.last_get_dialogs:
            self.last_get_dialogs = ctime - self.get_dialogs_interval + 1
        if (self.get_dialogs_interval >=0 and ctime - self.last_get_dialogs > self.get_dialogs_interval) or get_dialogs:
            self.last_get_dialogs = ctime
            res = []
            messages = self.api.messages.getConversations(count=200, filter='unread')
            try:
                messages = messages['items'][::-1]

            except TypeError: