Exemple #1
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # message
     message = content.get('message')
     print('online users response: %s' % message)
     # users
     users = content.get('users')
     if users is not None:
         print('      users:', json.dumps(users))
     return None
Exemple #2
0
    def send(self, content: Content) -> bool:
        """
        Send message content to this group
        (only existed member can do this)

        :param content: message content
        :return: True on success
        """
        facebook = self.facebook
        # check group ID
        gid = content.group
        if gid is None:
            content.group = self.group
        # check members
        members = facebook.members(self.group)
        if members is None or len(members) == 0:
            # get group assistants
            assistants = facebook.assistants(self.group)
            if assistants is None or len(assistants) == 0:
                raise LookupError('failed to get assistant for group: %s' % self.group)
            # querying assistants for group info
            self.messenger.query_group(group=self.group, users=assistants)
            return False
        # let group assistant to split and deliver this message to all members
        return self.messenger.send_content(sender=None, receiver=self.group, content=content)
Exemple #3
0
 def __ignored(self, content: Content, sender: ID, msg: ReliableMessage) -> bool:
     # check robot
     if sender.type in [NetworkType.ROBOT, NetworkType.STATION]:
         Log.info('Dialog > ignore message from another robot: %s' % sender)
         return True
     # check time
     now = int(time.time())
     dt = now - msg.time
     if dt > 600:
         # Old message, ignore it
         return True
     # check group message
     if content.group is None:
         # not a group message
         return False
     text = content.text
     if text is None:
         raise ValueError('text content error: %s' % content)
     # checking '@nickname'
     receiver = msg.receiver
     at = '@%s' % self.facebook.name(identifier=receiver)
     if text.find(at) < 0:
         Log.info('ignore group message that not querying me: %s' % text)
         return True
     # TODO: remove all '@nickname'
     text = text.replace(at, '')
     content.text = text
Exemple #4
0
 def process(self, content: Content, sender: ID, msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # submit device token for APNs
     token = content.get('device_token')
     if token is not None:
         self.database.save_device_token(token=token, identifier=sender)
         return ReceiptCommand.new(message='Token received')
Exemple #5
0
 def __ignored(self, content: Content, sender: ID,
               msg: InstantMessage) -> bool:
     # check robot
     if sender.type.is_robot() or sender.type.is_station():
         self.info('Dialog > ignore message from another robot: %s' %
                   msg.content)
         return True
     # check time
     now = int(time.time())
     dt = now - msg.envelope.time
     if dt > 600:
         self.info('Old message, ignore it: %s' % msg)
         return True
     # check group message
     if content.group is None:
         # not a group message
         return False
     text = content.text
     if text is None:
         raise ValueError('text content error: %s' % content)
     # checking '@nickname'
     receiver = self.facebook.identifier(msg.envelope.receiver)
     at = '@%s' % self.facebook.nickname(identifier=receiver)
     self.info('Group Dialog > searching "%s" in "%s"...' % (at, text))
     if text.find(at) < 0:
         self.info('ignore group message that not querying me: %s' % text)
         return True
     # TODO: remove all '@nickname'
     text = text.replace(at, '')
     content.text = text
Exemple #6
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # message
     message = content.get('message')
     if message is None:
         self.info('get online user(s) for %s' % sender)
         return self.__random_users()
     else:
         return self.__update(content=content)
Exemple #7
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # message
     message = content.get('message')
     if message is None:
         self.info('search users for %s, %s' % (sender, content))
         keywords = content['keywords'].split(' ')
         return self.__search(keywords=keywords)
     else:
         return self.__update(content=content)
Exemple #8
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # keywords
     keywords = content.get('keywords')
     if keywords is None:
         return TextContent.new(text='Search command error')
     keywords = keywords.split(' ')
     # search in database
     results = self.database.search(keywords=keywords)
     users = list(results.keys())
     return SearchCommand.new(users=users, results=results)
Exemple #9
0
 def process(self, content: Content, sender: ID, msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # report title
     title = content.get('title')
     if 'report' == title:
         return self.__process_old_report(cmd=content, sender=sender)
     # get CPU by report title
     cpu = self.cpu(command=title)
     # check and run
     if cpu is None:
         return TextContent.new(text='Report command (title: %s) not support yet!' % title)
     assert cpu is not self, 'Dead cycle! report cmd: %s' % content
     return cpu.process(content=content, sender=sender, msg=msg)
Exemple #10
0
 def message(self, signature: str) -> Optional[dict]:
     msg = self.msg_table.load(signature=signature)
     if msg is None:
         Log.error('failed to load message: %s' % signature)
         return None
     # message content
     content = msg.get('content')
     if content is None:
         data = msg['data']
         content = Content.parse(content=json.loads(data))
         assert isinstance(content, TextContent), 'content error: %s' % data
         msg['content'] = content.dictionary
     # message url
     link = msg.get('link')
     if link is None:
         link = msg_url(signature=signature)
         msg['link'] = link
     return msg.dictionary
Exemple #11
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     self.info('client broadcast %s, %s' % (sender, content))
     # report title
     title = content.get('title')
     if 'report' == title:
         return self.__process_old_report(cmd=content, sender=sender)
     # get CPU by report title
     cpu = self.cpu(command=title)
     # check and run
     if cpu is None:
         self.error('command not supported yet: %s' % content)
     elif cpu is self:
         # FIXME: dead cycle!
         self.error('Dead cycle! command: %s' % content)
         raise AssertionError('Dead cycle!')
     else:
         # process by subclass
         return cpu.process(content=content, sender=sender, msg=msg)
Exemple #12
0
 def broadcast_content(self, content: Content, receiver: ID) -> bool:
     sender = self.facebook.current_user.identifier
     content.group = EVERYONE
     return self.messenger.send_content(sender=sender,
                                        receiver=receiver,
                                        content=content)
Exemple #13
0
 def broadcast_content(self, content: Content, receiver: ID) -> bool:
     content.group = EVERYONE
     return self.messenger.send_content(content=content, receiver=receiver)
Exemple #14
0
 def __update(self, content: Content) -> Optional[Content]:
     self.info('online users: %s' % content.get('message'))
     if 'users' in content:
         users = content['users']
         print('      users:', json.dumps(users))
     return None