1. 程式人生 > >釘釘新增機器人定時提醒群成員

釘釘新增機器人定時提醒群成員

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

    }

}