python3微信企业号 发送图文

简介:
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' )}
     =  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,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
安全 开发者 Python
用Python找出了删除自己微信的所有人并将他们自动化删除了
用Python找出了删除自己微信的所有人并将他们自动化删除了
|
7月前
|
JSON Java 测试技术
Python + Appium 自动化操作微信入门
Python + Appium 自动化操作微信入门
139 0
|
1月前
|
数据采集 测试技术 API
python爬虫之app爬取-微信朋友圈
搭建appium环境,appium基本使用,API操作等等
77 0
|
1月前
|
数据采集 存储 关系型数据库
Python爬虫-使用代理获取微信公众号文章
使用代理爬取微信公众号文章
52 0
|
1月前
|
监控 安全 API
怎么用Python找回微信撤回信息
怎么用Python找回微信撤回信息
33 0
|
3月前
|
小程序 关系型数据库 MySQL
基于Python和mysql开发的看图猜成语微信小程序(源码+数据库+程序配置说明书+程序使用说明书)
基于Python和mysql开发的看图猜成语微信小程序(源码+数据库+程序配置说明书+程序使用说明书)
|
3月前
|
小程序 关系型数据库 MySQL
基于Python和mysql开发的今天吃什么微信小程序(源码+数据库+程序配置说明书+程序使用说明书)
基于Python和mysql开发的今天吃什么微信小程序(源码+数据库+程序配置说明书+程序使用说明书)
|
4月前
|
机器人 API 持续交付
不懂浪漫?用30行Python代码实现自动给心上人发送微信消息
不懂浪漫?用30行Python代码实现自动给心上人发送微信消息
|
5月前
|
运维 监控 数据处理
使用Python开发员工微信监管软件的基础框架
在企业管理中,员工微信使用的监管成为一项重要的任务。为了实现高效的监管,我们可以利用Python语言开发一套基础框架,用于员工微信监管软件的开发。本文将介绍这个基础框架,并提供一些代码示例,以帮助读者理解如何构建这样的监管系统。
188 0
|
6月前
|
存储 程序员 Python
用Python实现微信多开,1行代码免费用
用Python实现微信多开,1行代码免费用
387 2
用Python实现微信多开,1行代码免费用

热门文章

最新文章