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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
本例 只适用于企业号,公众号没试,可自行测试,方法差不多。有事找官网开发文档。 #!/usr/bin/env python # -*- coding: utf-8 -*- import requests
import json
import urllib.request
ID = "xxxxxxxxxxxx" Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
UserID = "hequan2011" ##成员ID列表(消息接收者,多个接收者用'|'分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
PartyID = 17 ##部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
AppID = 0 ##应用ID,默认是 企业小助手 企业应用的id,整型。可在应用的设置页面查看
def get_token(): ##获取TOKEN
gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}" . format ( ID , Secret)
r = requests.get(gurl)
dict_result = (r.json())
return dict_result[ 'access_token' ]
def get_media_ID(path): ##上传到临时素材 图片ID
Gtoken = get_token()
img_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=image" . format (Gtoken)
files = { 'image' : open (path, 'rb' )}
r = requests.post(img_url, files = files)
re = json.loads(r.text)
return re[ 'media_id' ]
## def send_text(text): ##发送文字
post_data = {}
msg_content = {}
msg_content[ 'content' ] = text ## 消息内容,最长不超过2048个字节
post_data[ 'touser' ] = UserID
post_data[ 'toparty' ] = PartyID
post_data[ 'msgtype' ] = 'text'
post_data[ 'agentid' ] = AppID
post_data[ 'text' ] = msg_content
post_data[ 'safe' ] = '0' #表示是否是保密消息,0表示否,1表示是,默认0
Gtoken = get_token()
purl1 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}" . format (Gtoken)
json_post_data = json.dumps(post_data, False , False )
request_post = urllib.request.urlopen(purl,json_post_data.encode(encoding = 'UTF8' ))
return request_post
def send_tu(path): ##发送图片
img_id = get_media_ID(path)
post_data1 = {}
msg_content1 = {}
msg_content1[ 'media_id' ] = img_id
post_data1[ 'touser' ] = UserID
post_data1[ 'toparty' ] = PartyID
post_data1[ 'msgtype' ] = 'image'
post_data1[ 'agentid' ] = AppID
post_data1[ 'image' ] = msg_content1
post_data1[ 'safe' ] = '0'
Gtoken = get_token()
purl2 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}" . format (Gtoken)
json_post_data1 = json.dumps(post_data1, False , False )
request_post = urllib.request.urlopen(purl2,json_post_data1.encode(encoding = 'UTF8' ))
return request_post
send_tu( "1.png" ) ##图片目录
send_text( "123-何全" ) ##文字内容
|
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1906922,如需转载请自行联系原作者