釘釘發送工作通知


下載官方.NET SDK,編譯之后生成 TopSdk.dll,C#項目中引用這個DLL文件 ,也可以在項目中帶着官方代碼。

一、准備工作:

CorpId:認證的企業都有這個,敏感信息,拒絕泄露

CorpSecret:認證的企業都有這個,敏感信息,拒絕泄露

AgentID:新建的H5應用會給應用憑證 AppKey,AppSecret,AgentID 發送工作通知需要 AgentID,其他兩個不用。

二、查看官方文檔,例子是JAVA。改成.Net 即可。園子有人用asyncsend 接口也能發送成功。我們這里使用官方文檔中的接口。

https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2

官方JVAV代碼改成.NET沒有碰到問題,直接上代碼吧。

        /// <summary>
        /// 發送釘釘工作消息
        /// </summary>
        /// <param name="agentId">應用的憑證號</param>
        /// <param name="userID">接收消息的員工號</param>
        /// <returns></returns>
        public static string SendDingMessage(long agentId,string userID)
        {
            //string Access_token=獲得token。這里只演示發送消息,默認已經獲得了token
            //定義client
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            //定義 request2
            OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
            req.SetHttpMethod("POST");//非必須
            req.ToAllUser = false;//是否發送給所有人,限額3條
            req.AgentId = agentId;//應用的ID
            req.UseridList = userID;// 員工UserID;
                                             //要發送的消息
            MsgDomain message = AddOaMessage("張三");// new MsgDomain();
            //卡片消息 文件消息 圖片消息 鏈接消息  markdown消息 OA消息 文本消息 語音消息
            //action_card  file  image  link markdown oa text voice
            /*
            //Text
            TextDomain text = new TextDomain();
            text.Content = "123";
            obj1.Text = text;
            //image
            ImageDomain image = new ImageDomain();
            image.MediaId = "@123";
            obj1.Image = image;
            //link
            LinkDomain link = new LinkDomain();
            link.PicUrl="picUrl";
            link.MessageUrl="messageUrl";
            link.Text="text";
            link.Title="title";
            obj1.Link=link;
            //file
            FileDomain obj5 = new FileDomain();
            obj5.MediaId="media_id";
            obj1.File=obj5;
            //voice
            VoiceDomain obj6 = new VoiceDomain();
            obj6.Duration="100";
            obj6.MediaId="100";
            obj1.Voice=obj6;
            //Markdown
            MarkdownDomain obj14 = new MarkdownDomain();
            obj14.Text="text";
            obj14.Title="title";
            obj1.Markdown=obj14;
            */

            /*
            //ActionCard
            ActionCardDomain actionCard = new ActionCardDomain();

            List<BtnJsonListDomain> list17 = new List<BtnJsonListDomain>();
            BtnJsonListDomain btnJsonList = new BtnJsonListDomain();
            btnJsonList.ActionUrl="action_url";
            btnJsonList.Title="title";
            list17.Add(btnJsonList);

            actionCard.BtnJsonList=list17;
            actionCard.BtnOrientation="btn_orientation";
            actionCard.SingleUrl="single_url";
            actionCard.SingleTitle="single_title";
            actionCard.Markdown="markdown";
            actionCard.Title="title";
            obj1.ActionCard=actionCard;
            */
            req.Msg_ = message;
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.Execute(req, Access_token);
            return rsp.Body;
        }


        /// <summary>
        /// 創建一個OA消息
        /// </summary>
        /// <param name="UserName">用戶名,非必須</param>
        /// <returns></returns>
        private static MsgDomain AddOaMessage(string UserName)
        {
            #region 輔助生成金額隨機數
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            Random random = new Random(iSeed);
            string money = random.Next(100).ToString();
            #endregion
            MsgDomain message = new MsgDomain();
            //卡片消息 文件消息 圖片消息 鏈接消息  markdown消息 OA消息 文本消息 語音消息
            //action_card  file  image  link markdown oa text voice
            message.Msgtype = "oa";
            OADomain OaMsg = new OADomain();//OA消息
            //包括 body head MessageUrl PcMessageUrl
            OaMsg.MessageUrl = "http://dingtalk.com";//消息點擊鏈接地址,當發送消息為小程序時支持小程序跳轉鏈接
            OaMsg.PcMessageUrl = "http://dingtalk.com";//PC端點擊消息時跳轉到的地址
            //消息頭                                           //*消息頭部內容
            HeadDomain head = new HeadDomain();
            head.Bgcolor = "FFBBBBBB";
            head.Text = "測試001";
            //oa 消息頭
            OaMsg.Head = head;
            //*body 消息體
            BodyDomain body = new BodyDomain();
            body.Author = "admin";//*發送人 
            body.FileCount = "3";//文件數量 非必須
            body.Image = "@lADOADmaWMzazQKA";//圖片 非必須
            body.Content = "釘釘URL";//*內容
            body.Title = "測試" + Guid.NewGuid().ToString("N");//*標題
            //**金額,一個數字類型
            RichDomain rich = new RichDomain();
            rich.Unit = "元";
            rich.Num = money;
            //**Form
            List<FormDomain> form = new List<FormDomain>();
            FormDomain obj12 = new FormDomain();
            obj12.Value = UserName;
            obj12.Key = "姓名";
            form.Add(obj12);
            FormDomain form2 = new FormDomain();
            form2.Value = "打牌,游泳";
            form2.Key = "愛好";
            form.Add(form2);
            //body 包括2項
            body.Rich = rich;
            body.Form = form;
            //OA消息 body 消息體
            OaMsg.Body = body;
            //消息
            message.Oa = OaMsg;
            return message;
        }

        private static MsgDomain AddTextMessage(string UserName)
        {
            #region 輔助生成金額隨機數
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            Random random = new Random(iSeed);
            string money = random.Next(100).ToString();
            #endregion

            MsgDomain message = new MsgDomain();
            //卡片消息 文件消息 圖片消息 鏈接消息  markdown消息 OA消息 文本消息 語音消息
            //action_card  file  image  link markdown oa text voice
            message.Msgtype = "text";
            //Text
            TextDomain text = new TextDomain();
            text.Content = UserName + "123" + money;
            message.Text = text;

            return message;
        }

 推送消息效果。

 

 

SDK:

https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02

官方文檔:

https://ding-doc.dingtalk.com/document#/org-dev-guide/send-work-notifications

消息類型:

https://ding-doc.dingtalk.com/document#/org-dev-guide/message-types-and-data-format#topic-2618201

參考:

https://www.cnblogs.com/seaquakear/p/11444901.html

 


免責聲明!

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



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