项目印记

预测未来的最好办法,就是把它创造出来。--艾伦.凯(美)

如何用 Python 发送通知到微信?

使用Python代码发送微信通知,同时在企业微信或关注的微信公众号中出现消息提醒!

import json

import time

import requests

'''

本文件主要实现通过企业微信应用给企业成员发消息

'''

CORP_ID = "xxxx"

SECRET = "xxxx"

class WeChatPub:

    s = requests.session()

    def __init__(self):

        self.token = self.get_token()

    def get_token(self):

        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}"

        rep = self.s.get(url)

        if rep.status_code != 200:

            print("request failed.")

            return

        return json.loads(rep.content)['access_token']

    def send_msg(self, content):

        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token

        header = {

            "Content-Type": "application/json"

        }

        form_data = {

            "touser": "FengXianMei",#接收人

            "toparty": "1",#接收部门

            "totag": " TagID1 | TagID2 ",#通讯录标签id

            "msgtype": "textcard",

            "agentid": 1000002,#应用ID

            "textcard": {

                "title": "债券打新提醒",

                "description": content,

                "url": "URL",

                "btntxt": "更多"

            },

            "safe": 0

        }

        rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)

        if rep.status_code != 200:

            print("request failed.")

            return

        return json.loads(rep.content)

if __name__ == "__main__":

    wechat = WeChatPub()

    timenow = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

    wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">注意!</div><div class=\"highlight\">今日有新债,坚持打新!</div>")

    print('消息已发送!')

问 AI(只依据本文回答)
输入你的问题,AI 只根据当前段落和全篇上下文回答。
本文批注(0)