C#

博客园 首页 新随笔 联系 订阅 管理

下载官方.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

 

posted on 2020-11-18 10:43  不想当码农的码农  阅读(1756)  评论(0编辑  收藏  举报