Пример #1
0
 def on_command(self, msg, stdin, stdout):
     if len(msg["args"]) >= 2:
         cmd = msg["args"][1]
         if cmd == "in" or cmd == "at":
             try:
                 date = parse_datetime(" ".join(msg["args"][1:]))
             except ValueError:
                 raise smartbot.StopCommand("I don't understand that date.")
             else:
                 duration = (date - datetime.datetime.now()).total_seconds()
                 time.sleep(max(0, duration))
         else:
             raise StopCommand("‘{}’ is not a valid command.".format(cmd))
     else:
         raise StopCommandWithHelp(self)
Пример #2
0
    def on_command(self, msg, stdin, stdout):
        pattern_str = r"remind (me|[^\s]+) (to|about|that) (.*) (in|at) (.*)$"
        match = re.match(pattern_str, msg["message"], re.IGNORECASE)
        if not match:
            match = re.match(pattern_str, stdin.getvalue().strip(),
                             re.IGNORECASE)

        if match:
            try:
                date = parse_datetime("{0} {1}".format(match.group(4),
                                                       match.group(5)))
            except ValueError:
                raise StopCommand("I don't understand that date.")
            else:
                message = None
                if match.group(1) == "me" or match.group(1) == msg["sender"]:
                    self.bot.send(msg['reply_to'],
                                  "Sure thing {0}, I'll remind you on {1}."
                                  .format(msg["sender"],
                                          date.strftime("%c").strip()))
                    message = "{0}: you asked me to remind you {1} {2}" \
                              .format(msg["sender"], match.group(2),
                                      match.group(3))
                else:
                    self.bot.send(msg['reply_to'],
                                  "Sure thing {0}, I'll remind {1} on {2}."
                                  .format(msg["sender"], match.group(1),
                                          date.strftime("%c").strip()))
                    message = "{0}: {1} asked me to remind you {2} {3}" \
                              .format(match.group(1), msg["sender"],
                                      match.group(2), match.group(3))

                total_seconds = (date - datetime.datetime.now()) \
                    .total_seconds()
                duration = max(0, total_seconds)
                time.sleep(duration)
                print(message, file=stdout)