def upload_content(file_path):
    """
    Upload rich menu background picture.

        reference
        - https://developers.worksmobile.com/jp/document/1005025?lang=en

    :param file_path: resource local path
    :return: resource id
    """
    headers = {
        "consumerKey": OPEN_API["consumerKey"],
        "x-works-apiid": OPEN_API["apiId"]
    }

    files = {'resourceName': open(file_path, 'rb')}

    url = API_BO["upload_url"]
    url = utils.replace_url_bot_no(url)

    LOGGER.info("upload content . url:%s", url)

    response = auth_post(url, files=files, headers=headers)
    if response.status_code != 200:
        LOGGER.info("push message failed. url:%s text:%s body:%s", url,
                    response.text, response.content)
        raise Exception("upload content. http return error.")
    if "x-works-resource-id" not in response.headers:
        LOGGER.error("invalid content. url:%s txt:%s headers:%s", url,
                     response.text, response.headers)
        raise Exception("upload content. not fond 'x-works-resource-id'.")
    return response.headers["x-works-resource-id"]
def push_message(account_id, content, header=None):
    """
    Send message to user. the package is the following JSON structure.

        reference
        - https://developers.worksmobile.com/jp/document/1005008?lang=en

    :param account_id: user account id
    :param content: message content
    :param header: http header
    """

    if content is None:
        LOGGER.info("content is None.")
        raise HTTPError(500, "internal error. content is None.")

    request = {"accountId": account_id, "content": content}

    headers = API_BO["headers"]
    if header is not None:
        headers = Merge(header, headers)

    headers["consumerKey"] = OPEN_API["consumerKey"]

    url = API_BO["push_url"]
    url = replace_url_bot_no(url)
    response = auth_post(url, data=json.dumps(request), headers=headers)
    if response.status_code != 200:
        LOGGER.error("push message failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. Internal interface call error.")
def set_rich_menu_image(resource_id, rich_menu_id):
    """
    Set a rich menu image.

        reference
        - https://developers.worksmobile.com/jp/document/100504002?lang=en

    :param resource_id: resource id
    :param rich_menu_id: rich menu id
    :return:
    """
    body = {"resourceId": resource_id}

    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]

    url = API_BO["rich_menu_url"] + "/" + rich_menu_id + "/content"
    url = utils.replace_url_bot_no(url)
    LOGGER.info("set rich menu image . url:%s", url)

    response = auth_post(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.info("set rich menu image failed. url:%s text:%s body:%s", url,
                    response.text, response.content)
        raise Exception("set richmenu image. http return error.")

    LOGGER.info("set rich menu image success. url:%s txt:%s body:%s", url,
                response.text, response.content)
Exemple #4
0
def make_add_rich_menu_body(rich_menu_name):
    """
    add rich menu body

        reference
        - https://developers.worksmobile.com/jp/document/100504001?lang=en

    :param rich_menu_name: rich menu name
    :return: rich menu id
    """
    size = make_size(2500, 1686)

    fmt0 = _("Record clock-in")
    bound0 = make_bound(0, 0, 1250, 1286)
    action0 = make_i18n_postback_action("sign_in", "richmenu", "Record clock-in",
                                   fmt0, "Record clock-in", fmt0)

    fmt1 = _("Record clock-out")
    bound1 = make_bound(1250, 0, 1250, 1286)
    action1 = make_i18n_postback_action("sign_out", "richmenu", "Record clock-out",
                                   fmt1, "Record clock-out", fmt1)

    fmt2 = _("Start over")
    bound2 = make_bound(0, 1286, 2500, 400)
    action2 = make_i18n_postback_action("to_first", "richmenu", "Start over",
                                   fmt2, "Start over", fmt2)

    rich_menu = make_add_rich_menu(
                    rich_menu_name,
                    size,
                    [
                        make_area(bound0, action0),
                        make_area(bound1, action1),
                        make_area(bound2, action2)
                    ])

    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]

    url = API_BO["rich_menu_url"]
    url = utils.replace_url_bot_no(url)

    LOGGER.info("register richmenu. url:%s", url)
    print(url)

    response = auth_post(url, data=json.dumps(rich_menu), headers=headers)
    if response.status_code != 200:
        LOGGER.info("register richmenu failed. url:%s text:%s body:%s",
                    url, response.text, response.content)
        raise Exception("register richmenu. http return error.")

    LOGGER.info("register richmenu success. url:%s txt:%s body:%s",
                url, response.text, response.content)

    tmp = json.loads(response.content)
    return tmp["richMenuId"]
