#!/usr/bin/env python # --coding:utf-8-- import json from urllib import request import ssl import config APP_ID = config.FEISHU_APP_ID APP_SECRET = config.FEISHU_SECRET_KEY APP_VERIFICATION_TOKEN = config.FEISHU_VERIFICATION_TOKEN ssl._create_default_https_context = ssl._create_unverified_context def jiraReminder(email,text): access_token = get_tenant_access_token() if access_token == "": print("Fail to get the token") return send_message(access_token,email,text) return def get_tenant_access_token(): url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/" headers = { "Content-Type" : "application/json" } req_body = { "app_id": APP_ID, "app_secret": APP_SECRET } data = bytes(json.dumps(req_body), encoding='utf8') req = request.Request(url=url, data=data, headers=headers, method='POST') try: response = request.urlopen(req) except Exception as e: print(e) return "" rsp_body = response.read().decode('utf-8') rsp_dict = json.loads(rsp_body) code = rsp_dict.get("code", -1) if code != 0: print("get tenant_access_token error, code =", code) return "" return rsp_dict.get("tenant_access_token", "") def send_message(token, email, text): url = "https://open.feishu.cn/open-apis/message/v4/send/" headers = { "Content-Type": "application/json", "Authorization": "Bearer " + token } req_body = { "email": email, "msg_type": "text", "content": { "text": "There is a jira issue assigned to you,the link is "+text } } data = bytes(json.dumps(req_body), encoding='utf8') req = request.Request(url=url, data=data, headers=headers, method='POST') try: response = request.urlopen(req) except Exception as e: print(e.read().decode()) return rsp_body = response.read().decode('utf-8') rsp_dict = json.loads(rsp_body) code = rsp_dict.get("code", -1) if code != 0: print("send message error, code = ", code, ", msg =", rsp_dict.get("msg", ""),"link = ",text)