def create_calendar(): 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("_EXTERNAL_KEY_", load_external_key()) LOGGER.info("create calendar. url:%s body:%s", url, str(body)) 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 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 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 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"] } 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 Exception("get user info. http return code error.") tmp_req = json.loads(response.content) data = tmp_req.get("data", None) if data is None: raise Exception("get user info. data filed is None.") external_key = data.get("externalKey", None) name = data.get("name", None) if external_key is None: raise Exception("get user info. external_key filed is None.") return external_key, name
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"]
def create_schedule(current, end, begin, account_id, title): """ create schedule. reference: https://developers.worksmobile.com/kr/document/100702703?lang=ko :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("_EXTERNAL_KEY_", load_external_key()) url = url.replace("_CALENDAR_ID_", calendar_id) 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 set_user_specific_rich_menu(rich_menu_id, 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)
def create_schedule(current, end, begin, account_id): summarys = {"kr": "출근시간", "jp": "出勤時間", "en": "Clock-in time"} uid = str(uuid.uuid4()) + account_id schedule_data = make_icalendar_data(uid, summarys[LOCAL], 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("_EXTERNAL_KEY_", load_external_key()) url = url.replace("_CALENDAR_ID_", calendar_id) 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 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)
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.")
def get_external_key_from_remote(): external_key_url = API_BO["TZone"]["external_key_url"] headers = { "content-type": "application/x-www-form-urlencoded", "charset": "UTF-8", "consumerKey": OPEN_API["consumerKey"] } response = auth_post(external_key_url, headers=headers) if response.status_code != 200 or response.content is None: LOGGER.error("get external key failed. url:%s text:%s body:%s", external_key_url, response.text, response.content) raise Exception("get external key. http return code error.") tmp_req = json.loads(response.content) data = tmp_req.get("data", None) if data is None: raise Exception("get external key. data filed is None.") external_key = data.get("externalKey", None) if external_key is None: raise Exception("get external key. external_key filed is None.") return external_key
def set_user_specific_rich_menu(rich_menu_id, account_id): """ Set a user-specific rich menu. reference: https://developers.worksmobile.com/kr/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)
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"]
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"]