原文鏈接: https://www.cnblogs.com/supery007/p/8136295.html
1. 公眾號測試地址
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
?
?
?
2. 代碼
# pip3 install requests
import requests
import json
def get_access_token():
"""
獲取微信全局接口的憑證(默認(rèn)有效期倆個(gè)小時(shí))
如果不每天請求次數(shù)過多, 通過設(shè)置緩存即可
"""
result = requests.get(
url="https://api.weixin.qq.com/cgi-bin/token",
params={
"grant_type": "client_credential",
"appid": "wx2499da7621f818e8",
"secret": "6239e3dfc5af686777ea40b9f3df5f48",
}
).json()
if result.get("access_token"):
access_token = result.get('access_token')
else:
access_token = None
return access_token
def sendmsg(openid,msg):
access_token = get_access_token()
body = {
"touser": openid,
"msgtype": "text",
"text": {
"content": msg
}
}
response = requests.post(
url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
params={
'access_token': access_token
},
data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')
)
# 這里可根據(jù)回執(zhí)code進(jìn)行判定是否發(fā)送成功(也可以根據(jù)code根據(jù)錯(cuò)誤信息)
result = response.json()
print(result)
if __name__ == '__main__':
sendmsg('關(guān)注者的ID','發(fā)送消息內(nèi)容')
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

