zoukankan      html  css  js  c++  java
  • .net core发送钉钉消息_消息加签模式

    这是一篇纯记录代码文章,完整运行需要自己调整:

    钉钉发送消息后台进行了改版,这里介绍加签模式的代码:

    /// <summary>
            /// 发送钉钉消息内容
            /// </summary>
            /// <param name="groupName"></param>
            /// <param name="content"></param>
            /// <returns></returns>
            public async Task SendDingdingMessage(string content)
            {
                return;//去掉钉钉消息通知
    
                string requestHost = this._httpContextAccessor.HttpContext.Request.Host.Value;
                //测试和仿真环境发钉钉提示消息
                if (requestHost.Contains("meshop.net") || requestHost.Contains("runshopstore.com"))
                {
                    string sign = null;
    
                    TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    string timeStamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
    
                    var encoding = new UTF8Encoding();
                    byte[] signBytes = encoding.GetBytes($"{timeStamp}
    {this._dingDingGroup.Secret}");
                    byte[] secretByte = encoding.GetBytes(this._dingDingGroup.Secret);
                    using (var hmacsha256 = new HMACSHA256(secretByte))
                    {
                        byte[] hashmessage = hmacsha256.ComputeHash(signBytes);
                        sign = HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage));
                    }
    
                    string messageUrl = $"{this._dingDingGroup.WebHookUrl}&timestamp={timeStamp}&sign={sign}";
                    List<string> atPhoneList = null;
                    if (!string.IsNullOrEmpty(this._dingDingGroup.AtPhones))
                    {
                        atPhoneList = this._dingDingGroup.AtPhones.Split(',').ToList();
                        atPhoneList.RemoveAll(m => string.IsNullOrEmpty(m));
                    }
                    DingDingText dingDingText = new DingDingText
                    {
                        msgtype = "text",
                        text = new DingDingText.textObj
                        {
                            content = $"{CONST.HOST_ADMIN}_{content}"
                        },
                        at = new DingDingText.atObj
                        {
                            isAtAll = false,
                            atMobiles = atPhoneList
                        }
                    };
    
                    HttpClient httpClient = this._httpClientFactory.CreateClient();
                    await httpClient.Post(messageUrl, JsonHelper.ConvertJsonToStr(dingDingText), "application/json");
                }
            }
    *感谢您的阅读。喜欢的、有用的就请大哥大嫂们高抬贵手“推荐一下”吧!你的精神 支持是博主强大的写作动力。欢迎转载!
    *博主的文章是自己平时开发总结的经验,由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。
    *我的博客: http://www.cnblogs.com/lxhbky/
  • 相关阅读:
    Git配置
    第一次作业
    第二次作业
    python目前最好用的IDE——pycharm
    九九乘法表
    python语言的优点和缺点
    Python高效编程的19个技巧
    Python中 filter | map | reduce | lambda的用法
    Python 代码优化常见技巧
    求逆序对
  • 原文地址:https://www.cnblogs.com/lxhbky/p/14415440.html
Copyright © 2011-2022 走看看