日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python微信接口发送消息_Python 微信公众号发送消息

發布時間:2025/1/21 86 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python微信接口发送消息_Python 微信公众号发送消息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#pip3 install requests

importrequestsimportjsondefget_access_token():"""獲取微信全局接口的憑證(默認有效期倆個小時)

如果不每天請求次數過多, 通過設置緩存即可"""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=Nonereturnaccess_tokendefsendmsg(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')

)#這里可根據回執code進行判定是否發送成功(也可以根據code根據錯誤信息)

result =response.json()print(result)if __name__ == '__main__':

sendmsg('關注者的ID','發送消息內容')

總結

以上是生活随笔為你收集整理的python微信接口发送消息_Python 微信公众号发送消息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。