Unity编辑器智能辅助工具 钉钉机器人

Unity在打bundle资源和出包的时候总是最烦人的,因为不知道进度条到底什么时候走完,所以得过会就去看下那坑爹的进度条,多次之后总算被耗到没耐心了,能不能发挥下程序员的能力解决下这烦人的问题呢,希望在Unity编辑器下当我们比如打完bundle或出包完时我们能第一时间收到消息,这样就不会在那漫长的等待过程中被消耗耐心。

结合钉钉的机器人,我们就能解决这个问题,具体开发文档可以前往钉钉官方查看,下面就是c#代码

using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public class DingTalkHelper
{
    private const string WEB_HOOK = @"https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxx";   //设置自己创建的机器人地址

    /// <summary>
    /// 钉钉机器人发送通知消息
    /// </summary>
    /// <param name="msg"></param>
    public static void Notify(string msg)
    {
        string textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";
        string s = Post(textMsg, null);

        Debug.Log("Post return:" + s);
    }

#region Post
    /// <summary>
    /// 以Post方式提交命令
    /// </summary>
    /// <param name="apiurl">请求的URL</param>
    /// <param name="jsonstring">请求的json参数</param>
    /// <param name="headers">请求头的key-value字典</param>
    private static string Post(string jsonstring, Dictionary<string, string> headers = null)
    {
        //远程证书无效
        ServicePointManager.ServerCertificateValidationCallback =
            delegate (object s, X509Certificate certificate,
                     X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };

        WebRequest request = WebRequest.Create(WEB_HOOK);
        request.Method = "POST";
        request.ContentType = "application/json";
        if (headers != null)
        {
            foreach (var keyValue in headers)
            {
                if (keyValue.Key == "Content-Type")
                {
                    request.ContentType = keyValue.Value;
                    continue;
                }
                request.Headers.Add(keyValue.Key, keyValue.Value);
            }
        }

        if (string.IsNullOrEmpty(jsonstring))
        {
            request.ContentLength = 0;
        }
        else
        {
            byte[] bs = Encoding.UTF8.GetBytes(jsonstring);
            request.ContentLength = bs.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(bs, 0, bs.Length);
            newStream.Close();
        }


        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        Encoding encode = Encoding.UTF8;
        StreamReader reader = new StreamReader(stream, encode);
        string resultJson = reader.ReadToEnd();
        return resultJson;
    }
#endregion
}

在需要的地方调用 Notify 接口,最终效果如下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值