Exemplo n.º 1
0
    def post(self, *args, **kwargs):
        from wxpy import ensure_one

        content = self.get_argument('content', '')
        search = self.get_argument('search', '')
        token = self.get_argument('token', None)
        default_receiver = self.get_argument('receiver', None)

        if glb.token:
            if glb.token != token:
                self.status_code = STATUS_PERMISSION_DENIED
                self.write('Token is missing')
                return
        try:
            search = json.loads(search)
        except ValueError:
            search = search
        try:
            if isinstance(search, dict):
                receiver = ensure_one(
                    glb.wxbot.bot.friends(update=True).search(**search))
            else:
                receiver = ensure_one(
                    glb.wxbot.bot.friends(update=True).search(search))
        except ValueError:
            receiver = None
        if receiver:
            receiver.send_msg(content)
        else:
            msg = '消息发送失败,没有找到接收者。\n[搜索条件]: {0}\n[消息内容]:{1}'.format(
                search, content)
            message = Message(msg, receiver=default_receiver)
            glb.wxbot.send_msg(message)
            _logger.info(msg)
        self.write('Success')
Exemplo n.º 2
0
def check_bot(task_type=SYSTEM_TASK):
    """
    wxpy bot 健康检查任务
    """
    if glb.wxbot.bot.alive:
        msg = generate_run_info()
        message = Message(content=msg, receiver='status')
        glb.wxbot.send_msg(message)
        _logger.info(
            '{0} Send status message {1} at {2:%Y-%m-%d %H:%M:%S}'.format(
                task_type, msg, datetime.datetime.now()))
    else:
        # todo
        pass
Exemplo n.º 3
0
    def post(self, *args, **kwargs):
        content = self.get_argument('content', '')
        title = self.get_argument('title', '')
        task_time = self.get_argument('time', None)
        remind = int(self.get_argument('remind', DEFAULT_REMIND_TIME))
        token = self.get_argument('token', None)
        receiver = self.get_argument('receiver', None)

        if glb.token:
            if glb.token != token:
                self.status_code = STATUS_PERMISSION_DENIED
                self.write('Token is missing')
                return
        if task_time:
            try:
                task_time = datetime.datetime.strptime(task_time,
                                                       '%Y-%m-%d %H:%M:%S')
                timestamp = time.mktime(
                    (task_time -
                     datetime.timedelta(seconds=remind)).timetuple())
            except ValueError as e:
                self.status_code = STATUS_ERROR
                self.write(e.message)
                _logger.exception(e)
                return
        else:
            task_time = datetime.datetime.now()
            timestamp = int(time.mktime(task_time.timetuple()))
        try:
            message = Message(content,
                              title,
                              task_time,
                              datetime.timedelta(seconds=remind),
                              receiver=receiver)
            self.ioloop.call_at(timestamp, self.delay_task, DELAY_TASK,
                                message)
            self.write('Success')
        except Exception as e:
            self.status_code = STATUS_TORNADO_EXCEPTION
            self.write(e.message)
            _logger.exception(e)
Exemplo n.º 4
0
    def post(self, *args, **kwargs):
        message = self.get_argument('content', None)
        token = self.get_argument('token', None)
        receiver = self.get_argument('receiver', None)
        if not message:
            self.status_code = STATUS_ERROR
            self.write('Content is required')
            return

        if glb.token:
            if glb.token != token:
                self.status_code = STATUS_PERMISSION_DENIED
                self.write('Token is missing')
                return
        try:
            msg = Message(message, receiver=receiver)
            glb.wxbot.send_msg(msg)
            self.write('Success')
        except Exception as e:
            _logger.exception(e)
            self.status_code = STATUS_BOT_EXCEPTION
            self.write(e.message)
Exemplo n.º 5
0
    def post(self, *args, **kwargs):
        content = self.get_argument('content', '')
        title = self.get_argument('title', '')
        interval = self.get_argument('interval', None)
        token = self.get_argument('token', None)
        receiver = self.get_argument('receiver', None)

        if glb.token:
            if glb.token != token:
                self.status_code = STATUS_PERMISSION_DENIED
                self.write('Token is missing')
                return
        if not interval:
            self.status_code = STATUS_ERROR
            self.write('interval is required')
            return
        try:
            interval = int(interval)
        except Exception as e:
            self.status_code = STATUS_ERROR
            self.write('interval must be a integer')
        try:
            message = Message(content,
                              title=title,
                              interval=datetime.timedelta(seconds=interval),
                              receiver=receiver)
            user_periodic = tornado.ioloop.PeriodicCallback(
                functools.partial(self.periodic_task, PERIODIC_TASK, message),
                interval * 1000, self.ioloop)
            glb.periodic_list.append(user_periodic)
            user_periodic.start()
            self.write('Success')
        except Exception as e:
            self.status_code = STATUS_TORNADO_EXCEPTION
            self.write(e.message)
            _logger.exception(e)