侧边栏壁纸
  • 累计撰写 125 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

【python】使用python封装企业微信群机器人api

DarkAthena
2021-09-16 / 0 评论 / 0 点赞 / 892 阅读 / 3831 字

之前工作上有接触一点python,但没有进行过体系化的学习,只会小修小改,最近花了大概一周的时间,看完了廖雪峰的python教程和w3school的python教程,就尝试写点简单的东西练手,这个就是我写的第一个有实用价值的python程序
https://github.com/Dark-Athena/qyweixinrobot_py

#!/usr/bin/env python
#coding=utf-8
#功能 :封装企业微信群机器人API
#日期 :2021-09-05 
#作者:Dark-Athena
#说明:
""" 
 支持纯文本、markdown、图片、文件、图文
 a = wx.WxRobot('1111-222-333-444-55555') #webhook
 a.sendMessage('文本内容')
 a.sendMarkdown('markdown内容')
 a.sendImage('d:/图片.jpg') #图片文件的绝对路径
 a.sendMedia('d:/文件.jpg') #文件的绝对路径

 a.sendNews('[{
               "title" : "中秋节礼品领取",
               "description" : "今年中秋节公司有豪礼相送",
               "url" : "www.qq.com",
               "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
           }]')
"""
           
import requests
import json
import hashlib
import base64
import os

class WxRobot():
    headers = {"Content-Type": "application/json"}
    req_message = {"errcode": 1,"errmessage": "请求微信企业失败,请检查网络"}
    
    def __init__(self, webhook):
        self.post_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={0}".format(webhook)
        
    def _req(self,data):
        try:
            return requests.post(self.post_url, data=json.dumps(data), headers=self.headers,timeout=10).json()
        except Exception as e:
            return self.req_message
            
    def sendMessage(self, message):
        data = {"msgtype": "text","text": {"content": str(message)}}
        return self._req(data)
    
    def sendMarkdown(self, message):
        data = {"msgtype": "markdown","markdown": {"content": str(message)}}
        return self._req(data)
        
    def sendNews(self, message):
        data = {"msgtype": "news","news": {"articles": list(message)}}
        print (data)
        return self._req(data)  
                 
    def sendImage(self, image_path):
        if os.path.exists(image_path):
            with open(image_path, "br") as f:
                fcont = f.read()
                ls_f = base64.b64encode(fcont)
                fmd5 = hashlib.md5(fcont)
                data = {"msgtype": "image", "image": {"base64": ls_f.decode('utf8'), "md5": fmd5.hexdigest()}}
                return self._req(data) 
        else:
            self.req_message['errmessage']='图片文件不存在'
            return self.req_message
            
    def sendMedia(self, file_path):
        if os.path.exists(file_path):
            upload_url = self.post_url.replace('send', 'upload_media') + '&type=file'
            try:
                media_id = requests.post(upload_url, files=[('media', open(file_path, 'rb'))]).json()['media_id'] 
                print (media_id)
            except Exception as e:
                self.req_message['errmessage']='上传文件失败,请检查网络'
                return self.req_message   
            data = {"msgtype": "file","file": {"media_id": media_id}}
            return self._req(data)
        else:
            self.req_message['errmessage']='文件不存在'
            return self.req_message
        
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
博主关闭了所有页面的评论