Python如何实现微信企业号文本消息推送功能的示例


本文摘自php中文网,作者黄舟,侵删。

这篇文章主要介绍了Python编程实现微信企业号文本消息推送功能,结合实例形式分析了Python微信企业号文本消息推送接口的调用相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python微信企业号文本消息推送功能。分享给大家供大家参考,具体如下:

企业号的创建、企业号应用的创建、组、tag、part就不赘述了,一搜一大堆,但是网上拿的那些个脚本好多都不好使,所以自己修了一个

坦率的讲,这个脚本是用来作为zabbix的通知媒介脚本的,本人是个菜鸟,如果哪里不对,大神们不要笑话,python也处于学习阶段,如果有哪些地方不合理,很希望可以不吝赐教,废话不多说,脚本奉上:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#!/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"

以上就是Python如何实现微信企业号文本消息推送功能的示例的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中pandas的深入理解(代码示例)

Python卸载模块的方法汇总

Python能做什么项目

Python怎么安装jieba库

Python爬虫实现全国失信被执行人名单查询功能示例

idle和Python区别

Python六种数据类型是什么?

Python可以写病毒吗

Python里d是什么意思

详解Python使用asyncio包处理并发的方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...