钉钉开发系列机器人发送数据

钉钉的每个群都可以建若干个机器人,有默认的比如github,也可以自定义。我们使用自定义,建立自己的机器人,然后得到一串的URL,只要向这个URL进行POST请求后,就能将消息通知到对应的群中。机器人的创建可以参照官方的文档。

发送通知的代码如下

 

private string WEB_HOOK = "https://oapi.dingtalk.com/robot/send?access_token=XXXXX";  
  
       private void buttonTest_Click(object sender, EventArgs e)  
       {  
           try  
           {  
               //text类型
               //string phone1 = "15009280795";
               //string phone2 = "13609197096";
               //string textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"},\"at\": {\"atMobiles\": [\"" + phone1 + "\",\"" + phone2 + "\"]},\"isAtAll\": false}";
               //link类型
                //string text = "坐飞机也能玩手机?昨日21时15分,海航HU7781次航班的乘客尝了“头啖汤”。";
                //string title = "坐飞机玩手机? 海航东航满足你!";
                //string picUrl = "";
                //string messageUrl = "https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_6433821039443213195%22%7D&n_type=0&p_from=1";
                //string textMsg = "{ \"msgtype\": \"link\", \"link\": {\"text\": \"" + text + "\",\"title\":\"" + title + "\",\"picUrl\":\"" + picUrl + "\",\"messageUrl\":\"" + messageUrl + "\"}}";


               string msg = textBox1.Text;  
               String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";  
               string s = Post(WEB_HOOK, textMsg, null);  
               MessageBox.Show(s);  
           }  
           catch (Exception ex)  
           {  
               MessageBox.Show(ex.Message);  
           }  
       }  
 
       #region Post  
       ///   
       /// 以Post方式提交命令  
       ///   
       /// 请求的URL  
       /// 请求的json参数  
       /// 请求头的key-value字典  
       public static String Post(string apiurl, string jsonString, Dictionary headers = null)  
       {  
           WebRequest request = WebRequest.Create(@apiurl);  
           request.Method = "POST";  
           request.ContentType = "application/json";  
           if (headers != null)  
           {  
               foreach (var keyValue in headers)  
               {  
                   if (keyValue.Key == "Content-Type")  
                   {  
                       request.ContentType = keyValue.Value;  
                       continue;  
                   }  
                   request.Headers.Add(keyValue.Key, keyValue.Value);  
               }  
           }  
  
           if (String.IsNullOrEmpty(jsonString))  
           {  
               request.ContentLength = 0;  
           }  
           else  
           {  
               byte[] bs = Encoding.UTF8.GetBytes(jsonString);  
               request.ContentLength = bs.Length;  
               Stream newStream = request.GetRequestStream();  
               newStream.Write(bs, 0, bs.Length);  
               newStream.Close();  
           }  
  
  
           WebResponse response = request.GetResponse();  
           Stream stream = response.GetResponseStream();  
           Encoding encode = Encoding.UTF8;  
           StreamReader reader = new StreamReader(stream, encode);  
           string resultJson = reader.ReadToEnd();  
           return resultJson;  
       }  
       #endregion  

 

 

 

 

 

你可能感兴趣的:(WinFom,c#)