Python3调用微信企业号API发送文本消息代码示例
作者:深海不寂莫 时间:2023-05-03 16:23:41
本文主要向大家分享了Python3调用微信企业号API发送文本消息示例的有关代码,具体如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import json
import sys
import logging
touser = '@all'
agentid = 0
corpid = 'wx5aef2da956514535'
corpsecret = 'Co17m_OPlvE8Q4P2RKKwtq5oIA3p42xGUZEvCHBI8S0'
url = 'https://qyapi.weixin.qq.com'
subject = sys.argv[2]
message = sys.argv[3]
logging.basicConfig(level=logging.DEBUG, filename='E:\Python_project\Scripts\my.log',
format='%(asctime)s - %(levelname)s: %(message)s')
class Weixin:
def __init__(self, url, corpid, corpsecret):
token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret)
self.token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token']
def send_message(self, url, data):
send_url = '%s/cgi-bin/message/send?access_token=%s' % (url, self.token)
self.respone = urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read()
x = json.loads(self.respone.decode())['errcode']
if x == 0:
logging.debug('Successfully %s %s' % (subject, message))
return 'Succesfully'
else:
logging.debug('Failed %s %s' % (subject, message))
return 'Failed'
def messages(self, subject, message):
values = {
"touser": touser,
"msgtype": 'text',
"agentid": agentid,
"text": {'content': subject + message},
"safe": 0
}
return self.send_message(url, bytes(json.dumps(values), 'utf-8'))
if __name__ == '__main__':
obj = Weixin(url, corpid, corpsecret)
ret = obj.messages(subject, message)
来源:http://www.cnblogs.com/postgres/p/6942812.html
标签:python,微信企业号
0
投稿
猜你喜欢
解决Mysql的left join无效及使用的注意事项说明
2024-01-16 00:18:09
Golang实现带优先级的select
2024-04-26 17:36:41
display:inline问题小结
2008-05-01 13:08:00
解析ROC曲线绘制(python+sklearn+多分类)
2021-04-06 12:16:16
Golang命令行进行debug调试操作
2024-04-27 15:37:33
一小时学会TensorFlow2之全连接层
2022-11-27 10:35:30
OpenCV图像分割之分水岭算法与图像金字塔算法详解
2022-11-27 07:59:24
以大热剧《觉醒年代》为例用Python绘制可视化仪表盘
2022-03-23 15:27:07
golang:json 反序列化的[]和nil操作
2024-04-26 17:19:24
Python变量和数据类型和数据类型的转换
2023-09-28 07:44:34
如何控制Go编码JSON数据时的行为(问题及解决方案)
2024-05-22 17:47:47
MySQL分区表实现按月份归类
2024-01-17 12:56:36
怎样才能解决MySQL 5.0.16的乱码问题
2008-12-03 15:58:00
Python利用redis-py实现哈希数据类型的常用指令操作
2021-09-24 18:16:45
简单了解Python变量作用域正确使用方法
2022-02-03 04:18:55
用javascript实现gb2312转utf-8的脚本
2024-04-16 08:58:42
python中and和or逻辑运算符的用法示例
2022-10-13 03:31:18
python安装库的最详细方法(以安装pygame库为例)
2021-03-08 04:29:40
SQL Server使用row_number分页的实现方法
2024-01-28 21:02:38
学生如何注册Pycharm专业版以及pycharm的安装
2023-01-20 15:43:40