Esempio n. 1
0
    def __init__(self,
                 last_seen=time.time(),
                 next_action=action.NOTHING,
                 chat_id=None):

        self.last_seen = last_seen
        self.next_action = next_action
        self.chat_id = chat_id
        self.vk_user = Vk_user()
        self.vk_token = None
        self.next_server = None
        self.interacted_with = set()  # set of chats or users
        self.next_recepient = None
Esempio n. 2
0
    def receive_vk_message(self, update, client):
        flags = update[2]
        from_id = update[3]
        text = update[6]
        attachments = update[7]

        if flags & 2 == 2:
            # Skip when message is outgoing
            return

        from_name = ''

        if from_id & 2000000000 == 2000000000:
            # Message came from chat
            chat_id = from_id - 2000000000
            chat = Vk_chat.fetch(client.vk_token, chat_id)
            from_name = chat.name_from(attachments['from'])
            client.add_interaction_with(chat)
        else:
            user = Vk_user.fetch_user(client.vk_token, from_id)
            from_name = user.get_name()
            client.add_interaction_with(user)

        self.updater.bot.sendMessage(chat_id=client.chat_id,
                                     text=message.NEW_MESSAGE(from_name, text),
                                     reply_markup=Bot.keyboard(
                                         client.keyboard_markup()),
                                     parse_mode=ParseMode.MARKDOWN)
        client.persist()
Esempio n. 3
0
    def receive_vk_message(self, update, client):
        flags = update[2]
        from_id = update[3]
        text = update[6]
        attachments = update[7]

        if flags & 2 == 2:
            # Skip when message is outgoing
            return

        from_name = ''

        if from_id & 2000000000 == 2000000000:
            # Message came from chat
            chat_id = from_id - 2000000000
            chat = Vk_chat.fetch(client.vk_token, chat_id)
            from_name = chat.name_from(attachments['from'])
            client.add_interaction_with(chat)
        else:
            user = Vk_user.fetch_user(client.vk_token, from_id)
            from_name = user.get_name()
            client.add_interaction_with(user)

        self.updater.bot.sendMessage(chat_id=client.chat_id,
                text=message.NEW_MESSAGE(from_name, text),
                reply_markup=Bot.keyboard(client.keyboard_markup()),
                parse_mode=ParseMode.MARKDOWN)
        client.persist()
Esempio n. 4
0
    def __init__(self,
                 last_seen=time.time(),
                 next_action=action.NOTHING,
                 chat_id=None):

        self.last_seen = last_seen
        self.next_action = next_action
        self.chat_id = chat_id
        self.vk_user = Vk_user()
        self.vk_token = None
        self.next_server = None
        self.interacted_with = set() # set of chats or users
        self.next_recepient = None
Esempio n. 5
0
 def load_vk_user(self, vk_token):
     self.vk_token = vk_token
     if self.vk_user.should_fetch():
         user = Vk_user.fetch_current_user(vk_token)
         if user != None:
             self.vk_user = user
Esempio n. 6
0
class Client:
    def __init__(self,
                 last_seen=time.time(),
                 next_action=action.NOTHING,
                 chat_id=None):

        self.last_seen = last_seen
        self.next_action = next_action
        self.chat_id = chat_id
        self.vk_user = Vk_user()
        self.vk_token = None
        self.next_server = None
        self.interacted_with = set()  # set of chats or users
        self.next_recepient = None

    def db_key(self):
        return 'Client-' + str(self.chat_id)

    def persist(self):
        self.seen_now()
        db.set(self.db_key(), self.to_json())
        db.sync()

    def seen_now(self):
        self.last_seen = time.time()

    def load_vk_user(self, vk_token):
        self.vk_token = vk_token
        if self.vk_user.should_fetch():
            user = Vk_user.fetch_current_user(vk_token)
            if user != None:
                self.vk_user = user

    def keyboard_markup(self):
        if self.next_action == action.MESSAGE:
            return self.picked_keyboard()

        return [['/pick ' + user.get_name()] for user in self.interacted_with
                if not user.should_fetch()]

    def picked_keyboard(self):
        return [['/unpick ' + self.next_recepient.get_name()], ['/details']]

    def expect_message_to(self, to_name):
        self.next_recepient = next(
            (u for u in self.interacted_with if u.get_name() == to_name), None)
        if self.next_recepient != None:
            self.next_action = action.MESSAGE
        self.persist()

    def send_message(self, text):
        if self.next_recepient == None:
            return

        self.next_recepient.send_message(self.vk_token, text)

    def add_interaction_with(self, user):
        if user.empty():
            return

        self.interacted_with.add(user)

    def to_json(self):
        return jsonpickle.encode(self)

    @staticmethod
    def from_json(json_str):
        return jsonpickle.decode(json_str)

    @staticmethod
    def all_from_db():
        clients = dict()
        for client_key in db.dict():
            if not re.match('Client-.+', client_key):
                continue

            client_json = db.get(client_key)
            client = Client.from_json(client_json)
            if client.vk_user.should_fetch():
                client.load_vk_user(client.vk_token)

            clients[client.chat_id] = client

        print('Restored clients from db: ' + str(clients))
        return clients
Esempio n. 7
0
 def load_vk_user(self, vk_token):
     self.vk_token = vk_token
     if self.vk_user.should_fetch():
         user = Vk_user.fetch_current_user(vk_token)
         if user != None:
             self.vk_user = user
Esempio n. 8
0
class Client:
    def __init__(self,
                 last_seen=time.time(),
                 next_action=action.NOTHING,
                 chat_id=None):

        self.last_seen = last_seen
        self.next_action = next_action
        self.chat_id = chat_id
        self.vk_user = Vk_user()
        self.vk_token = None
        self.next_server = None
        self.interacted_with = set() # set of chats or users
        self.next_recepient = None

    def db_key(self):
        return 'Client-' + str(self.chat_id)

    def persist(self):
        self.seen_now()
        db.set(self.db_key(), self.to_json())
        db.sync()

    def seen_now(self):
        self.last_seen = time.time()

    def load_vk_user(self, vk_token):
        self.vk_token = vk_token
        if self.vk_user.should_fetch():
            user = Vk_user.fetch_current_user(vk_token)
            if user != None:
                self.vk_user = user

    def keyboard_markup(self):
        if self.next_action == action.MESSAGE:
            return self.picked_keyboard()

        return [['/pick ' + user.get_name()]
                for user in self.interacted_with
                if not user.should_fetch()]

    def picked_keyboard(self):
        return [['/unpick ' + self.next_recepient.get_name()],
                ['/details']]

    def expect_message_to(self, to_name):
        self.next_recepient = next((u for u in self.interacted_with
                                    if u.get_name() == to_name), None)
        if self.next_recepient != None:
            self.next_action = action.MESSAGE
        self.persist()

    def send_message(self, text):
        if self.next_recepient == None:
            return

        self.next_recepient.send_message(self.vk_token, text)

    def add_interaction_with(self, user):
        if user.empty():
            return

        self.interacted_with.add(user)

    def to_json(self):
        return jsonpickle.encode(self)

    @staticmethod
    def from_json(json_str):
        return jsonpickle.decode(json_str)

    @staticmethod
    def all_from_db():
        clients = dict()
        for client_key in db.dict():
            if not re.match('Client-.+', client_key):
                continue

            client_json = db.get(client_key)
            client = Client.from_json(client_json)
            if client.vk_user.should_fetch():
                client.load_vk_user(client.vk_token)

            clients[client.chat_id] = client

        print('Restored clients from db: ' + str(clients))
        return clients