github地址:https://github.com/AlbertCQY/scripts/tree/master/send_wechat
python3代码参考如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =========================================================================
"""
-- File Name : main.py
-- Purpose : 发微信模块
-- Date : 2020/01
-- Author:陈晴阳
Vervisons:
-- 20200106 1.0,陈晴阳,微信发送图片和文本的方法封装为模块
"""
# =========================================================================
import urllib.request
import json
import sys
import requests
import os
import settingsclass WeChat():def __init__(self,agentid,corpid,secret,party = '1'):gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='+corpid+'&corpsecret='+secretself.token = self.gettoken(gettoken_url)self.post_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.token+"&agentid="+agentidself.img_url = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload'self.party = partyif self.party == '1':self.message = {"touser":"@all", #所有用户#"totag":"1",       #标签ID  1"msgtype":"text","agentid":agentid,  "text":{"content":'NONE'},"safe":0}self.image = {"touser":"@all", #所有用户"msgtype":"image","agentid":agentid,  "image": {"media_id":0},"safe":0}else:self.message = {"toparty":self.party, #指定部门id的用户#"totag":"1",       #标签ID  1"msgtype":"text","agentid":agentid,  "text":{"content":'NONE'},"safe":0}self.image = {"toparty":self.party, #指定部门id的用户"msgtype":"image","agentid":agentid,  "image": {"media_id":0},"safe":0}def gettoken(self,gettoken_url):try:tokenresponse = urllib.request.urlopen(gettoken_url).read().decode()except  urllib.error.URLError as e:print("获取token错误",e)token = json.loads(tokenresponse)['access_token']return tokendef get_media_ID(self,path):payload_img={'access_token':self.token,'type':'image'}try:data ={'media':open(path,'rb')}except Exception as err:print('open file error ',err)try:r=requests.post(url=self.img_url,params=payload_img,files=data)#print 'get imageid  ok'except Exception as err:print('get image id error ',err)dict =r.json()#print "=====",dictreturn dict['media_id']def send_messages(self,content):self.message['text']['content'] = contenttry:message_json = bytes(json.dumps(self.message), 'utf-8') #change to json bytesmsg_response = urllib.request.urlopen(urllib.request.Request(url=self.post_url, data=message_json)).read()x = json.loads(msg_response.decode())['errcode']if x == 0:print('Send_Messages Succesfully')else:print('Send_Messages Failed')except Exception as e:print(e)return msg_responsedef send_images(self,media_id):self.image['image']['media_id'] = media_idtry:main_messages = bytes(json.dumps(self.image),'utf-8')request = urllib.request.Request(self.post_url, main_messages)response = urllib.request.urlopen(request)msg = response.read()except Exception as e:logFile = open('send_wechat_pic.log','a')print(logFile,e)logFile.close()sys.exit()return msgif __name__ == '__main__':# logFile1 = open('send_wechat_txt.log','a')# logFile2 = open('send_wechat_pic.log','a')GRAPH_PATH = "/tools/scripts/pic/"FileNames=os.listdir(GRAPH_PATH)wechat_sender = WeChat(settings.def_agentid,settings.def_corpid,settings.def_secret)#初始化对象try:content='测试'wechat_sender.send_messages(content)#调用方法发送信息 并返回信息except Exception as e1:print(e1)for i in range(len(FileNames)):#调用方法发送图片,并返回异常信息try:pic_path=GRAPH_PATH+FileNames[i]media_id = wechat_sender.get_media_ID(pic_path)wechat_sender.send_images(media_id)except Exception as e2:print(e2)

python3发送微信相关推荐

  1. [python3] 发送微信 同步手机端

    通过企业微信, 发送消息 可以同步至手机端微信 1. 登录企业微信,新建应用 https://work.weixin.qq.com/wework_admin/frame 我的企业 --> 企业I ...

  2. 服务器监控 微信发送,python3.8 微信发送服务器监控报警消息代码实现

    python3.8 微信发送服务器监控报警消息代码实现 发布时间:2020-10-06 08:01:33 来源:脚本之家 阅读:93 作者:炒鸡蛋 这篇文章主要介绍了python3.8 微信发送服务器 ...

  3. 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)...

    不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...

  4. python 发送微信语音消息_全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)...

    不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...

  5. python3+服务器+微信公众号的聊天机器人

    写在最前:假期想稍微自学一下python语言,在课程中实现了聊天机器人,但是那时候是从网上找的源码,这次想要自己一句一句试试编码.为了利于交互,希望基于微信平台进行开发,但是用自己的微信账号害怕发出去 ...

  6. 使用python 定时发送微信信息给喜欢的人

    原创 使用python 定时发送微信信息给喜欢的人 2019-11-08 16:34:18 冒牌技术小哥 阅读数 171 文章标签: python 更多 分类专栏: python 版权声明:本文为博主 ...

  7. 用Python3发微信好友消息、群聊、高逼格的朋友圈【附代码】

    1.发送微信消息 需要安装PIL库 PIL是一个功能非常强大的Python图像处理标准库,但是呢由于PIL支持Python2.7,所以使用Python3的程序员又在PIL的基础上分离出来了一个分支,创 ...

  8. linux 监控微信通知,一个 Linux 下基于 Bash 的文件和数据库监控及备份工具,可发送微信报警通知...

    shellMonitor 一个 Linux 下基于 bash 的文件和数据库监控及备份工具. 写这个工具的原因,在于一个朋友的一个小电商网站,因为未明原因被黑了,总是会被增加超级管理员,并将收款账号改 ...

  9. php+实现群发微信模板消息_php实现发送微信模板消息的方法,php信模板消息_PHP教程...

    php实现发送微信模板消息的方法,php信模板消息 本文实例讲述了php实现发送微信模板消息的方法.分享给大家供大家参考.具体如下: 该方法基于thinkphp实现实现,具体OrderPush.cla ...

