工作通知消息是以某个微应用的名义推送到员工的工作通知消息,例如生日祝福、入职提醒等。
发送工作通知消息需要注意以下事项:
同一个应用相同消息的内容同一个用户一天只能接收一次。
同一个应用给同一个用户发送消息,企业内部应用一天不得超过500次。
通过设置to_all_user参数全员推送消息,一天最多3次。
超出以上限制次数后,接口返回成功,但用户无法接收到。详细的限制说明,请参考工作通知消息限制。
该接口是异步发送消息,接口返回成功并不表示用户一定会收到消息,需要通过获取工作通知消息的发送结果接口查询是否给用户发送成功。
消息类型和样例可参考消息类型与数据格式。
- 先获取token
- 再根据token和用户的手机号获取钉钉的userid
- 根据token和用户的userid发送消息
具体代码如下
代码中的*****要自己填
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import com.alibaba.fastjson.JSONObject;
public class SendMsg {
private static String TOKEN ="";
private String errcode="";
private String userid="";
private final String appkey="*****";
private final String appsecret="*****";
private final String agent_id="*****";
private final String to_all_user="false";
public void SendMessage(String phone,String msg){ getUserid(phone);
if(!"0".equals(errcode)){ errcode =""; getToken(); getUserid(phone); }
send(userid,msg); if(!"0".equals(errcode)) { getToken(); errcode =""; send(userid, msg); } } public void getToken() { String URL_1 = "https://oapi.dingtalk.com/gettoken?appkey="; String URL_2 ="&appsecret="; String URL = URL_1.concat(appkey).concat(URL_2).concat(appsecret); try { URL url1 = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url1.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json"); connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } reader.close(); connection.disconnect(); String obj= String.valueOf(sb); JSONObject jsonObject = JSONObject.parseObject(obj); TOKEN =jsonObject.getString("access_token"); } catch (IOException e) { e.printStackTrace(); } }
public void getUserid(String phone) { String URL_1 = "https://oapi.dingtalk.com/user/get_by_mobile?access_token="; String URL_2 ="&mobile="; String URL = URL_1.concat(TOKEN).concat(URL_2).concat(phone); try { URL url1 = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url1.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json"); connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } reader.close(); connection.disconnect(); String obj= String.valueOf(sb); JSONObject jsonObject = JSONObject.parseObject(obj); userid =jsonObject.getString("userid"); errcode =jsonObject.getString("errcode"); } catch (IOException e) { e.printStackTrace(); } }
public void send(String userid,String msg) {
String URL_title = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="; String URL=URL_title.concat(TOKEN); try { URL url = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json"); connection.connect();
PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); JSONObject obj = new JSONObject(); JSONObject obj1 = new JSONObject(); JSONObject obj2 = new JSONObject(); obj2.put("content",msg); obj1.put("text",obj2); obj1.put("msgtype","text");
obj.put("msg", obj1); obj.put("to_all_user",to_all_user); obj.put("agent_id",agent_id); obj.put("userid_list",userid);
out.println(obj); out.flush(); out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } reader.close(); connection.disconnect(); String obj11= String.valueOf(sb); JSONObject jsonObject = JSONObject.parseObject(obj11); errcode =jsonObject.getString("errcode"); } catch (IOException e) { e.printStackTrace(); }
} }
|