Example of how Python implements the text message push function of WeChat Enterprise Account

黄舟
Release: 2017-08-22 13:26:45
Original
2912 people have browsed it

This article mainly introduces Python programming to implement the text message push function of WeChat Enterprise Account. It analyzes the operation skills related to calling the Python WeChat Enterprise Account text message push interface in the form of examples. Friends in need can refer to it

The example in this article describes the text message push function of Python WeChat enterprise account. I share it with you for your reference. The details are as follows:

I won’t go into details about the creation of enterprise accounts, the creation of enterprise account applications, groups, tags, and parts. There are a lot of them after searching, but those scripts found on the Internet Many of them didn't work, so I built one

Frankly speaking, this script is used as a notification media script for zabbix. I am a novice. If something is wrong, please don't laugh at me. Python is still learning. stage, if there is anything unreasonable, I hope you can give me some advice. Without further ado, here is the script:


##

#!/usr/bin/python
# _*_coding:utf-8 _*_
import urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid, corpsecret):
  gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&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 token
def 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]) # 参数1:发送给用户的账号,必须关注企业号,并对企业号有发消息权限
  party = str(sys.argv[2]) # 参数2:发送给组的id号,必须对企业号有权限
  agent = str(sys.argv[3]) # 参数3:企业号中的应用id
  subject = str(sys.argv[4]) # 参数4:标题【消息内容的一部分】
  content = str(sys.argv[5]) # 参数5:文本具体内容
  corpid = 'CorpID' # CorpID是企业号的标识
  corpsecret = 'corpsecretSecret' # corpsecretSecret是管理组凭证密钥
  try:
    accesstoken = gettoken(corpid, corpsecret)
    senddata(accesstoken, user, party, agent, subject, content)
  except Exception, e:
    print str(e) + "Error Please Check \"corpid\" or \"corpsecret\" Config"
Copy after login

The above is the detailed content of Example of how Python implements the text message push function of WeChat Enterprise Account. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template