TNW-發送模板消息

簡介

TNW: TypeScript(The) + Node.js(Next) + WeChat 微信公衆號開發腳手架,支持 http 模塊擴展、支持任何 Node.js 的服務端框架(Express、NestJS、eggjs 等)vue

概述

微信公衆號模板消息的使用權限、設置所屬行業、規則、申請、添加、刪除以及你們問得比較多的問題能夠參考以前寫的一篇文章,此文章到目前爲止(2019.4.28) 已有 8w+的閱讀量 90+的評論 微信公衆號開發之模板消息git

同時也能夠參考官方模板消息接口文檔 點擊這裏typescript

access_token 是公衆號的全局惟一接口調用憑據,公衆號調用各接口時都需使用 access_token數據庫

獲取 access_token 詳細介紹請參考以前寫的文章express

TNW-獲取公衆號的 access_tokenapi

TNW 中已實現的接口

export class TemplateApi {
    public static sendTemplateUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s'
    private static setIndustryUrl = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=%s';
    private static getIndustryUrl = 'https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=%s';
    private static getTemplateIdUrl = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=%s';
    private static delTemplateUrl = 'https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=%s';
    private static getAllTemplateUrl = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=%s';

    /** * 發送模板消息 * @param tempJson */
    public static async send(tempJson: string) {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.sendTemplateUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpPost(url, tempJson);
    }
    /** * 設置所屬行業 * @param industry_id1 公衆號模板消息所屬行業編號 * @param industry_id2 公衆號模板消息所屬行業編號 */
    public static async setIndustry(industry_id1: string, industry_id2: string) {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.setIndustryUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpPost(url, JSON.stringify({
            "industry_id1": industry_id1,
            "industry_id2": industry_id2
        }));
    }
    /** * 獲取設置的行業信息 */
    public static async getIndustry() {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.getIndustryUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpGet(url);
    }
    /** * 獲取模板列表 * @param templateIdShort 模板庫中模板的編號 */
    public static async getTemplateId(templateIdShort: string) {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.getTemplateIdUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpPost(url, JSON.stringify({
            "template_id_short": templateIdShort
        }));
    }
    /** * 刪除模板 */
    public static async delTemplate() {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.delTemplateUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpGet(url);
    }
    /** * 獲取模板列表 */
    public static async getAllTemplate() {
        let accessToken = await AccessTokenApi.getAccessToken();
        let url = util.format(this.getAllTemplateUrl, (<AccessToken>accessToken).getAccessToken);
        return HttpKit.getHttpDelegate.httpGet(url);
    }
複製代碼

發送模板消息

Express 示例以下:微信

// 發送模板消息
app.get('/sendTemplate', (req: any, res: any) => {
    let templateJson = new TemplateData().New().
        setToUser("ofkJSuGtXgB8n23e-y0kqDjJLXxk").
        setTemplateId("BzC8RvHu1ICOQfO4N7kp6EWz9VAbISJjV2fO5t7MiXE").
        setTemplateUrl("https://gitee.com/javen205/IJPay").
        add("first", "恭喜你購買成功!", "#743A3A").
        add("keyword1", "IJPay 讓支付觸手可及", "#0000FF").
        add("keyword1", "聚合支付公開課", "#0000FF").
        add("keyword2", "免費", "#0000FF").
        add("keyword3", "Javen205", "#0000FF").
        add("keyword4", "2019-03-12 13:14", "#0000FF").
        add("remark", "請點擊詳情直接看課程直播,祝生活愉快", "#008000").
        build();
    console.log("templateJson", templateJson);
    TemplateApi.send(templateJson).then(data => {
        res.send(data);
    });
});
複製代碼

Nest 示例以下:架構

// 發送模板消息
  @Get('/sendTemplate')
  sendTemplate(@Req() req: Request, @Res() res: Response) {
    let templateJson = new TemplateData().New().
      setToUser("ofkJSuGtXgB8n23e-y0kqDjJLXxk").
      setTemplateId("BzC8RvHu1ICOQfO4N7kp6EWz9VAbISJjV2fO5t7MiXE").
      setTemplateUrl("https://gitee.com/javen205/IJPay").
      add("first", "恭喜你購買成功!", "#743A3A").
      add("keyword1", "IJPay 讓支付觸手可及", "#0000FF").
      add("keyword1", "聚合支付公開課", "#0000FF").
      add("keyword2", "免費", "#0000FF").
      add("keyword3", "Javen205", "#0000FF").
      add("keyword4", "2019-03-12 13:14", "#0000FF").
      add("remark", "請點擊詳情直接看課程直播,祝生活愉快", "#008000").
      build();
    console.log("templateJson", templateJson);
    TemplateApi.send(templateJson).then(data => {
      res.send(data);
    });
  };
複製代碼

上面是經過封裝的實體類來構建發送模板消息須要的 JSON 數據,固然你也可從配置文件、數據庫…...中讀取 JSON 數據。app

默認 nest.js 底層也是使用的 express ,固然也能夠替換成其餘框架,nest.js 更多介紹請參考官方文檔。框架

開源推薦

相關文章
相關標籤/搜索