def push_message(account_id, content, header=None): """ Send message to user. the package is the following JSON structure. reference - `Common Message Property <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: logging.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: logging.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 create_articles(title, type, content, account_id=None, attention_period_in_days=None): """ Create articles. reference - `Common Message Property <https://developers.worksmobile.com/jp/document/100180301?lang=en>`_ """ headers = { "charset": "UTF-8", "consumerKey": OPEN_API["consumerKey"] } board_no = get_value("{type}board".format(type=type), None) if board_no is None: logging.error("create articles. board no is None.") raise HTTPError(500, "create articles. board no is None.") body = { "title": title, "body": content, "boardNo": board_no, "domainId": DOMAIN_ID, "sendCreatedNotify": True, "useComment": True } if account_id is not None: body["accountId"] = account_id if attention_period_in_days is not None: body["attentionPeriodInDays"] = attention_period_in_days multi1 = MultipartEncoder( fields={"article":(None, json.dumps(body))} ) headers['content-type'] = multi1.content_type boards_url = API_BO["home"]["create_articles_url"] response = auth_post(boards_url, data=multi1, headers=headers) if response.status_code != 200 or response.content is None: logging.error( "create articles failed. url:%s text:%s headers:%s body:%s", boards_url, response.text, json.dumps(headers), multi1.to_string()) if response.status_code == 507: return storage_lack() else: return create_articles_failed() tmp_req = json.loads(response.content) article_no = tmp_req.get("articleNo", None) if article_no is None: logging.error("create articles failed. url:%s text:%s", boards_url, response.text) raise HTTPError(500, "create articles. article no is None.") return None
def create_boards(board): """ Create boards. reference - `Common Message Property <https://developers.worksmobile.com/jp/document/100180201?lang=en>`_ :return: board no """ body = { "domainId": DOMAIN_ID, "title": board["title"], "description": board["description"], "boardType": "BOARD" } headers = API_BO["headers"] headers["consumerKey"] = OPEN_API["consumerKey"] boards_url = API_BO["home"]["boards_url"] response = auth_post(boards_url, data=json.dumps(body), headers=headers) if response.status_code != 200 or response.content is None: logging.error("create boards failed. url:%s text:%s headers:%s body:%s", boards_url, response.text, json.dumps(headers), json.dumps(body)) raise Exception("create boards. http return code error.") tmp_req = json.loads(response.content) board_no = tmp_req.get("boardNo", None) if board_no is None: raise Exception("create boards. board no filed is None.") return board_no
def get_account_info(account_id): """ Get account information. reference - `Common Message Property <https://developers.worksmobile.com/kr/document/1006004/v1?lang=en>`_ :param account_id: user account id. :return: name(user name), department(user department). """ contacts_url = "%s?account=%s" % \ (API_BO["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: logging.error("get account info failed. url:%s text:%s body:%s", contacts_url, response.text, response.content) raise Exception("get account info. http return code error.") tmp_req = json.loads(response.content) info = tmp_req.get("data", None) if info is None: raise Exception("get account info. info filed is None.") name = info.get("name", None) department = info.get("groupName", None) return name, department
def upload_content(file_path): """ Upload rich menu background picture. reference - `Common Message Property <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) logging.info("upload content. url:%s", url) response = auth_post(url, files=files, headers=headers) if response.status_code != 200: logging.info("push message failed. url:%s text:%s body:%s", url, response.text, response.content) raise Exception("upload content. http return error.") resource_id = response.headers.get("x-works-resource-id", None) if resource_id is None: logging.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 resource_id
def set_rich_menu_image(resource_id, rich_menu_id): """ Set a rich menu image. reference - `Common Message Property <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) response = auth_post(url, data=json.dumps(body), headers=headers) if response.status_code != 200: logging.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.") logging.info("set rich menu image success. url:%s txt:%s body:%s", url, response.text, response.content)
def register_bot_domain(bot_no): """ Register a message bot domain. reference - `Common Message Property <https://developers.worksmobile.com/jp/document/1005004?lang=en>`_ :param bot_no: bot no """ url = API_BO["bot"] +"/"+ str(bot_no) + "/domain/" + str(DOMAIN_ID) data = {"usePublic": True, "usePermission": False} response = auth_post(url, data=json.dumps(data), headers=headers()) if response.status_code != 200: raise Exception("register bot domain field: code:%d content:%s" % ( response.status_code, response.text))
def register_bot(): """ Register a message bot. reference - `Common Message Property <https://developers.worksmobile.com/jp/document/1005002?lang=en>`_ :param photo_address: Access address of user's Avatar, If you need to change the user image, please replace the corresponding file in the image/, Only PNG file. :return: bot no """ url = API_BO["bot"] fmt = _("FAQ Ask Bot") a = lambda x, y: {"language": x, "name": y} b = lambda x, y: {"language": x, "description": y} data = { "name": BOT_NAME, "i18nNames": get_i18n_content(fmt, "register_bot", function=a), "photoUrl": PHOTO_URL, "description": BOT_NAME, "i18nDescriptions": get_i18n_content(fmt, "register_bot", function=b), "managers": [ADMIN_ACCOUNT], "submanagers": [], "useGroupJoin": False, "useDomainScope": False, "useCallback": True, "callbackUrl": CALLBACK_ADDRESS, "callbackEvents": ["text", "location", "sticker", "image"] } response = auth_post(url, data=json.dumps(data), headers=headers()) if response.status_code != 200: raise Exception("register bot field: code:%d text:%s" % (response.status_code, response.text)) tmp = json.loads(response.content) bot_no = tmp.get('botNo', None) if bot_no is None: raise Exception("register bot field: bot no is None") return bot_no
def set_user_specific_rich_menu(rich_menu_id, account_id): """ Set a user-specific rich menu. reference - `Common Message Property <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: logging.info("push message failed. url:%s text:%s body:%s", url, response.text, response.content) raise Exception("set user specific richmenu. http return error.") logging.info("set user specific richmenu 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 - `Common Message Property <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) fmt_label1 = _("\"Find FAQ\" by task") fmt_text1 = _("Do you have any work-related questions? " "\n\"Find FAQ\" by task") bound1 = make_bound(0, 0, 2500, 1160) action1 = make_i18n_postback_action( "query", "richmenu", "\"Find FAQ\" by task", fmt_label1, "Do you have any work-related " "questions? \n\"Find FAQ\" by task", fmt_text1) fmt2 = _("Send a question") bound2 = make_bound(0, 1160, 1250, 526) action2 = make_i18n_postback_action("enquire", "richmenu", "Send a question", fmt2, "Send a question", fmt2) fmt3 = _("Go to Initial Menu") bound3 = make_bound(1250, 1160, 1250, 526) action3 = make_i18n_postback_action("to_first", "richmenu", "Go to Initial Menu", fmt3, "Go to Initial Menu", fmt3) rich_menu = make_add_rich_menu(rich_menu_name, size, [ make_area(bound1, action1), make_area(bound2, action2), make_area(bound3, action3) ]) headers = API_BO["headers"] headers["consumerKey"] = OPEN_API["consumerKey"] url = API_BO["rich_menu_url"] url = utils.replace_url_bot_no(url) logging.info("register richmenu. url:%s", url) response = auth_post(url, data=json.dumps(rich_menu), headers=headers) if response.status_code != 200: logging.info("register richmenu failed. url:%s text:%s body:%s", url, response.text, response.content) raise Exception("register richmenu. http return error.") tmp = json.loads(response.content) rich_menu_id = tmp.get("richMenuId", None) if rich_menu_id is None: logging.error("register richmenu failed. url:%s txt:%s body:%s", url, response.text, response.content) raise Exception("register richmenu failed. rich menu id is None.") logging.info("register richmenu success. url:%s txt:%s body:%s", url, response.text, response.content) return rich_menu_id