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 upload_content(file_path):
    """
    Upload rich menu background picture.
    reference: https://developers.worksmobile.com/kr/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 get_rich_menus():
    """
    Get rich menus
    reference: https://developers.worksmobile.com/kr/document/100504004?lang=en

    :return: rich menu list
    """
    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]
    url = API_BO["rich_menu_url"]
    url = utils.replace_url_bot_no(url)

    LOGGER.info("push message begin. url:%s", url)
    response = auth_get(url, headers=headers)
    if response.status_code != 200:
        LOGGER.info("push message failed. url:%s text:%s body:%s", url,
                    response.text, response.content)
        return None

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

    tmp = json.loads(response.content)
    if "richmenus" in tmp:
        return tmp["richmenus"]

    return None
def set_rich_menu_image(resource_id, rich_menu_id):
    """
    Set a rich menu image.
    reference: https://developers.worksmobile.com/kr/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)
def make_add_rich_menu_body(rich_menu_name):
    """
    add rich menu body
    reference: https://developers.worksmobile.com/kr/document/100504001?lang=en

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

    bound0 = make_bound(0, 0, 1250, 1286)
    action0 = make_postback_action(
        "sign_in",
        display_text="Record clock-in",
        label="Record clock-in",
    )

    bound1 = make_bound(1250, 0, 1250, 1286)
    action1 = make_postback_action("sign_out",
                                   display_text="Record clock-out",
                                   label="Record clock-out")

    bound2 = make_bound(0, 1286, 2500, 400)
    action2 = make_postback_action("to_first",
                                   display_text="Start over",
                                   label="Start over")

    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)

    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"]
Example #6
0
def canncel_user_specific_rich_menu(account_id):
    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]
    url = API_BO["rich_menu_url"] + "/account/" + account_id
    url = utils.replace_url_bot_no(url)

    response = auth_del(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("canncel user specific richmenu. http return error.")
    LOGGER.info("push message success. url:%s txt:%s body:%s",
                url, response.text, response.content)
def cancel_user_specific_rich_menu(account_id):
    """
    Cancel a user-specific rich menu
    reference: https://developers.worksmobile.com/kr/document/100504012?lang=en

    :param account_id: user account id
    """
    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]
    url = API_BO["rich_menu_url"] + "/account/" + account_id
    url = utils.replace_url_bot_no(url)

    response = auth_del(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("canncel user specific richmenu. http return error.")
    LOGGER.info("push message success. url:%s txt:%s body:%s", url,
                response.text, response.content)
Example #8
0
def set_rich_menu_image(resource_id, rich_menu_id):

    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)
Example #9
0
def get_rich_menus():
    headers = API_BO["headers"]
    headers["consumerKey"] = OPEN_API["consumerKey"]
    url = API_BO["rich_menu_url"]
    url = utils.replace_url_bot_no(url)

    LOGGER.info("push message begin. url:%s", url)
    response = auth_get(url, headers=headers)
    if response.status_code != 200:
        LOGGER.info("push message failed. url:%s text:%s body:%s",
                    url, response.text, response.content)
        return None

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

    tmp = json.loads(response.content)
    if "richmenus" in tmp:
        return tmp["richmenus"]

    return None
Example #10
0
def push_message(account_id, content, header=None):

    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.")
Example #11
0
def upload_content(file_path):
    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"]
Example #12
0
def make_add_rich_menu_body(rich_menu_name):
    size = make_size(2500, 1686)

    bound0 = make_bound(0, 0, 1250, 1286)
    jp_text0 = i18n_display_text("ja_JP", "出勤を記録する")
    en_text0 = i18n_display_text("en_US", "Record clock-in")
    kr_text0 = i18n_display_text("ko_KR", "출근 기록하기")
    display_text0 = [jp_text0, en_text0, kr_text0]

    jp_label_text0 = make_i18n_label("ja_JP", "出勤を記録する")
    en_label_text0 = make_i18n_label("en_US", "Record clock-in")
    kr_label_text0 = make_i18n_label("ko_KR", "출근 기록하기")
    display_label0 = [jp_label_text0, en_label_text0, kr_label_text0]

    action0 = make_postback_action("sign_in",
                                   display_text="출근 기록하기",
                                   label="출근 기록하기",
                                   i18n_display_texts=display_text0,
                                   i18n_labels=display_label0)

    bound1 = make_bound(1250, 0, 1250, 1286)
    jp_text1 = i18n_display_text("ja_JP", "退勤を記録する")
    en_text1 = i18n_display_text("en_US", "Record clock-out")
    kr_text1 = i18n_display_text("ko_KR", "퇴근 기록하기")
    display_text1 = [jp_text1, en_text1, kr_text1]

    jp_label_text1 = make_i18n_label("ja_JP", "退勤を記録する")
    en_label_text1 = make_i18n_label("en_US", "Record clock-out")
    kr_label_text1 = make_i18n_label("ko_KR", "퇴근 기록하기")
    display_label1 = [jp_label_text1, en_label_text1, kr_label_text1]

    action1 = make_postback_action("sign_out",
                                   display_text="퇴근 기록하기",
                                   label="퇴근 기록하기",
                                   i18n_display_texts=display_text1,
                                   i18n_labels=display_label1)

    bound2 = make_bound(0, 1286, 2500, 400)
    jp_text2 = i18n_display_text("ja_JP", "最初へ")
    en_text2 = i18n_display_text("en_US", "Start over")
    kr_text2 = i18n_display_text("ko_KR", "처음으로")
    display_text2 = [jp_text2, en_text2, kr_text2]

    jp_label_text2 = make_i18n_label("ja_JP", "最初へ")
    en_label_text2 = make_i18n_label("en_US", "Start over")
    kr_label_text2 = make_i18n_label("ko_KR", "처음으로")
    display_label2 = [jp_label_text2, en_label_text2, kr_label_text2]

    action2 = make_postback_action("to_first",
                                   display_text="처음으로",
                                   label="처음으로",
                                   i18n_display_texts=display_text2,
                                   i18n_labels=display_label2)

    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)

    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"]