• .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/
  • 相关阅读:
    Java RandomAccessFile用法(转)
    CSS样式设计小技巧(水平居中,垂直居中)
    CSS总结(下篇)
    CSS总结(中篇)
    CSS总结(上篇)
    Html常用到的标签
    java 重定向和转发的区别(转)
    eclipse启动tomcat无法访问的解决方法(转)
    Busybox下mdev配置说明
    Linux 下网卡参数配置
  • 原文地址:https://www.cnblogs.com/lxhbky/p/14415440.html
Copyright © 2020-2023  润新知