python向企业微信发送文字和图片消息的示例
(编辑:jimmy 日期: 2024/11/17 浏览:3 次 )
吐槽一下企业微信的api文档真的不好读······
企业微信本来是有功能,可以直接把图片显示到正文的,但是那个api我调用一直出错,各种折腾也没解决。后来感觉就算了,没必要,用url拼接在文字中也行,这样的好处就是以后可以换图床,不局限在企业微信内部(好像免费版一天之内可以上传200张图片,所以如果需求比较大的话可以采用第三方图床)
我把上传部分封装了一个使用新线程的方法,以防使用的时候因为上传卡住主线程(亲测如果不这样的话确实会卡住)
import requests import json from threading import Thread import cv2 import os class Wechat_Info(): def __init__(self): self.partyID = '' self.corpID = '' self.secret = '' self.agentID = '' self.token = None self.pic = None def __get_token(self, corpid, secret): Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = { "corpid": corpid, "corpsecret": secret } r = requests.get(url=Url, params=Data) token = r.json()['access_token'] return token def send_message(self, message): url = "https://qyapi.weixin.qq.com/cgi-bin/message/send".format( self.__get_token(self.corpID, self.secret)) data = { "toparty": self.partyID, "msgtype": "text", "agentid": self.agentID, "text": { "content": message }, "safe": "0" } result = requests.post(url=url, data=json.dumps(data)) return result.text def get_media_url(self, path): ##上传到图片素材 图片url Gtoken = self.__get_token(self.corpID, self.secret) img_url = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg".format(Gtoken) files = {'media': open(path, 'rb')} r = requests.post(img_url, files=files) re = json.loads(r.text) # print("media_id: " + re['media_id']) return re['url'] def send_pic(self, pic_path): img_url = self.get_media_url(pic_path) url = "https://qyapi.weixin.qq.com/cgi-bin/message/send".format( self.__get_token(self.corpID, self.secret)) data = { "toparty": self.partyID, "msgtype": "text", "agentid": self.agentID, "text": { "content": img_url }, "safe": "0" } result = requests.post(url=url, data=json.dumps(data)) return result.text def send_violence_warning(self, pic_path): img_url = self.get_media_url(pic_path) url = "https://qyapi.weixin.qq.com/cgi-bin/message/send".format( self.__get_token(self.corpID, self.secret)) data = { "toparty": self.partyID, "msgtype": "text", "agentid": self.agentID, "text": { "content": '【管理员注意】,有校园暴力行为发生,请及时处理!监控图片' + img_url }, "safe": "0" } result = requests.post(url=url, data=json.dumps(data)) return result.text def send_violence_warning_read_path_from_class(self): pic_path = 'temp.jpg' cv2.imwrite(pic_path, self.pic) self.send_violence_warning(pic_path) os.remove(pic_path) def send_violence_warning_in_new_thread(self, pic): self.pic = pic thread = Thread(target=self.send_violence_warning_read_path_from_class) thread.start() if __name__ == '__main__': wechat_info = Wechat_Info() # result = wechat_info.send_message('微信测试_2020') # result = wechat_info.send_pic('C:\\Users\\JiaDing\\Desktop\\pic.jpg') result = wechat_info.send_violence_warning('C:\\Users\\JiaDing\\Desktop\\pic.jpg') print(result)
在主线程里面调用那个send_violence_warning_in_new_thread,把opencv的图片传进来就行
效果:
虽然是url,但是效果其实也挺好的,点一下就能显示了,即使是第三方图床也没有提示什么的,操作速度也很快,没有其他幺蛾子
以上就是python向企业微信发送文字和图片消息的示例的详细内容,更多关于python发送文字和图片的资料请关注其它相关文章!
下一篇:浅析python 通⽤爬⾍和聚焦爬⾍