ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

利用 Vercel 搭建企业微信消息推送 API

2022-06-03 21:01:13  阅读:177  来源: 互联网

标签:微信 content token API msgtype datas touser errmsg Vercel


Vercel 是一个用于前端框架和静态站点的平台,可以用来部署静态网站和 Serverless 函数,功能强大,使用方便。

企业微信提供一系列的 API 接口,可以通过企业微信应用或群机器人发送消息,而且企业微信接入了 MiPush,在 MIUI 系统上可以无后台接收消息。

但是发送企业微信应用消息比较麻烦,要先通过corpidcorpsecret获取access_token,然后通过 POST 请求发送消息。所以我利用 Vercel 搭建了一个 Serverless 函数,只需要一个token就可以发送消息,非常方便。

接口说明

请求地址为https://api.meancoder.xyz/push,参数如下:

参数 必须 说明
token 在环境变量中设置的TOKEN,用于验证身份
content 消息内容
msgtype textmarkdown,默认为text
touser 指定接收消息的成员,默认为Mean

代码

# -*- coding: utf8 -*-
import requests
import json
import os
import urllib.parse
from http.server import BaseHTTPRequestHandler


def push(content, touser, msgtype):
    params = {
        'corpid': os.environ['CORPID'],
        'corpsecret': os.environ['CORPSECRET']
    }
    access_token = json.loads(requests.get(
        'https://qyapi.weixin.qq.com/cgi-bin/gettoken', params=params).text)['access_token']
    push_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+access_token
    data = {
        'msgtype': msgtype,
        'agentid': os.environ['AGENTID'],
        msgtype: {
            'content': content
        },
        'touser': touser
    }
    response = requests.post(push_url, json=data)
    return response.text


class handler(BaseHTTPRequestHandler):

    def do_GET(self):
        errcode = 0
        errmsg = "ok"

        content = ""
        touser = "Mean"
        msgtype = "text"

        datas = urllib.parse.parse_qs(self.path.split("?")[1])

        if "token" not in datas:
            errcode = -1
            errmsg = "no token"
        elif datas["token"][0] != os.environ["TOKEN"]:
            errcode = -2
            errmsg = "invalid token"

        if "content" in datas:
            content = datas["content"][0]
        else:
            errcode = -3
            errmsg = "no content"

        if "touser" in datas:
            touser = datas["touser"][0]

        if "msgtype" in datas:
            if datas["msgtype"][0] not in ["text", "markdown"]:
                errcode = -4
                errmsg = "invalid token"
            else:
                msgtype = datas["msgtype"][0]

        if errcode == 0:
            errmsg = push(content, touser, msgtype)
        else:
            errmsg = json.dumps({"errcode": errcode, "errmsg": errmsg})

        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.end_headers()
        self.wfile.write(errmsg.encode())
        return

重定向

由于 api 要放在/api路径下,默认是通过https://api.meancoder.xyz/api/push发送请求。如果在根目录配置vercel.json文件,就可以把/api重定向到根目录,就能直接通过https://api.meancoder.xyz/push发送请求了。

{
  "rewrites": [
    {
      "source": "/:path*",
      "destination": "/api/:path*"
    }
  ]
}

标签:微信,content,token,API,msgtype,datas,touser,errmsg,Vercel
来源: https://www.cnblogs.com/meancoder/p/wecom-push-api.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有