zoukankan      html  css  js  c++  java
  • 钉钉开发系列(十二)机器人

    钉钉的每个群都可以建若干个机器人,有默认的比如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
                {
                    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
            /// <summary>
            /// 以Post方式提交命令
            /// </summary>
            /// <param name="apiurl">请求的URL</param>
            /// <param name="jsonString">请求的json参数</param>
            /// <param name="headers">请求头的key-value字典</param>
            public static String Post(string apiurl, string jsonString, Dictionary<String, String> 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
    通知结果如下图


    其他消息的类型的可以参照官方文档生成数据即可。

    欢迎打描左侧二维码打赏。

    转载请注明明出处。


  • 相关阅读:
    存储过程与触发器的区别
    WebDriver基本操作入门及UI自动化练手页面
    第四章 TestNG测试用例分步解析(上)
    第三章 Webdriver Java API简介(下)
    第三章 Webdriver Java API简介(上)
    第二章 TestNG环境搭建
    第一章 TestNG框架自动化简述
    基于Selenium2和TestNG的自动化测试
    程序员都应该知道的福利
    TestNG系列教程:并行执行测试
  • 原文地址:https://www.cnblogs.com/sparkleDai/p/7604902.html
Copyright © 2011-2022 走看看