Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to push Python data to Wechat Enterprise account

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to push Python data to the Wechat enterprise account. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Individual registration WeCom account

The use of requests

First, you need to install the requests library: pip install requests

Then import and use through import requests

The requests.get () method can get a web page, and requests.post () can send a POST request

More can be seen at http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

WeCom's parameters

Corpid: represents enterprise ID, which can be found in WeCom-> my Enterprise-> Enterprise ID

Secret: Secret of App, in App and Mini Program-> created App-> Secret

PartyID: address book department ID, address book-> department-> department ID

Agentid: application ID, in Apps and Mini Program-> created applications-> AgentId

JSON response content

There is also a built-in JSON decoder in Requests to process JSON data, such as r.json (). If JSON decoding fails, r.json () throws an exception.

It should be noted that a successful call to r.json () does not mean the success of the response. Some servers will include a JSON object (such as the error details of HTTP 500s) in the failed response, and this JSON will be decoded and returned. To check whether the request is successful, use r.raise_for_status () or check that r.status_code is the same as your expectations

Send test data to WeCom import requestsimport sysimport jsondef GetToken (Corpid,Secret): Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = {" corpid ": Corpid," corpsecret ": Secret} r = requests.get (url=Url,params=Data) Token = r.json () ['access_token'] return Tokendef SendMessage (Token,Agentid,Subject,Content ): Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"% Token Data = {" toparty ": PartyID," msgtype ":" text "," agentid ": Agentid," text ": {" content ": Subject +'\ n' + Content}," safe ":" 0 "} ret = requests.post (url=Url Data=json.dumps (Data) return ret.textif _ _ name__ = ='_ main__': Subject = sys.argv [1] Content = sys.argv [2] # CorpID is enterprise ID Corpid = "ww5cfabaf35ce8cd7b" # Secret Secret = "uiwvmNj8f1IVy3QYrZ62WePGFKA_BsIPmHigq3TRydM" # address book department ID PartyID = "1" # Application ID Agentid = "1000002" Token = GetToken (Corpid, Secret) status = SendMessage (Token, Agentid, Subject Content) print (status)

WeCom is encapsulated into an interface and provided to external calls.

Import requestsimport jsonclass Wechat_Info: def _ _ init__ (self): self.partyID ='1' self.corpID = 'enterprise ID' self.secret =' applied secret' self.agentID = '1000002' self.token = None def _ get_token (self, corpid Secret): Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = {" corpid ": corpid," corpsecret ": secret} r = requests.get (url=Url, params=Data) token = r.json () ['access_token'] return token def send_message (self Message): url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format( self.__get_token (self.corpID, self.secret)) data = {" toparty ": self.partyID," msgtype ":" text "," agentid ": self.agentID "text": {"content": message}, "safe": "0"} result = requests.post (url=url Data=json.dumps (data) return result.textif _ _ name__ ='_ _ main__': wechat_info = Wechat_Info () result = wechat_info.send_message ('Wechat test') print (result) about how Python data is pushed to Wechat Enterprise account to share here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report