Created
April 15, 2019 11:21
-
-
Save OhkuboSGMS/488f065bf0a564f0270c45538f937a4c to your computer and use it in GitHub Desktop.
SlackにBuild通知を投げる
tokenにアプリのtokenを設定する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SlackNotificationOnBuildFinish.csをEditorフォルダに入れる(なければ作る)
PackageManagerからEditorCoroutineを有効にする.
Build終了後に設定されたチャンネルに通知が投稿される