Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of using nailing robot to push messages to nail groups based on java

Shulou Source: shulou.com Published: 2022-06-03 09:20:51 11月01日 Update

Editor to share with you how to use the nail robot to push messages to nail groups based on java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

The first step is to log in to the nailing computer version and get the webhook of the nailing robot.

The second step is to use java to send a post request to the nail to complete the message push

Package com.thinkgem.wlw.modules.lhjh.DingTalk;import com.alibaba.fastjson.JSON;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import java.net.URL;import java.net.URLConnection;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;/** * @ Author: zhouhe * @ Date: 14:49 * / public class SendHttps {private static Logger logger = LoggerFactory.getLogger (SendHttps.class) on 2019-6-20 / * send POST request with parameters of Map, contentType=x-www-form-urlencoded * * @ param url * @ param mapParam * @ return * / public static String sendPostByMap (String url, Map mapParam) {Map headParam = new HashMap (); headParam.put ("Content-type", "application/json;charset=UTF-8"); return sendPost (url, mapParam, headParam) } / * send the request for POST method to the specified URL * * @ param url send the URL * @ param param request parameter of the request, the response result of the remote resource represented by * @ return * / public static String sendPost (String url, Map param, Map headParam) {PrintWriter out = null; BufferedReader in = null; String result = ""; try {URL realUrl = new URL (url); / / Open the connection to URL URLConnection conn = realUrl.openConnection () / / set the general request attribute request header conn.setRequestProperty ("accept", "* / *"); conn.setRequestProperty ("connection", "Keep-Alive"); conn.setRequestProperty ("user-agent", "Fiddler"); if (headParam! = null) {for (Entry entry: headParam.entrySet ()) {conn.setRequestProperty (entry.getKey (), entry.getValue ());}} / / the following two lines of conn.setDoOutput (true) must be set to send POST request Conn.setDoInput (true); / / get the output stream corresponding to the URLConnection object out = new PrintWriter (conn.getOutputStream ()); / / send request parameter out.print (JSON.toJSONString (param)); / / buffer out.flush () of the flush output stream; / / define the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (conn.getInputStream (); String line; while ((line = in.readLine ())! = null) {result + = line }} catch (Exception e) {logger.info ("an exception occurred sending a POST request!" + e); e.printStackTrace ();} / / use finally blocks to close the output stream and input stream finally {try {if (out! = null) {out.close ();} if (in! = null) {in.close ();}} catch (IOException ex) {ex.printStackTrace ();}} return result;}}

Step 3, write the test class

Package com.thinkgem.wlw.modules.lhjh.DingTalk;import java.util.HashMap;import java.util.Map;/** * @ Author: zhouhe * @ Date: 14:52 * / 2019-6-20 * / public class SendMessage {public static void main (String [] args) {/ / webhook String dingDingToken= "https://oapi.dingtalk.com/robot/send?access_token=0f0daca33m98gn78f00189fe1e1e908b81fa26d0d8ddd48fa78a844cd8636187"; / / requested JSON data. Here I use map to convert json format Map json=new HashMap () in the tool class. Map text=new HashMap (); json.put ("msgtype", "text"); text.put ("content", "Linxi Coking: VOCs emission concentration greater than the upper limit: 61.89"); json.put ("text", text); / / send post request String response = SendHttps.sendPostByMap (dingDingToken, json); System.out.println ("corresponding result:" + response);}}

The above is all the contents of the article "how to use the nailing robot to push messages to the nailing group based on java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Machine robot message push parameter article output function content result input upper limit not much between representative most objects properties tools data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Linux Xiaomi Redmi