| | |
| | | import com.smtaiserver.smtaiserver.vo.WechatMessageVO; |
| | | import com.smtservlet.core.SMTRequest; |
| | | import com.smtservlet.util.Json; |
| | | import com.smtservlet.util.SMTJsonWriter; |
| | | import com.smtservlet.util.SMTStatic; |
| | | |
| | | import org.apache.logging.log4j.LogManager; |
| | |
| | | public ModelAndView aiReplyToTheUserASecondTime(String answer, String fromUserName) |
| | | throws Exception { |
| | | String accessToken = getAccessToken(); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("touser", fromUserName); |
| | | jsonObject.put("msgtype", "text"); |
| | | JSONObject jsonObject1 = new JSONObject(); |
| | | jsonObject1.put("content", answer); |
| | | jsonObject.put("text", jsonObject1); |
| | | _logger.info("jsonObject: {}", jsonObject); |
| | | |
| | | SMTJsonWriter jsonWr = new SMTJsonWriter(false); |
| | | jsonWr.addKeyValue("touser", fromUserName); |
| | | jsonWr.addKeyValue("msgtype", "text"); |
| | | jsonWr.beginMap("text"); |
| | | { |
| | | jsonWr.addKeyValue("content", answer); |
| | | } |
| | | jsonWr.endMap(); |
| | | // JSONObject jsonObject = new JSONObject(); |
| | | // jsonObject.put("touser", fromUserName); |
| | | // jsonObject.put("msgtype", "text"); |
| | | // JSONObject jsonObject1 = new JSONObject(); |
| | | // jsonObject1.put("content", answer); |
| | | // jsonObject.put("text", jsonObject1); |
| | | // _logger.info("jsonObject: {}", jsonObject); |
| | | String url = |
| | | String.format( |
| | | "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", accessToken); |
| | | Map<String, Object> stringObjectMap = jsonObjectToMap(jsonObject); |
| | | String s = sendPost(url, stringObjectMap); |
| | | //Map<String, Object> stringObjectMap = jsonObjectToMap(jsonWr.getRootJson()); |
| | | String s = sendPost(url, jsonWr.getRootJson()); |
| | | _logger.info("上传结果: {}", s); |
| | | return null; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) throws JSONException { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | Iterator<String> keys = jsonObject.keys(); |
| | | // public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) throws JSONException { |
| | | // Map<String, Object> map = new HashMap<>(); |
| | | // Iterator<String> keys = jsonObject.keys(); |
| | | // |
| | | // while (keys.hasNext()) { |
| | | // String key = keys.next(); |
| | | // Object value = jsonObject.get(key); |
| | | // |
| | | // // 如果值是 JSONObject,递归转换为 Map |
| | | // if (value instanceof JSONObject) { |
| | | // value = jsonObjectToMap((JSONObject) value); |
| | | // } |
| | | // |
| | | // map.put(key, value); |
| | | // } |
| | | // |
| | | // return map; |
| | | // } |
| | | |
| | | while (keys.hasNext()) { |
| | | String key = keys.next(); |
| | | Object value = jsonObject.get(key); |
| | | |
| | | // 如果值是 JSONObject,递归转换为 Map |
| | | if (value instanceof JSONObject) { |
| | | value = jsonObjectToMap((JSONObject) value); |
| | | } |
| | | |
| | | map.put(key, value); |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 验证签名util |
| | |
| | | return s; |
| | | } |
| | | |
| | | public static String sendPost(String urlString, Map<String, Object> params) throws Exception { |
| | | public static String sendPost(String urlString, Json jsonParam) throws Exception { |
| | | // 将参数转换为 JSON 格式字符串 |
| | | JSONObject jsonParams = new JSONObject(params); |
| | | String payload = jsonParams.toString(); |
| | | // JSONObject jsonParams = new JSONObject(params); |
| | | String payload = jsonParam.toString(); |
| | | // 创建连接 |
| | | URL url = new URL(urlString); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |