Exemple #1
0
async def push():
    bot = nb.get_bot()

    query: dict = json.loads(await request.get_data())
    qq_: Optional[str] = query.get("qq")
    msg_: Optional[str] = query.get("msg")
    token_: Optional[str] = query.get("token")

    result, msg = check_args(qq=qq_, msg=msg_, token=token_)

    rcode_ = 403
    rmsg_ = msg

    if result and qq_:
        encrypt_qq = to_token(qq_)

        logger.info(
            f"qq: {qq_} msg: {msg_} token: {token_} encrypt_qq: {encrypt_qq}")
        if token_ == encrypt_qq:
            try:
                await bot.send_private_msg(user_id=qq_, message=msg_)
                rcode_ = 200
                rmsg_ = "发送成功"
            except CQHttpError:
                rcode_ = 500
                rmsg_ = "向用户发消息失败!"
            bot._server_app.config["JSONIFY_MIMETYPE"] = "text/html"
        else:
            rmsg_ = "验证信息错误"
    logger.info(f"rcode_: {rcode_} rmsg_: {rmsg_}")
    return jsonify(code=rcode_, msg=rmsg_)
Exemple #2
0
    async def post(self):
        query: dict = request.args
        qq: Optional[str] = query.get("qq")
        token: Optional[str] = query.get("token")

        data = await request.get_data()

        _, msg = check_args(qq=qq, token=token)
        if not _:
            return false_ret(msg=msg)

        if to_token(qq) != token:
            abort(403)
        data = json.loads(data)

        error_key = []
        process_info = defaultdict(dict)
        for key, enable in data.items():
            SubC = judge_sub(key)
            if not SubC:
                error_key.append(key)
                continue
            if enable:
                result, p_msg = await SubC.add_sub(qq2event(qq),
                                                   key)  # type: ignore
            else:
                result, p_msg = await SubC.del_sub(qq2event(qq),
                                                   key)  # type: ignore
            process_info[key]["result"] = result
            process_info[key]["msg"] = p_msg
        if error_key:
            return false_ret(data=error_key, msg="error keys")
        return true_ret(data=process_info)
Exemple #3
0
async def bind():
    res = await request.get_json()
    qq = res.get("qq")
    username = res.get("username")
    password = res.get("password")
    token = res.get("token")

    logger.debug("username:{} password:{} token:{}".format(
        username, password, token))

    result, msg = check_args(qq=qq,
                             username=username,
                             password=password,
                             token=token)

    if to_token(qq) != token:
        abort(403)

    if not result:
        return false_ret(msg=msg)

    logger.info("qq{}正在请求绑定!".format(qq))
    # 是否已经绑定
    user = await User.get(str(qq))

    _bot = get_bot()
    if user is None:
        logger.info("qq{}是新用户,正在尝试登录教务处...".format(qq))
        u = Login(username, password)
        is_log, log_resp = await run_sync_func(u.try_login)
        if is_log:
            user_info = await run_sync_func(log_resp.json)
            logger.debug(user_info)
            await logger.complete()

            await User.add(
                qq=qq,
                student_id=username,
                password=password,
                cookies=pickle.dumps(u.get_cookies()),
            )
            logger.info("qq{}绑定成功!".format(qq))
            await _bot.send(qq2event(qq), "教务处绑定成功!")
            await _bot.send(qq2event(qq), "可以向我发送 帮助 来继续使用~")
            await _bot.send(qq2event(qq), "点击 https://bot.artin.li 来查看帮助~")
            await _bot.send(qq2event(qq),
                            f"点击个人中心可以配置更多: {get_user_center(qq2event(qq))}")
            return true_ret("qq绑定成功!")
        else:
            logger.info("qq{}绑定失败!".format(qq))
            await _bot.send(qq2event(qq), "教务处绑定失败!")
            return false_ret("qq绑定失败!失败原因是{}".format(log_resp))
    return false_ret("该qq已经绑定了!")
Exemple #4
0
    async def get(self):
        query: dict = request.args
        print("query: ", query)
        qq: Optional[str] = query.get("qq")
        token: Optional[str] = query.get("token")

        result, msg = check_args(qq=qq, token=token)
        if not result:
            return false_ret(msg=msg)

        if to_token(qq) != token:
            abort(403)

        subs = await get_subs(qq2event(int(qq)))  # type: ignore
        return true_ret(data=subs)
Exemple #5
0
async def unbind():
    qq = request.args.get("qq")
    token = request.args.get("token")
    result, msg = check_args(qq=qq, token=token)

    if to_token(qq) != token:
        abort(403)
    if not result:
        return false_ret(msg=msg)

    logger.info("qq {}正在请求解绑!".format(qq))

    try:
        await User.unbind(qq)
        logger.info("qq {}请求解绑成功!".format(qq))
        return true_ret(msg="解除绑定成功!")
    except Exception as e:
        logger.exception(e)

    return false_ret(msg="没有这个用户!")
Exemple #6
0
    async def get(self):
        query: dict = request.args
        qq: Optional[str] = query.get("qq")
        token: Optional[str] = query.get("token")

        _, msg = check_args(qq=qq, token=token)
        if not _:
            return false_ret(msg=msg)

        if to_token(qq) != token:
            abort(403)

        user_subs = await SubWrapper.get_user_sub(qq2event(qq))  # type: ignore
        available_subs = SubWrapper.get_subs()
        result = defaultdict(dict)
        for k, v in available_subs.items():
            result[k]["name"] = v
            if user_subs.get(k):
                result[k]["enable"] = True
            else:
                result[k]["enable"] = False
        return true_ret(data=result)