Skip to content

Instantly share code, notes, and snippets.

@OhkuboSGMS
Created April 15, 2019 11:21
Show Gist options
  • Save OhkuboSGMS/488f065bf0a564f0270c45538f937a4c to your computer and use it in GitHub Desktop.
Save OhkuboSGMS/488f065bf0a564f0270c45538f937a4c to your computer and use it in GitHub Desktop.
SlackにBuild通知を投げる tokenにアプリのtokenを設定する
using System;
using System.Collections;
using System.Text;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Networking;
namespace BuildProcess
{
public class SlackNotificationOnBuildFinish
{
public class Payload
{
public string text;
public string type = "mrkdwn";
}
[PostProcessBuild(212)]
public static void OnBuildFinish(BuildTarget target, string pathToBuildProject)
{
var json = JsonUtility.ToJson(new Payload()
{
text =$"BuildFinished:\n Target: *{target}* \n OutputPath:{pathToBuildProject} \n FinishedTime:{DateTime.Now} "
});
var token = "XXXXX";
EditorCoroutineUtility.StartCoroutineOwnerless(PostSlackNotification(token,json));
}
private const string _baseUrl = "https://hooks.slack.com/services/";
public static IEnumerator PostSlackNotification(string token, string json)
{
using (UnityWebRequest www = SlackRequest(token, json))
{
www.SetRequestHeader("Content-Type", "application/json");
var op = www.Send();
while (!www.isDone)
{
if (www.isHttpError)
{
yield break;
}
yield return null;
}
if (www.isNetworkError)
{
Debug.Log(www.error);
}
}
}
public static UnityWebRequest SlackRequest(string token, string json)
{
//POSTだとContent-Typeが変更できない
UnityWebRequest www = new UnityWebRequest(_baseUrl + token, UnityWebRequest.kHttpVerbPOST);
www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
return www;
}
}
}
@OhkuboSGMS
Copy link
Author

OhkuboSGMS commented Apr 15, 2019

SlackNotificationOnBuildFinish.csをEditorフォルダに入れる(なければ作る)
PackageManagerからEditorCoroutineを有効にする.

  1. Appsでアプリを作成する
  2. Incoming Webhooksを有効にする
  3. Add New Webhook to Workspaceでtokenを発行
  4. ↑のtokenにトークンを設定
    Build終了後に設定されたチャンネルに通知が投稿される

@OhkuboSGMS
Copy link
Author

Slack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment