Examples of how Python implements WeChat Enterprise text message push functionality

Source: Internet
Author: User
This article mainly introduced the Python programming implementation Enterprise number text message push function, combined with instance form analysis Python Enterprise number text message push interface call related operation skill, need friend can refer to the next

This example describes the Python enterprise number text message push feature. Share to everyone for your reference, as follows:

Enterprise number creation, enterprise number application creation, group, tag, part does not repeat, a search a lot of, but the online take of those scripts are not good, so I repaired a

Frankly speaking, this script is used as a ZABBIX notification medium script, I am a rookie, if not, the gods do not joke, Python is also in the learning stage, if there is not reasonable, I would like to give advice, nonsense not much to say, script to serve:


#!/usr/bin/python# _*_coding:utf-8 _*_import urllib2import jsonimport sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') def gettoken (Corpid, corpsecret): Gettoken_url = ' https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= ' + corpid + ' & Amp;corpsecret= ' + corpsecret try:token_file = Urllib2.urlopen (gettoken_url) except URLLIB2. Httperror as E:print e.code print E.read (). Decode ("UTF8") sys.exit () Token_data = Token_file.read (). Decode (' UTF -8 ') Token_json = Json.loads (token_data) Token_json.keys () token = token_json[' Access_token '] return tokendef senddata (Access_token, user, party, agent, subject, content): Send_url = ' https://qyapi.weixin.qq.com/cgi-bin/message/send? Access_token= ' + access_token send_values = "{\" touser\ ": \" "+ user +" \ ", \" toparty\ ": \" "+ Party +" \ ", \" totag\ ": \" \ ", \" Msgtype\ ": \" text\ ", \" agentid\ ": \" "+ Agent +" \ ", \" text\ ": {\" content\ ": \" "+ subject +" \ n "+ content +" \ "},\" safe\ ": \" 0\ "}" Send_request = Urllib2. Request (Send_url, send_values) respoNSE = Json.loads (Urllib2.urlopen (send_request). read ()) print str (response) If __name__ = = ' __main__ ': User = str (sys.argv [1]) # parameter 1: Send to the user's account, must pay attention to the enterprise number, and the enterprise number has the message Rights Party = STR (sys.argv[2]) # parameter 2: sent to the group ID number, must have permission to the enterprise number Agent = str (sys.argv[3]) # parameter 3 : App ID in enterprise number subject = str (sys.argv[4]) # parameter 4: Title "Part of message contents" CONTENT = str (sys.argv[5]) # parameter 5: text-specific content corpid = ' corpid ' # Co Rpid is the identity of the enterprise number Corpsecret = ' Corpsecretsecret ' # Corpsecretsecret is the Administrative group credential key Try:accesstoken = GetToken (Corpid, Corpsecret ) SendData (accesstoken, user, party, agent, subject, content) except Exception, E:print str (e) + "Error please Che CK \ "corpid\" or \ "Corpsecret\" Config "
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.