使用企业微信和钉钉机器人代替server 酱[python \ shell] 推送消息到微信和钉钉

发布于 2021-04-29  4.08k 次阅读


server 酱升级turbo, 开始收费,收费也没什么,也白嫖很久了,去turbo 版一看发现推荐走的是企业微信推送,既然都走企业微信了,直接用企业微信api就好了,没什么必要通过server 酱进行推送(server 酱作用在于帮忙获取企业微信token, 此token 两小时过期)。

#1. 注册企业微信,获取有关信息

现在注册企业微信挺简单,有个微信号就行,绑定下微信,验证个手机号就通过了。
企业微信注册地址
注册完进入后台 --> 应用管理-自建-创建应用 --> 进入自建应用获取 AgentId Secret (编辑可见人员) --> 我的企业-企业信息 获取企业ID --> 我的企业-微信插件 微信扫一扫加入企业 --> api 推送消息

企业微信发送应用消息接口文档地址: https://work.weixin.qq.com/api/doc/90000/90135/90236

#2. 使用 shell 推送消息到微信

此处使用 news 消息类型,其他类型查看api 文档,觉得new好看点。


#!/bin/bash
# 使用企业微信api 推送消息到微信
# by mo

# 企业id
CORPID=''
# 应用secret
COPRSECRET=''
# 应用id
AGENTID=
# 推送标题
TITLE='mo'
# 推送详情
DESC='ley'
# 推送url
URL='https://ley.best'
# 推送封面
PICURL='https://ley21.oss-cn-hangzhou.aliyuncs.com/img/U20a9fa560f3341089d8375bee5e53281F.jpg'


# 获得企业微信token, 每个token 有效期7200秒, 需要安装 jq
function get_access_token(){
    _corpid=$1
    _corpsecret=$2
    _token=$(curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${_corpid}&corpsecret=${_corpsecret}" |jq '.access_token' | sed 's/"//g')
    echo $_token
}

TOKEN=$(get_access_token $CORPID $COPRSECRET)

data='{
        "touser": "@all",
        "msgtype": "news",
        "agentid": '${AGENTID}',
        "news": {
            "articles": [
                {
                    "title": "'${TITLE}'",
                    "description": "'${DESC}'",
                    "url": "'${URL}'",
                    "picurl": "'${PICURL}'"
                }
            ]
        },
        "at": {
            "atMobiles": [
                ""
            ],
            "isAtAll": "true"
        }
    }'

# post 请求推送消息
curl -XPOST 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='${TOKEN}'' \
-H 'Content-Type: application/json' \
-d "$data"

#3. 使用python3 推送消息到微信


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json


def postwechat(corpid, corpsecret, url='https://www.coachoutlet.com/'):
    HEADERS = {"Content-Type": "application/json ;charset=utf-8"}
    # 图片地址,自行替换
    p = requests.get('https://ley.best/picapi/').text
    # 获取token
    r = requests.get(f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}').text
    js = json.loads(r)
    token = js['access_token']
    # data 中agentid 按应用实际id更换
    data = {
        "touser": "@all",
        "msgtype": "news",
        "agentid": 1000002,
        "news": {
            "articles": [
                {
                    "title": "title example",
                    "description": "description example",
                    "url": url,
                    "picurl": p
                }
            ]
        },
        "at": {
            "atMobiles": [
                ""
            ],
            "isAtAll": "true"
        }
    }
    String_textMsg = json.dumps(data)
    # 企业微信应用地址
    wechaturl = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}'
    res = requests.post(wechaturl, data=String_textMsg, headers=HEADERS)
    print(res.text)


# 企业id
CORPID = ''
# 应用secret
COPRSECRET = ''
# 点击跳转的url
URL = ''

# 推送
postwechat(CORPID, COPRSECRET, URL)

#4. 使用钉钉机器人推送消息到钉钉

钉钉需要自定义关键词,否则发送不成功,即 title 或 text 内需包含关键词


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json


def postdingding(dingdingurl):
    HEADERS = {"Content-Type": "application/json ;charset=utf-8"}
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": "title example",
            "text": "description example"
        },
        "at": {
            "atMobiles": [
                ""
            ],
            "isAtAll": "true"
        }
    }
    String_textMsg = json.dumps(data)

    res = requests.post(dingdingurl, data=String_textMsg, headers=HEADERS)
    print(res.text)


dingdingurl = ''
postdingding(dingdingurl)