Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to realize the message push function of nailing robot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use java to achieve nailing robot message push function, I believe that 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!

First build a group of nails and add robots

At this point, the robot has been added, and then write the code to connect the robot brother.

Import com.alibaba.fastjson.JSON;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import java.util.List;import java.util.Map / * @ author yanghao * @ version DingTalkTest.java, v 2019-03-29 11:36 * / public class DingTalkTest {public static void main (String [] args) {try {/ / address of the nailing robot (webhook with robot) String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=............"; / / whether to notify the owner boolean isAtAll = false / / notify the specific person's mobile phone number list List mobileList = Lists.newArrayList (); / / nailing robot message content String content = "Hello, little brother!" / / Assembly request content String reqStr = buildReqStr (content, isAtAll, mobileList); / / push message (http request) String result = HttpUtil.postJson (dingUrl, reqStr); System.out.println ("result = =" + result);} catch (Exception e) {e.printStackTrace () }} / * Assembly request message * @ param content * @ return * / private static String buildReqStr (String content, boolean isAtAll, List mobileList) {/ / message content Map contentMap = Maps.newHashMap (); contentMap.put ("content", content); / / notifier Map atMap = Maps.newHashMap (); / / 1. Whether to notify the owner of atMap.put ("isAtAll", isAtAll); / / 2. Notify the specific person's list of mobile phone numbers atMap.put ("atMobiles", mobileList); Map reqMap = Maps.newHashMap (); reqMap.put ("msgtype", "text"); reqMap.put ("text", contentMap); reqMap.put ("at", atMap); return JSON.toJSONString (reqMap);}}

The running results are as follows:

Result = {"errmsg": "ok", "errcode": 0}

The nail group displays the message:

Ok, simple message push, this is done!

Let's test again to notify everyone and notify specific people.

Change isAtAll to true

/ / whether to notify the owner boolean isAtAll = true;// notify the specific person's mobile phone number list List mobileList = Lists.newArrayList ()

Add the list of notifier numbers (Note: isAtAll and mobileList cannot be effective at the same time)

/ / whether to notify the owner boolean isAtAll = false;// to notify the specific person's list of mobile phone numbers List mobileList = Lists.newArrayList (); mobileList.add ("182 *")

Let's test the special symbols again.

Newline identifier

/ * * newline identifier * / private static final String NEWLINE = "\ n"; / / nailing robot message content / / String content = "Hello, little brother!" ; StringBuffer sb = new StringBuffer (); sb.append ("Hello, little brother!") .append (NEWLINE) .append ("read a book"); String content = sb.toString ()

Emoji Picture

First get the unicode coding of the emoji image

The code is as follows:

/ * * Apple unicode code * / private static final String APPLE = "\ ud83c\ udf4e"; / / nailing robot message content / / String content = "Hello, little brother!" ; StringBuffer sb = new StringBuffer (); sb.append ("Hello, little brother!") .append (NEWLINE) .append ("read a book") .append (NEWLINE) .append ("eat") .append (APPLE); String content = sb.toString ()

Usually in our project, as some alarm to join, convenient and practical very interesting nailing robot, a lot of practical skills, you can explore a wave in depth!

Updated on 2019-12-05

Many friends leave messages to consult the http request. Here are 2 http request codes.

1. Maven project

Add dependency

Cn.hutool hutool-all 4.0.12

Http request code

Private static final int timeout = 10000; public static String postJson (String url, String reqStr) {String body = null; try {body = HttpRequest.post (url) .body (reqStr) .timeout (timeout). Execute (). Body ();} catch (Exception e) {e.printStackTrace ();} return body;}

two。 Non-maven project

Add jar package httpclient-xxx.jarcommons-logging-xxx.jar

Http request code

Public static String postJson (String url, String body) {/ / create Httpclient object CloseableHttpClient httpClient = createCustomClient (); CloseableHttpResponse response = null; String resultString = null; try {/ / create HttpPost request HttpPost httpPost = new HttpPost (url); httpPost.addHeader ("Content-Type", "application/json"); if (body! = null) {httpPost.setEntity (new StringEntity (body, "utf-8")) } / / execute http request response = httpClient.execute (httpPost); resultString = EntityUtils.toString (response.getEntity (), "utf-8");} catch (Exception e) {e.printStackTrace ();} finally {try {if (response! = null) {response.close ();}} catch (Exception e) {e.printStackTrace ();}} return resultString } public static CloseableHttpClient createCustomClient () {RequestConfig defaultRequestConfig = RequestConfig.custom () .setSocketTimeout (120 * 1000) .setConne ctionRequestTimeout (120 * 1000) .setStaleConnectionCheckEnabled (true) .build (); return HttpClients.custom (). SetDefaultRequestConfig (defaultRequestConfig). Build (); above is all the contents of the article "how to use java to achieve the message push function of nailing robot". 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report