最新文章

  1. 【怎样写代码】复杂对象的组装与创建 -- 建造者模式(三):建造者模式
  2. redis和memcache的区别
  3. 优化传输文件的性能- -零拷贝
  4. python 自动输入_Python自动输入【新手必学】
  5. 前端学习(592):使用snippets辅助debugging
  6. 研究生新生大数据!一大波00后“涌入”同济,最小博士生20岁
  7. linux内核io源码,Linux Kernel do_io_submit()函数整数溢出漏洞
  8. 当有多个设备online时,命令行窗口通过adb连接指定设备方法
  9. CoffeeScript中的三元操作
  10. MySQL5.5编译工具configure向cmake过渡指南
  11. 十大经典排序算法与算法复杂度
  12. 微机原理、计算机组成原理与计算机体系结构之间的关系
  13. 微软语音合成助手 v1.5.1 文本转语音工具,真实语音AI生成器
  14. 【CTR预估】 xDeepFM模型
  15. php-Study1
  16. 过去66年的66项最佳发明(1994~2019)
  17. 运营商宽带网速为什么用bit而不用byte
  18. 什么是TTL电平,什么是CMOS电平,他们的区别
  19. js渲染10万数据列表,不阻塞UI
  20. 对AVL树和红黑树的个人理解

热门文章

  1. 电商平台需要怎样的推荐系统?
  2. 入门到精通!珍藏资源!VAE变分自编码器
  3. jquery for循环_前端基础入门五(掌握jQuery的常用api,实现动态效果)
  4. c语言数组插入一个数字 移位,如何将一个数组的元素循环左移?
  5. Leetcode每日一题:381.insert-delete-getrandom-o1-duplicates-allowed(时间插入、删除和获取随机元素-允许重复)
  6. sklearn学习4.数据处理与特征工程
  7. python接口自动化(五)--接口测试用例和接口测试报告模板(详解)
  8. 【十七】文件译文:user.properties (用户配置文件)
  9. 苹果屏幕使用时间怎么设置_苹果手机屏幕不能旋转怎么办
  10. vue中使用lazyload实现图片懒加载