钉钉添加自定义机器人 定时提醒群成员

时间:2021-03-18 08:51:55

  因为公司需要在钉钉添加一个自定义机器人,每天按时提醒群成员某件事情 。查看官网https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq,先按照官网教程添加一个自定义机器人,获取机器人对应的Webhook地址,如

https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx,直接放入浏览器后发现提示
{"errmsg":"需要POST请求","errcode":43002}
仔细看完官方文档后觉得自己搞笑了。。。。根据开发文档编写了一个测试demo,代码用C#实现,封装的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;
        }

调用:

 string WEB_HOOK = "https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXX";
            string msg = "大家开始报数: ";

            String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";//注意C#中json的拼接
            string s = Post(WEB_HOOK, textMsg, null);

全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.IO;

namespace DingdingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string WEB_HOOK = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string msg = "大家开始报数: ";

            String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";
            string s = Post(WEB_HOOK, textMsg, null);
        }


                /// <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;
        }

    }

}