使用腾讯云云函数和微信企业版创建自己的消息提醒服务

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

接着 上一篇 注册企业微信获取接口参数的步骤是一样的,重点说明下腾讯云云函数的相关的操作。黑鸟博客站长是第一次使用还在摸索,据说云函数是有部分免费额度可以花的,比需要拥有自己服务器部署PHP要节省些。

1、在腾讯云主页搜索“云函数”,进入控制台页面。

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》
或者直接打开 https://console.cloud.tencent.com/scf/index

2.在管理控制台左侧“函数服务”,点击“新建”选择“自定义创建”方式。
《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

a、这里使用的是 python3.6 其他没改的不影响本次使用,自己喜好也可以默认。

b、页面下拉填入代码:

# -*- coding: utf8 -*-
import os
import json
import requests
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# 从环境变量获取相关id
CORPID = os.environ['corpid']
AGENTID = os.environ['agentid']
CORPSECRET = os.environ['corpsecret']
# 相关id设置 
# corpid = 'xxx'  # 企业id
# agentid = 'xxx'   # 应用id
# corpsecret = 'xxx' # 企业secret

# 企业微信 api url
MSG_URL = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}'

# text_card template
text_card_dict = {
       "touser" : "@all",
       # "toparty" : "PartyID1 | PartyID2",
       # "totag" : "TagID1 | TagID2",
       "msgtype" : "textcard",
       "agentid" : AGENTID,
       "textcard" : {
                "title" : "领奖通知",
                "description" : "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
                "url" : "URL",
                            "btntxt":"更多"
       },
       "enable_id_trans": 0,
       "enable_duplicate_check": 0,
       "duplicate_check_interval": 1800
    }

#黑鸟博客 guihet.com

def get_access_token():
    url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(CORPID, CORPSECRET)
    resp = requests.get(url=url)
    resp_dict = resp.json()
    access_token = None
    if resp.status_code == 200:
        access_token = resp_dict['access_token']
    return access_token
def main_handler(event, context):
    logger.info("Received event: " + json.dumps(event, indent = 2))
    logger.info("Received context: " + str(context))
    # 解析请求参数
    queryString = event['queryString']
    msg_title = queryString.get('text',None)
    msg_desp = queryString.get('desp', msg_title)
    text_card_dict['textcard']['title'] = msg_title
    text_card_dict['textcard']['description'] = msg_desp
    # logger.info(str(text_card_dict))
    # 获取access_token
    access_token = get_access_token() if msg_title is not None else None
    resp_status = 400
    resp_json = dict()
    if access_token is not None:
        resp = requests.post(url=MSG_URL.format(access_token),json=text_card_dict)
        resp_status = resp.status_code
        resp_json = resp.json()
    return {
        "isBase64Encoded": False,
        "statusCode": resp_status,
        "headers": {'Content-Type': 'application/json'},
        "body": json.dumps(resp_json)
    }

c、页面继续下拉 点开“高级配置”,找到”环境变量”,填入三个参数,就是跟上一篇获取的企业微信里的那三个参数。agenid(应用id)、corpid(企业id)、corpsecret(企业secret)设置为云函数的环境变量。

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

会改的直接写到代码里也是可以的:

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

3、创建触发器

创建“API网关触发器”,其他可以默认不改。

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

复制底下的“访问路径”
《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

这样就完成了,直接访问如下形式的地址就可以触发微信的消息推送。

https://service-************.tencentcs.com/release/helloworld-1618735220?text=标题&desp=消息内容

《使用腾讯云云函数和微信企业版创建自己的消息提醒服务》

当然这也只是做到能用,还需要诸多改进。

回复 chenmo 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

  1. chenmo 说道:

    上一篇的PHP版是可以成功的。

  2. chenmo 说道:

    {"errorCode":-1,"errorMessage":"Traceback (most recent call last):\n File \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n func_handler = get_func_handler(file.rsplit(\".\", 1)[0], func)\n File \"/var/runtime/python3/bootstrap.py\", line 159, in get_func_handler\n mod = imp.load_module(mname, *imp.find_module(mname))\n File \"/var/lang/python3/lib/python3.6/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n File \"/var/lang/python3/lib/python3.6/imp.py\", line 172, in load_source\n module = _load(spec)\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 675, in _load\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 655, in _load_unlocked\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 674, in exec_module\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 781, in get_code\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 741, in source_to_code\n File \"\u003cfrozen importlib._bootstrap\u003e\", line 205, in _call_with_frames_removed\n File \"/var/user/index.py\", line 14\n corpid = 'ww524f' # 企业id\n ^\nIndentationError: unexpected indent","requestId":"4f70fec","statusCode":443}