釘釘添加自定義機器人 定時提醒群成員


  因為公司需要在釘釘添加一個自定義機器人,每天按時提醒群成員某件事情 。查看官網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;
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM