1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import json import requests,base64 def getsend(wecom_cid, wecom_aid, wecom_secret): get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}" response = requests.get(get_token_url).content access_token = json.loads(response).get('access_token') if access_token and len(access_token) > 0: send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}' def send(text, wecom_touid='@all'): data = { "touser":wecom_touid, "agentid":wecom_aid, "msgtype":"text", "text":{ "content":text }, "duplicate_check_interval":600 } response = requests.post(send_msg_url,data=json.dumps(data)).content return response else: def send(text, wecom_touid='@all'): print('get_token Failed') return send
''' WECOM_CID企业微信公司ID WECOM_AID企业微信应用ID WECOM_SECRET企业微信应用Secret wecom_touid消息发送对象的UID ''' info = getsend( wecom_cid = '***', wecom_aid = '***', wecom_secret = '***' ) wecom_touid = 'limour'
def lambda_handler(event, context): args = event.get('body') args = json.loads(args) if args.get('token') != 'a123456': return { 'statusCode': 404, 'body': json.dumps('invalid token!') } response = info( text = args.get('msg'), wecom_touid = args.get('touid') ) body = {} body["event"] = event.get('body') return { 'statusCode': 200, "isBase64Encoded": False, "headers": {"Content-Type" : "application/json"}, 'body': json.dumps(body) }
|