All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wslxm.springbootplus2.starter.robot.api.WechatApi Maven / Gradle / Ivy

package io.github.wslxm.springbootplus2.starter.robot.api;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import io.github.wslxm.springbootplus2.starter.robot.properties.RobotProperties;
import io.github.wslxm.springbootplus2.starter.robot.properties.RobotWechatProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信群机器人
 *
 * @author wangsong
 * @date 2023/05/17
 */
@Slf4j
@Component
public class WechatApi {

    @Autowired
    private RobotProperties robotProperties;

    /**
     * 发送信息
     *
     * @param  contentMap
     *      

* contentMap 如下: * "text": { * "content": "广州今日天气:29度,大部分多云,降雨概率:60%", * "mentioned_list":["wangqing","@all"], * "mentioned_mobile_list":["13800001111","@all"] * } *

*/ public void sendMsg(Map contentMap) { RobotWechatProperties wechatProperties = robotProperties.getWechat(); // 拼接消息体 Map paramMap = new HashMap<>(4); paramMap.put("msgtype", "text"); paramMap.put("text", contentMap); // 请求 HttpRequest.post(wechatProperties.getUrl()) .header("Content-Type", "application/json;charset=utf-8") .body(JSON.toJSONString(paramMap)) .execute().body(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy