Welcome everyone

语雀更新推送微信

实战 汪明鑫 1699浏览 0评论

语雀更新后(新增文章、更新文章、新增评论和回复)触发语雀的WebHook,实现消息推送到微信

理论上是可行的

 

填写一个url,就可以把数据推过去

 

直接推送到微信群聊是不可行的,微信没有提供这样直接对外暴露的口子,需要通过一个中间server,恰好有一个方糖server提供这样的服务

http://sc.ftqq.com/?c=code

 

还需要关注方糖公众号

在上面的页面绑定要推送的微信

先测试点对点消息推送

 

现在点击菜单栏发送消息

 

也可以直接在浏览器填URL测

https://sc.ftqq.com/SCU91391T98d6e181aadd26097d34bd9c4817626a5e7daf64673f5.send?text=菜狗

SCU91391T98d6e181aadd26097d34bd9c4817626a5e7daf64673f5 就是页面生成的key

 

然后【方糖】公众号就会收到你发的消息

 

 

现在点对点推送ok

 

再把语雀的更新触发的webHook 的url填上 https://sc.ftqq.com/SCU91391T98d6e181aadd26097d34bd9c4817626a5e7daf64673f5.send

是不是可以呢?

 

 

试了一下,是不可以的

为什么呢?

因为语雀推送的参数和方糖sever需要接受的参数不匹配

方糖server需要接受 text参数、desp参数

 

但语雀推送的参数

DocDetailSerializer

• id - 文档编号
• slug - 文档路径
• title - 标题
• book_id - 仓库编号,就是 repo_id
• book - 仓库信息 <BookSerializer>,就是 repo 信息
• user_id - 用户/团队编号
• user - 用户/团队信息 <UserSerializer>
• format - 描述了正文的格式 [lake , markdown]
• body - 正文 Markdown 源代码
• body_draft - 草稿 Markdown 源代码
• body_html - 转换过后的正文 HTML
• body_lake - 语雀 lake 格式的文档内容
• creator_id - 文档创建人 User Id
• public - 公开级别 [0 - 私密, 1 - 公开]
• status - 状态 [0 - 草稿, 1 - 发布]
• likes_count - 赞数量
• comments_count - 评论数量
• content_updated_at - 文档内容更新时间
• deleted_at - 删除时间,未删除为 null
• created_at - 创建时间
• updated_at - 更新时间

 

我们考虑到中间可以加一层web服务,接受语雀的请求,把参数转换成方糖sever需要的参数,就ok了

 

为此我专门去找语雀的p9大佬做了咨询,哈哈,鸡贼。。。

 

 

周末  永康兄弟主动承担开发工作

@RestController
@RequestMapping("/push")
public class PushController {

    @Autowired
    RestTemplate restTemplate;


    /**
     * 接收语雀推送,并推送微信
     */
    @RequestMapping("/weChat")
    public String  pushText(@RequestBody ParamDto paramDto){
        System.out.println(JSONUtil.objectToJsonString(paramDto));
        RequestDto requestParam =new RequestDto();
        requestParam.setSendkey("17915-529c8c66882169a27b63b26da3ee137e");
        requestParam.setText("语雀通知-测试");
        requestParam.setDesp(paramDto.getData().getBody());
        //创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String jsonString = JSONUtil.objectToJsonString(requestParam);
        HttpEntity<String> entity = new HttpEntity<>(jsonString, headers);
        ResponseEntity responseEntity =restTemplate.getForEntity(ConstantData.URL+"",String.class);
        return responseEntity.getBody().toString();
    }

}

 

经测试,点对点ok,但是一对多推送需要微信服务号,报错,只能暂时阻塞了,可惜

 

 

晚上永康上线了一对一语雀推送

然后我把我在方糖server绑定微信生成的链接(https://sc.ftqq.com/SCU91391T98d6e181aadd26097d34bd9c4817626a5e7daf64673f5.send)发给永康,就可以完成更新消息通过方糖推送给我和永康

既然没办法直接一推多,可以在代码层面遍历URL,进行推送,变向一推多

 

【代码如下】

package com.yocan.push.yuque.app;

import com.yocan.push.yuque.Constant.ConstantData;
import com.yocan.push.yuque.dto.ParamDto;
import com.yocan.push.yuque.dto.RequestDto;
import com.yocan.push.yuque.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author yocan
 */
@RestController
@RequestMapping("/push")
public class PushController {

    @Autowired
    RestTemplate restTemplate;


    /**
     * 接收语雀推送,并推送微信
     * 2020-03-30   21:00 修改为单点推送,由于没有服务号
     *  2020-03-30  22:00 无法一对多推送,可将需要推送的地址维护一个list  done
     */
    @RequestMapping("/weChat")
    public String  pushText(@RequestBody ParamDto paramDto){
        System.out.println(JSONUtil.objectToJsonString(paramDto));
        RequestDto requestParam =new RequestDto();
        requestParam.setText("<语雀通知>"+paramDto.getData().getTitle());
        requestParam.setDesp(paramDto.getData().getBody());
        ResponseEntity<String> stringResponseEntity =null;
        //构造get方法发送消息
        for (String url:ConstantData.URL){
            stringResponseEntity=restTemplate.getForEntity(url+"?text="+requestParam.getText()+"&desp=-"+requestParam.getDesp(),String.class);
        }

        return stringResponseEntity.getBody();
    }

}

 

package com.yocan.push.yuque.Constant;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConstantData {

    public static final List<String> URL = Arrays.asList("https://sc.ftqq.com/SCU40692Tccb6b97f9f07146f0b5ac909ca1bfcf35c3c6e3cd17d2.send","https://sc.ftqq.com/SCU91391T98d6e181aadd26097d34bd9c4817626a5e7daf64673f5.send");


}

 

详见永康github : https://github.com/yocaning/PushFromYq

后面可以陆续把大家绑定微信生成的url加入list

 

Nice!

 

【优化版】 form  永康 & 郭老师

现在需要新加一个推送链接还要手动修改urlList,维护起来比较麻烦

考虑提供一个接口,需要推新信息的人直接调这个接口注册URL,

然后注册后的url被持久化到文件,每次推送前先读文件所有URL,进行逐一推送

 

url注册地址

@GetMapping("/urlRegistered")
    public String urlRegistered(@RequestParam String url) throws IOException {
        if (url.contains("\"")) {
            return "URL错误,请检查重试";
        }
        System.out.println("URL-注册" + url);
        File file = new File("/root/data/url");
        try (Writer writer = new FileWriter(file, true)) {
            // 把内容转换成字符数组
            char[] data = url.toCharArray();
            for (char chars : data) {
                writer.append(chars);
            }
            writer.append("\n");
        } catch (Exception e) {
            e.printStackTrace();
            return "异常,请联系 Yocan";
        }
        return "OK";
    }

 

推送前获取需要推送的url列表

public static List<String> getUrlList() {
        // 构建指定文件
        // 根据文件创建文件的输出流
        File file = new File("/root/data/url");
        try (FileReader reader = new FileReader(file)) {
            // 创建字符数组
            char[] data = new char[10240];
            // 读取内容,放到字符数组里面
            reader.read(data);
            String string = new String(data);
            string = string.trim();
            String[] strings = string.split("\n");

            return Arrays.asList(strings);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

 

 

转载请注明:汪明鑫的个人博客 » 语雀更新推送微信

喜欢 (4)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz