企業微信聊天機器人demo

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.test.utils.LogUtil;

/**  
 * <p>Title:QyWxMsgUtil</p>  
 * <p>Description: 企業微信機器人工具類</p> 
 * 
 * @author admin
 * @date 2019年10月8日  
 */
public class QyWxMsgUtil {
    private static final LogUtil logger = LogUtil.getLogUtil(QyWxMsgUtil.class);
    
    public static void main(String[] args) {
        String webHookUrl="聊天機器人的地址";
        Integer titleLevel=0;
        String title="這是標題啊";
        String content="這是內容";
        sendQyWxTextMsg(webHookUrl, titleLevel, title, content);
    }

    
    /**  
     *
     * <p>Title: sendQyWxTextMsg</p>  
     *
     * <p>Description: 發送企業微信機器人文本內容</p>  
     *
     * @param webHookUrl
     * @param titleLevel 標題顏色:默認爲green;1爲yellow;2爲red;
     * @param title
     * @param content
     * @return  
     *
     */
    public static boolean sendQyWxTextMsg(String webHookUrl,Integer titleLevel,String title,String content ){
        boolean result=false;
        String requestData = getRequestData(titleLevel, title, content);
        String resp = post(webHookUrl, requestData);
        logger.info("發送企業微信:返回結果:{0}", resp);
        if (resp!=null && resp.contains("ok")) {
            result=true;
        }
        return result;
        
    }

    /**  
     *
     * <p>Title: getRequestData</p>  
     *
     * <p>Description: 包裝請求參數</p>  
     *
     * @param titleLevel
     * @param title
     * @param content
     * @return  
     *
     */
    public static String getRequestData(Integer titleLevel, String title, String content) {
        StringBuilder bd=new StringBuilder();
        String titleLevelStr="info";//green
        if (titleLevel!=null) {
            if (titleLevel==1) {
                titleLevelStr="comment";//yellow
            }else if (titleLevel==2) {
                titleLevelStr="warning";//red
            }
        }
        bd.append("{\r\n"); 
        bd.append("\"msgtype\": \"markdown\",\r\n"); 
        bd.append("\"markdown\": {\r\n");
        bd.append("\"content\": \"<font color='");
        bd.append(titleLevelStr);
        bd.append("'>");
        bd.append(title);
        bd.append("</font>\r\n"); 
        bd.append(content);
        bd.append("\"}\r\n");
        bd.append("}");
        return bd.toString();
    }
    
    /**  
     *
     * <p>Title: post</p>  
     *
     * <p>Description: 發送post請求</p>  
     *
     * @param url
     * @param content
     * @return  
     *
     */
    public static String post(String url, String content) {
        String str = null;
        CloseableHttpClient hc = HttpClients.createDefault();
        try{
            HttpPost req = new HttpPost(url);
            StringEntity entity = new StringEntity(content, "UTF-8");
            req.setEntity(entity);
            CloseableHttpResponse rep = hc.execute(req);
            try{
                str = EntityUtils.toString(rep.getEntity(), "UTF-8");
            }finally{
                rep.close();
            }
        }catch(Exception e){
            logger.error(e, "http post error : url={0},content={1}", url, content);
        }finally{
            try {
                hc.close();
            } catch (IOException e) {
                logger.error(e, "http client close error : url={0},content={1}", url, content);
            }
        }
        return str;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章