socializer/src/presenters/longpollthread.py

29 lines
987 B
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import threading
from vk_api.longpoll import VkLongPoll, VkEventType
from pubsub import pub
2018-12-18 05:32:41 -06:00
from requests.exceptions import ReadTimeout, ConnectionError
from logging import getLogger
log = getLogger("controller.longpolThread")
class worker(threading.Thread):
def __init__(self, session):
super(worker, self).__init__()
log.debug("Instantiating longPoll server")
self.session = session
self.longpoll = VkLongPoll(self.session.vk.session_object)
def run(self):
try:
for event in self.longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
pub.sendMessage("order-sent-message", obj=event)
elif event.type == VkEventType.USER_ONLINE:
pub.sendMessage("user-online", event=event)
elif event.type == VkEventType.USER_OFFLINE:
pub.sendMessage("user-offline", event=event)
elif event.type == VkEventType.USER_TYPING:
pub.sendMessage("user-typing", obj=event)
except:
pub.sendMessage("longpoll-read-timeout")