Bootstrap

超好用的Server酱,推送微信消息

 用python封装了Server酱的请求的api和日志查看的api

代码即粘即用,需要修改点:把Server酱的key换成自己的

import requests


class weChat(object):
    def __init__(self):
        '''
        : _key 需要去server酱进行申请,https://sct.ftqq.com/
        '''
        self._key = 'XXXXXXXXXXXXXX'
        self._pushid = None
        self._readkey = None
        self.send_log = None  # 用来存放日志信息

    def sed_wechat(self, title, desp=None):
        '''
        : title: weChat推动卡片的title
        : desp: 点击卡片进入详情的数据
        return 发送成功返回True False
        '''
        if desp is None:
            desp = "无详细信息"
        data = {
            "text": title,
            "desp": desp,

        }
        req_sed = self.request(method='POST', url=f'https://sctapi.ftqq.com/{self._key}.send', data=data).json()
        print(req_sed)
        if req_sed['code'] == 0:
            self._pushid = req_sed['data'].get("pushid")
            self._readkey = req_sed['data'].get("readkey")
            self._get_send_log()
            return True
        else:
            return False

    def _get_send_log(self):
        """
        获取日志信息,写入send_log
        """
        if self._readkey is None and self._pushid is None:
            return
        url = f"https://sctapi.ftqq.com/push?id={self._pushid}&readkey={self._readkey}"
        try:
            self.send_log = self.request(url=url).json()
        except Exception as e:
            self.send_log = e

    def request(self, url, method="GET", data=None):
        """
        封装requests请求方法
        """
        try:
            if data:
                return requests.request(method=method, url=url, data=data)
            else:
                return requests.request(method=method, url=url, params=data)
        except Exception as e:
            return False


wc = weChat()
sd = wc.sed_wechat(title="第SAN次测试")
print(sd)

lg = wc.send_log
print(lg)

;