import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MsgTemplateTest
 * @Description:
 * @Author: WangGeng
 * @Date: 2020/9/15 15:37
 * @Version: 1.0
 **/
public class MsgTest {

    private static final String ACCESS_TOKEN = "37_GEl-Cwm-eYMf061QhVyPBGMqmMwucJrqC7BFQa3neeFMjik1xKdZDnNWUSwM_UnKb9xNtTgcZba_X3odvC069CCft2aLRNl8MdojGvYzLytv66v4B8r0RUjO4z08bLwViDfbbwayEJWvC0sHTVKjAFAWYN";

    @Test
    public void template() {
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", "oXtnkwxZGEccnCVitqEcFV6TyNvM");
        jsonObject.put("template_id", "K5MLWBBVBE30bIo1cc04nYoePRWN5wlqlWDU9x5lxc4");
        jsonObject.put("url", "https://www.baidu.com");
        jsonObject.put("topcolor", "#FF0000");

        JSONObject data = new JSONObject();
        JSONObject startTime = new JSONObject();
        startTime.put("value", "2020-09-15 08:00");
        startTime.put("color", "#173177");
        data.put("startTime", startTime);

        JSONObject endTime = new JSONObject();
        endTime.put("value", "2020-09-15 18:00");
        endTime.put("color", "#173177");
        data.put("endTime", endTime);

        JSONObject remarks = new JSONObject();
        remarks.put("value", "这里是相关备注信息");
        remarks.put("color", "#173177");
        data.put("remarks", remarks);

        jsonObject.put("data", data);

        ResponseEntity<JSONObject> result = restTemplate.postForEntity("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}", jsonObject, JSONObject.class, ACCESS_TOKEN);
        System.out.println(result.getStatusCode());
        System.out.println(result.getBody());
    }

    @Test
    public void groupMessage() {
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonObject = new JSONObject();

        JSONArray touser = new JSONArray();
        touser.add("oXtnkwxZGEccnCVitqEcFV6TyNvM");
        touser.add("oXtnkw8l7ZtDyJufeHSCIXWRK84c");
        jsonObject.put("touser", touser);
        jsonObject.put("msgtype", "text");

        JSONObject text = new JSONObject();
        text.put("content", "<h1>您好微信</h1>");
        jsonObject.put("text", text);

        ResponseEntity<JSONObject> result = restTemplate.postForEntity("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={access_token}", jsonObject, JSONObject.class, ACCESS_TOKEN);
        System.out.println(result.getStatusCode());
        System.out.println(result.getBody());
    }

}
