科技

  • 钉钉报警接入代码

    @Service@Slf4jpublic class DingTalkUtil { @Value("${dingTalk.robot.url}") private String robotUrl; @Value("${dingTalk.robot.me}") private String me; // 钉钉密钥 @Value("${dingTalk.robot.secret}") private String secret; @Value("${dingTalk.enabled}") private Boolean enabled; private OkHttpClient okHttpClient; private static final ObjectMapper objectMapper = new ObjectMapper(); private static final MediaType jsonMediaType = MediaType.parse("application/json"); @PostConstruct public void init() { ExecutorService executorService = new ThreadPoolExecutor( 1, 5, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100), ThreadFactoryBuilder.create().setNamePrefix("dingTalk-").build(), new ThreadPoolExecutor.CallerRunsPolicy() ); Dispatcher dispatcher = new Dispatcher(executorService); dispatcher.setMaxRequests(5); dispatcher.setMaxRequestsPerHost(5); okHttpClient = new OkHttpClient.Builder() .readTimeout(Duration.ofSeconds(1)) .connectTimeout(Duration.ofSeconds(1)) .callTimeout(Duration.ofSeconds(1)) .writeTimeout(Duration.ofSeconds(1)) .dispatcher(dispatcher) .build(); } /** * 异步发送钉钉机器人文本消息. */ public void sendTextMessage(String content) { doSendTextMessage(content, textMessage -> { }); } /** * 异步发送文本消息并@自己. */ public void sendTextMessageWithAtMe(String content) { doSendTextMessage(content, textMessage -> textMessage.getAt().getAtMobiles().add(me)); } /** * 异步发送文本消息并@所有人. */ public void sendTextMessageWithAtAll(String content) { doSendTextMessage(content, textMessage -> textMessage.getAt().setAtAll(true)); } private void doSendTextMessage(String content, Consumer<TextMessage> messageConfigurator) { if (!enabled) { return; } if (StringUtils.isBlank(content)) { throw new IllegalArgumentException("文本消息内容不能为空"); } TextMessage textMessage = new TextMessage(); textMessage.setText(new TextMessage.Content(content)); messageConfigurator.accept(textMessage); long timestamp = System.currentTimeMillis(); String sign = sign(timestamp); try { Request request = new Request.Builder() .url((robotUrl + "×tamp=" + timestamp + "&sign=" + sign)) .post(RequestBody.create(objectMapper.writeValueAsString(textMessage), jsonMediaType)) .build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { log.error("发送钉钉消息失败, 请求: {}.", call, e); } @Override public void onResponse(@NotNull Call call, @NotNull Response response) { ResponseBody responseBody = response.body(); log.debug("钉钉发送成功, call: {}, resp: {}.", call.request().body(), responseBody); if (responseBody != null) responseBody.close(); } }); } catch (JsonProcessingException e) { throw ExceptionUtil.wrapRuntime(e); } } private String sign(long timestamp) { final String seed = (timestamp + "\n" + secret); try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); byte[] result = mac.doFinal(seed.getBytes(StandardCharsets.UTF_8)); return URLEncoder.encode(Base64.getEncoder().encodeToString(result), StandardCharsets.UTF_8.displayName()); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw ExceptionUtil.wrapRuntime(e); } } @Getter private static class TextMessage { private final String msgtype = "text"; @Setter private Content text; private final At at = new At(); @Data @AllArgsConstructor private static class Content { private String content; } private static class At { @Setter private boolean isAtAll = false; @Getter private final List<String> atMobiles = new LinkedList<>(); // 不能删除,否则会导致生成的json字段名是atAll, 导致@所有人不生效 public boolean getIsAtAll() { return isAtAll; } } }}

    科技 2021年11月18日
  • Excel函数大全——连接运算符

    今天说的是Excel中的连接运算符,连接运算符就只有一个为:&。要说明的是:

    科技 2021年11月18日
  • 蜜雪冰城拟上市,经营范围新增第二类增值电信业务

    近日,蜜雪冰城股份有限公司(下简称:蜜雪冰城)拟在A股市场上市,目前接受辅导备案。这意味着,蜜雪冰城有望成为继奈雪的茶之后,国内的茶饮“第二股”。

    科技 2021年11月18日
  • 做电商必备的55个网站请参考

    全网最全拿货网站!做电商必备!1.衣联网(www.eelly.com

    科技 2021年11月18日
  • 全球数治|企业数字化转型的四个层次及关键挑战

    澎湃新闻记者 吕娜 编译

    科技 2021年11月18日
  • 时尚设计带来不俗音质,哈氪零度真无线蓝牙耳机要做行业引领者

    蓝牙耳机已然成了很多人的必备物件,然而蓝牙耳机非黑即白的设计难免会让人产生视觉疲劳。最近笔者就体验了一款来自HAKII 旗下的一款HAKII ICE哈氪零度真无线蓝牙耳机,无论是颜值还是在音质续航等方面都带来了不错的体验。

    科技 2021年11月18日
  • 吴涛:互联网产品研发流程概论(下)

    本文由36氪企服点评专家团吴涛原创。

    科技 2021年11月18日
  • 三星Galaxy A33 5G机身框架泄露在网络上

    三星Galaxy A33 5G将于2022年初推出,支持5G,发售时提供四种颜色。我们已经看到了该设备的媒体渲染图,并了解到它将携带一颗5000mAh的电池。这些泄露的手机边框图片让人眼前一亮,这有助于直观了解其控制按钮的位置。

    科技 2021年11月18日
  • 中国四大财团

    中国的四大财团,你们知道都是谁吗?连阿里巴巴和腾讯都得靠边。

    科技 2021年11月18日
  • 风电+储能+换电概念+新能源车,4天上涨20.99%!还有机会吗?

    一、热门个股解读——协鑫能科(002015)

    科技 2021年11月18日