Exemple #5
0
def create_schedule(current, end, begin, account_id, title):
    """
    create schedule.

        reference
        - https://developers.worksmobile.com/jp/document/100702703?lang=en

    :return: schedule id.
    """

    uid = str(uuid.uuid4()) + account_id
    schedule_data = make_icalendar_data(uid, title, current, end, begin,
                                        account_id, True)
    body = {"ical": schedule_data}

    calendar_id = get_value(API_BO["calendar"]["name"], None)
    if calendar_id is None:
        LOGGER.error("get calendar from cached failed.")
        raise HTTPError(500, "internal error. get calendar is failed.")

    headers = create_headers()
    url = API_BO["calendar"]["create_schedule_url"]
    url = url.replace("_ACCOUNT_ID_", ADMIN_ACCOUNT)
    url = url.replace("_CALENDAR_ID_", calendar_id)

    print(url)

    response = auth_post(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("create schedules failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500,
                        "internal error. create schedule http code error.")

    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. http response error.")

    LOGGER.info("create schedule. url:%s text:%s body:%s", url, response.text,
                response.content)

    return_value = tmp_req.get("returnValue", None)
    if return_value is None:
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. create schedule content error.")

    schedule_uid = return_value.get("icalUid", None)
    if schedule_uid is None:
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. create schedule content error.")
    return schedule_uid
def get_user_info_by_account(account_id):
    """
    Get user info of account.

        reference
        - https://developers.worksmobile.com/jp/document/1006004/v1?lang=en

    If you fail to get external key,
    log in to the development console to check your configuration.

        reference
        - https://auth.worksmobile.com/login/login?

    accessUrl=https%3A%2F%2Fdevelopers.worksmobile.com
    %3A443%2Fconsole%2Fopenapi%2Fmain)

    :return: external key
    """
    contacts_url = "%s?account=%s" % \
                       (API_BO["TZone"]["contacts_url"], account_id)
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        "charset": "UTF-8",
        "consumerKey": OPEN_API["consumerKey"]
    }

    print(contacts_url)

    response = auth_post(contacts_url, headers=headers)
    if response.status_code != 200 or response.content is None:
        LOGGER.error("get user info failed. url:%s text:%s body:%s",
                     contacts_url, response.text, response.content)
        raise HTTPError(500, "get user info. http return code error.")
    tmp_req = json.loads(response.content)
    data = tmp_req.get("data", None)
    if data is None:
        raise HTTPError(500, "get user info. data filed is None.")
    name = data.get("name", None)
    if name is None:
        raise HTTPError(500, "internal error. name filed is none")
    return name
Exemple #7
0
def create_calendar():
    """
    create calender.

        reference
        - https://developers.worksmobile.com/jp/document/100702701?lang=en

    :return: calendar id.
    """
    body = {
        "name":
        "Attendance management bot",
        "description":
        "Attendance management bot",
        "invitationUserList": [{
            "email": ADMIN_ACCOUNT,
            "actionType": "insert",
            "roleId": 2
        }]
    }

    headers = create_headers()
    url = API_BO["calendar"]["create_calendar_url"]
    url = url.replace("_ACCOUNT_ID_", ADMIN_ACCOUNT)
    LOGGER.info("create calendar. url:%s body:%s", url, str(body))
    print(url)

    response = auth_post(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("create calendar failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise Exception("create calendar id. http response code error.")

    LOGGER.info("create calendar id. url:%s txt:%s body:%s", url,
                response.text, response.content)
    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("create calendar failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise Exception("create calendar id. response no success.")
    return tmp_req["returnValue"]
def set_user_specific_rich_menu(rich_menu_id, account_id):
    """
    Set a user-specific rich menu.

        reference
        - https://developers.worksmobile.com/jp/document/100504010?lang=en

    :param rich_menu_id: rich menu id
    :param account_id: user account id
    """
    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]
    url = API_BO["rich_menu_url"] + "/" \
          + rich_menu_id + "/account/" + account_id

    url = utils.replace_url_bot_no(url)

    response = auth_post(url, headers=headers)
    if response.status_code != 200:
        LOGGER.info("push message failed. url:%s text:%s body:%s", url,
                    response.text, response.content)
        raise Exception("set user specific richmenu. http return error.")
    LOGGER.info("set user specific richmenu success. url:%s txt:%s body:%s",
                url, response.text, response.content)