腳本用於企業微信發送消息,可以使用普通微信關注企業微信帳戶後實現微信消息推送
# -*- coding: utf-8 -*-
# coding=utf-8
import json
import requests
#企業惟一ID
corpid = "xxxxxx"
#自定義應用密鑰
secret = "xxxxxxx"
#自定義應用編號
agentid = "xxxxxxx"
def GetTokenFromServer(Corpid,Secret):
access_token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+secret
r = requests.get(url=access_token_url)
print(r.json()['access_token'])
return r.json()['access_token']
def send_wechat():
access_token = GetTokenFromServer(corpid,secret)
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % access_token
headers={
"Content-Type": "application/json"
}
data={
"touser": "xxx", #消息接收人,多個用|分隔 如: xxx|xxx|xxx
#"toparty" : "1", #如須要發送消息到整個部門的人,則使用該參數, 數字表明該部門 ID
"agentid" : agentid, #自定義應用編號
"msgtype" : "text", #發送類型
"text": {
"content": "hello world." #內容
}
}
data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8')
r = requests.post(url=Url, headers=headers, data=data_dict)
print(r.text)
send_wechat()