import requests import json def SendWeChatWorkMessage(access_token, agent_id, title, description, _url, btntxt="更多", to_user=None, to_party=None, to_tag=None): """ 发送企业微信文本卡片消息 :param access_token: 调用接口凭证 :param agent_id: 企业应用的id :param title: 卡片标题 :param description: 卡片描述内容(支持HTML) :param url: 点击后跳转的链接 :param btntxt: 按钮文字,默认为"更多" :param to_user: 接收消息的成员,多个用'|'分隔,None表示不指定 :param to_party: 接收消息的部门,多个用'|'分隔,None表示不指定 :param to_tag: 接收消息的标签,多个用'|'分隔,None表示不指定 :return: 接口返回结果 """ url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}" message = { "msgtype": "textcard", "agentid": agent_id, "textcard": { "title": title, "description": description, "url": _url, "btntxt": btntxt }, "enable_id_trans": 0, "enable_duplicate_check": 0, "duplicate_check_interval": 1800 } if to_user: message["touser"] = to_user if to_party: message["toparty"] = to_party if to_tag: message["totag"] = to_tag headers = {'Content-Type': 'application/json; charset=utf-8'} response = requests.post(url, headers=headers, data=json.dumps(message, ensure_ascii=False).encode('utf-8')) return response.json() if __name__ == "__main__": ACCESS_TOKEN = "EzUNqv2s_S_MZ70bmJuBL5-hp2tB1eX7I3jbLxb5kdUSUZXd4TlCJY92scfQrSJgvd8jIMRpR1B2wsqrFxfU0oyuu-pvP5kvBNgkxyIJ-w2aX2cLGHi7cC4sCYbAqAAP92wI3DjK7SrXFkyLBd6BRQdUjyInpQObzjyUzOSiRK8IRTKB-Svs0UV9PRkfx54gBIgX5FNk_fdX9Uonp_kIjQ" AGENT_ID = 1000003 TITLE = "领奖通知" DESCRIPTION = """
3000年13月32日
请尽快前往工作室系统签到
否则经人力资源部确认后将发起离职
""" URL = "https://manage.gvsds.com/" BTNTXT = "查看详情" TO_USER = "WangYuTong" TO_PARTY = "@all" TO_TAG = "@all" result = SendWeChatWorkMessage(ACCESS_TOKEN, AGENT_ID, TITLE, DESCRIPTION, URL, BTNTXT, TO_USER, TO_PARTY, TO_TAG) print("发送结果:") print(json.dumps(result, indent=2, ensure_ascii=False))