コード例 #1
0
ファイル: sample.py プロジェクト: odeblic/hypernet
    def _on_schedule(self):
        """ This method is called repeatedly from an event loop """

        return

        # sender/receiver/stream id's are automatically translated by the base class Connector
        # when calling methods _pop_message_to_send() and _push_received_message()

        # example of incoming message for the bot in private
        message = Message.build('Hi @globot please ask for service #sample')
        channel = Channel(11, 0, None, self.get_name())
        self._push_received_message(message, channel)

        # example of incoming message for the bot in chatroom
        message = Message.build('Hi @globot please ask for service #sample')
        channel = Channel(60, 0, 777, self.get_name())
        self._push_received_message(message, channel)

        # example of incoming message in chatroom for another agent
        message = Message.build('Hi folks nothing new today')
        channel = Channel(11, 60, 777, self.get_name())
        self._push_received_message(message, channel)

        # example of outgoing message
        while True:
            ret = self._pop_message_to_send()
            if ret is None: break
            (message, channel) = ret
コード例 #2
0
    def on_schedule(self):
        now = datetime.datetime.now()

        while len(self._incoming_messages) > 0:
            (message, channel) = self._incoming_messages.pop()
            logging.debug('service \033[32malarm\033[0m says "{}" {}'.format(
                message, channel))
            seconds = self.extract_time(message)
            if seconds is not None:
                self.__alarms.append((now, seconds, channel))
            else:
                channel = self._bot.reply(channel)
                message = Message.build(
                    '#alarm service does not understand your request')
                self._outgoing_messages.insert(0, (message, channel))

        triggered = list()
        waiting = list()

        for (timestamp, seconds, channel) in self.__alarms:
            alarm_time = timestamp + datetime.timedelta(seconds=seconds)
            if alarm_time > now:
                waiting.append((timestamp, seconds, channel))
            else:
                triggered.append((timestamp, seconds, channel))

        for (timestamp, seconds, channel) in triggered:
            message = Message.build(
                '#alarm service has been requested {} seconds ago'.format(
                    seconds))
            channel = self._bot.reply(channel)
            self._outgoing_messages.insert(0, (message, channel))
            logging.debug('service \033[32malarm\033[0m says "{}" {}'.format(
                message, channel))

        self.__alarms = waiting