钉钉小程序开发--企业通知消息发送给个人

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

public class DDMsgHelper
    {
        private static int _Agent_id = 123456;   //填写创建的钉钉应用id号

        private static string _CorpId = "11111111111";  //appkey
        private static string _CorpSecret = "fbdvzvzvzvzvcvxzvcxvcxzvc"; //appsecret
        private static string _Accesstoken = GetAccessToken();

        public static string SendMsg(string userid, string content)
        {
            string MessageUrl = string.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", _Accesstoken);
            var par = new
            {
                agent_id = _Agent_id,
                userid_list = userid,
                msg = new
                {
                    msgtype = "text",
                    text = new
                    {
                        content = content
                    }
                }
            };
            string jsonRequest = JsonConvert.SerializeObject(par);
            string res = HttpPost(MessageUrl, jsonRequest);
            return res;
        }


        /// <summary>
        /// 获取钉钉部门员工id
        /// </summary>
        /// <param name="accesstoken"></param>
        /// <param name="agent_id"></param>
        /// <param name="department_id"></param>
        public static List<User> GetUsers(string department_id)
        {
            List<User> users = new List<User>();
            var AccessToken = _Accesstoken;
            //获取部门人员id
            string MessageUrl = string.Format("https://oapi.dingtalk.com/user/simplelist?access_token={0}&department_id={1}&agent_id={2}&offset=0&size=100", AccessToken, department_id, _Agent_id);
            Newtonsoft.Json.Linq.JObject jsonmsg = Newtonsoft.Json.Linq.JObject.Parse(HttpGet(MessageUrl));
            if (jsonmsg.GetValue("userlist") != null)
            {
                var userlist = jsonmsg.GetValue("userlist");
                foreach (var item in userlist)
                {
                    User user = new User(item["name"].ToString(), item["userid"].ToString());
                    users.Add(user);
                }

            }
            return users;
            
        }

        /// <summary>
        /// 获取钉钉接口交互凭证
        /// </summary>
        /// <returns></returns>
        private static string GetAccessToken()
        {
           

            string AccessUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", _CorpId, _CorpSecret);
            Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(HttpGet(AccessUrl));
            string AccessToken = json["access_token"].ToString();
            return AccessToken;
        }

        private static string HttpGet(string url)
        {
            //创建            
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            //设置请求方法           
            httpWebRequest.Method = "GET";
            //请求超时时间            
            httpWebRequest.Timeout = 20000;
            //发送请求            
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            //利用Stream流读取返回数据            
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
            //获得最终数据,一般是json            
            string responseContent = streamReader.ReadToEnd();

            streamReader.Close();
            httpWebResponse.Close();

            return responseContent;

        }

        private static string HttpPost(string url, string data)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            //字符串转换为字节码            
            byte[] bs = Encoding.UTF8.GetBytes(data);
            //参数类型,这里是json类型                       
            httpWebRequest.ContentType = "application/json";
            //参数数据长度            
            httpWebRequest.ContentLength = bs.Length;
            //设置请求类型            
            httpWebRequest.Method = "POST";
            //设置超时时间            
            httpWebRequest.Timeout = 20000;
            //将参数写入请求地址中            
            httpWebRequest.GetRequestStream().Write(bs, 0, bs.Length);
            //发送请求            
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            //读取返回数据            
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
            string responseContent = streamReader.ReadToEnd();

            streamReader.Close();
            httpWebResponse.Close();
            httpWebRequest.Abort();
            return responseContent;

        }
    }

    public class User
    {
        public User(string name, string id)
        {
            this.Name = name;
            this.Id = id;
        }
        public string Name { get; set; }

        public string Id { get; set; }
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值