准备事项

首先,需要注册一个企业微信的账号,直接用自己微信扫

完整代码

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
import requests


# 获取企业微信app token
def get_access_token():

WW_ID = 'XXXX_0'
WW_APP_SECRET = 'XXXX_1'

url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
data = {'corpid': WW_ID, 'corpsecret': WW_APP_SECRET}

return requests.get(url, params=data).json()


def post_msg():

token = get_access_token()['access_token']
# 固定token
# token = 'XXXX_4'
WW_APP_USERID = 'XXXX_2'
WW_APP_AGENTID = 'XXXX_3'

url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}'
data = {
'touser': WW_APP_USERID,
'agentid': WW_APP_AGENTID,
# msgtype: text, image, voice, video, file, textcard(文本卡片), news(图文消息), mpnews(图文消息), markdown,
'msgtype': 'text',
'text': {
"content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
}
}

return requests.post(url, json=data)


# get_access_token()
post_msg()

XXXX_{0-4} 💨机密信息,请勿泄漏!
获取方式见后文

参数配置

msgtype

1
2
3
'msgtype': 'text',
"text" : {
"content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"

1
2
3
4
'msgtype': 'image',
"image" : {
"media_id" : "MEDIA_ID"
},
1
2
3
4
'msgtype': 'voice',
"voice" : {
"media_id" : "MEDIA_ID"
}
1
2
3
4
5
6
'msgtype': 'video',
"video" : {
"media_id" : "MEDIA_ID",
"title" : "Title",
"description" : "Description"
}

1
2
3
4
'msgtype': 'file',
"file" : {
"media_id" : "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o"
}

1
2
3
4
5
6
7
'msgtype': 'textcard',
"textcard" : {
"title" : "领奖通知",
"description" : "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
"url" : "URL",
"btntxt":"更多"
}

1
2
3
4
5
6
7
8
9
10
11
'msgtype': 'news',
"news" : {
"articles" : [
{
"title" : "中秋节礼品领取",
"description" : "今年中秋节公司有豪礼相送",
"url" : "URL",
"picurl" : "http://images.png"
}
]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'msgtype': 'markdown',
"markdown": {
"content": "您的会议室已经预定,稍后会同步到`邮箱`
>**事项详情**
>事 项:<font color=\"info\">开会</font>
>组织者:@miglioguan
>参与者:@miglioguan、@kunliu、@kisonwang
>
>会议室:<font color=\"info\">广州TIT 1楼 301</font>
>日 期:<font color=\"warning\">2018年5月18日</font>
>时 间:<font color=\"comment\">上午9:00-11:00</font>
>
>请准时参加会议。
>
>如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)"
}

XXXX_{0-4}