教你利用python实现企业微信发送消息
作者:微笑吧LP 时间:2023-09-06 11:20:55
一、需要的参数
1、通讯用户:touser 或 通讯组:toparty
2、企业ID:corpid
3、应用ID/密钥:agentId,secret
二、获取通讯用户/组
通讯录 用户的账号或创建组的部门ID
三、获取企业ID
我的企业最下方
四、获取应用ID/密钥
企业微信管理员登录企业微信,
应用管理创建应用
可见范围:发给谁
五、脚本代码
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import requests, sys
class SendWeiXinWork():
def __init__(self):
self.CORP_ID = "xxx" # 企业号的标识
self.SECRET = "xxx" # 管理组凭证密钥
self.AGENT_ID = xxx # 应用ID
self.token = self.get_token()
def get_token(self):
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
data = {
"corpid": self.CORP_ID,
"corpsecret": self.SECRET
}
req = requests.get(url=url, params=data)
res = req.json()
if res['errmsg'] == 'ok':
return res["access_token"]
else:
return res
def send_message(self, to_user, content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
data = {
# "touser": to_user, # 发送个人就填用户账号
"toparty": to_user, # 发送组内成员就填部门ID
"msgtype": "text",
"agentid": self.AGENT_ID,
"text": {"content": content},
"safe": "0"
}
req = requests.post(url=url, json=data)
res = req.json()
if res['errmsg'] == 'ok':
print("send message sucessed")
return "send message sucessed"
else:
return res
if __name__ == '__main__':
SendWeiXinWork = SendWeiXinWork()
SendWeiXinWork.send_message("2", "测试a")
六、效果
来源:https://blog.csdn.net/weixin_43789195/article/details/117129230
标签:python,企业微信,发送消息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python画图中文不显示问题的解决方法
2023-05-30 14:07:09
![](https://img.aspxhome.com/file/2023/4/103694_0s.jpg)
ORACLE 最大连接数的问题
2009-07-23 14:27:00
python基本算法之实现归并排序(Merge sort)
2023-04-06 03:20:07
![](https://img.aspxhome.com/file/2023/7/112037_0s.gif)
python实现的B站直播录制工具
2023-05-29 00:51:48
python 异步async库的使用说明
2022-03-03 20:37:01
Python详细讲解浅拷贝与深拷贝的使用
2023-01-01 03:00:10
![](https://img.aspxhome.com/file/2023/5/81635_0s.png)
MySQL limit分页大偏移量慢的原因及优化方案
2024-01-25 14:28:30
PyQt5实现数据的增删改查功能详解
2021-03-30 06:50:07
![](https://img.aspxhome.com/file/2023/0/130680_0s.gif)
关于Interlij 无法使用中文输入法的解决方法(适用于Interlij全家桶 Linux环境)
2023-07-18 13:46:41
![](https://img.aspxhome.com/file/2023/9/127239_0s.png)
MySQL中将一列以逗号分隔的值行转列的实现
2024-01-20 15:31:23
Python实现简单状态框架的方法
2022-08-20 14:13:44
UCenter 批量添加用户的php代码
2024-05-11 09:25:09
Python参数传递实现过程及原理详解
2021-07-11 20:16:07
如何使用python3获取当前路径及os.path.dirname的使用
2023-07-22 06:29:37
基于bootstrap插件实现autocomplete自动完成表单
2024-05-09 10:39:43
python实现根据给定坐标点生成多边形mask的例子
2022-03-22 14:40:15
不唐突的JavaScript的七条准则[翻译]
2008-12-09 13:33:00
ASP教程:自己写的数据库操作类
2008-11-21 17:29:00
pandas读取Excel批量转换时间戳的实践
2023-04-20 12:47:07
matlab中二维插值函数interp2的使用详解
2023-08-11 00:28:45
![](https://img.aspxhome.com/file/2023/2/110192_0s.jpg)