Created
December 27, 2017 23:41
-
-
Save kankikuchi/ca7afbd4e396b82f54603ff661e879cd to your computer and use it in GitHub Desktop.
UnityからSlackにメッセージを送る【Unity】【Slack】
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
// SlackNotifier.cs | |
// http://kan-kikuchi.hatenablog.com/entry/SlackNotifier | |
// | |
// Created by kan.kikuchi on 2017.12.12. | |
using UnityEngine; | |
using UnityEngine.Networking; | |
using System.Collections; | |
using System.Collections.Generic; | |
/// <summary> | |
/// UnityからSlackに通知を送るクラス | |
/// </summary> | |
public static class SlackNotifier { | |
//アクセストークン | |
private const string TOKEN = "ここにアクセストークンを設定してね!"; | |
/// <summary> | |
/// Slackに通知を送る | |
/// </summary> | |
public static IEnumerator Send(string channel, string text, Dictionary<string, string> paramDict = null) { | |
//基本のURL作成 | |
string url = string.Format( | |
"https://slack.com/api/chat.postMessage?token={0}&channel={1}&text={2}", | |
TOKEN, channel, WWW.EscapeURL(text) | |
); | |
//パラメータの設定 | |
if(paramDict != null){ | |
foreach (KeyValuePair<string, string> param in paramDict) { | |
url += "&" + param.Key + "=" + param.Value; | |
} | |
} | |
//UnityWebRequestを生成し、送信 | |
UnityWebRequest request = UnityWebRequest.Get(url); | |
yield return request.SendWebRequest(); | |
//通信エラー判定 | |
bool isError = request.isNetworkError || request.isHttpError; | |
//通信エラー内容の表示 | |
if(isError) { | |
Debug.LogWarning(request.url + "\n" + request.responseCode + "\n" + request.error + " \n" + request.downloadHandler.text); | |
} | |
//レスポンスの表示 | |
else{ | |
Debug.Log(request.responseCode + "\n" + request.downloadHandler.text); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment