【Java开发】Java实现调用微信机器人,发送企业微信通知
生活随笔
收集整理的這篇文章主要介紹了
【Java开发】Java实现调用微信机器人,发送企业微信通知
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
企業微信機器人發送消息
- 一、可能需要的依賴
- 二、機器人地址號查看
- 三、效果展示
- 四、具體代碼
一、可能需要的依賴
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.5</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.3</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.3</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.3</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.23</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency>二、機器人地址號查看
新建一個群聊機器人,可以查看機器人的接口地址,不需要密鑰和企業id號
三、效果展示
輸如需要發送的消息
獲取返回值
微信收到消息
四、具體代碼
如果覺得不錯就點個贊吧!!
package TEST3;import org.apache.http.HttpStatus; 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 java.io.IOException; import java.util.Scanner;public class SendMsg2 {public static void main(String[] args) throws IOException {Scanner scan = new Scanner(System.in);System.out.println("請輸入要發送的消息內容:");String content = scan.nextLine();sendText(content);}//消息體//拼接json字符串//可以進行其他的拓展 比如卡片形式、圖片形式等public static String sendText(String content) throws IOException {content = "{\n" +" \"msgtype\": \"text\",\n" +" \"text\": {\n" +" \"content\": \"" + content + "\"\n" +" }\n" +"}";return send(content);}public static String send(String textMsg) throws IOException {CloseableHttpClient httpClient = HttpClients.createDefault();//實例化對象HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=33d69c01-d962-478b-8945-288e00e2abf2");//url地址httpPost.addHeader("Content-Type", "application/json; charset=utf-8");//發送消息的格式;StringEntity se = new StringEntity(textMsg, "utf-8");//編碼轉換httpPost.setEntity(se);CloseableHttpResponse response = httpClient.execute(httpPost);//發送成功接收返回值if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String result = EntityUtils.toString(response.getEntity(), "utf-8");System.out.println("發送微信機器人消息成功 " + result);return result;} else {System.out.println("發送微信機器人消息失敗");}// 關閉httpClient.close();response.close();return "發送微信機器人消息失敗";} }總結
以上是生活随笔為你收集整理的【Java开发】Java实现调用微信机器人,发送企业微信通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在enable了database v
- 下一篇: java 打印请求接口响应时间