Created
June 15, 2016 09:49
-
-
Save asus4/4d60e5fdf0ee526b3a412928cde7f7ca to your computer and use it in GitHub Desktop.
Unity to slack webhook url
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using MiniJSON; | |
namespace AppKit | |
{ | |
public class Slack | |
{ | |
string _webhookUrl; | |
Dictionary<string,object> _defaultOption; | |
float _lastSend; | |
public Slack(string webhookUrl, string optionJson = null) | |
{ | |
_webhookUrl = webhookUrl; | |
if (optionJson == null) | |
{ | |
_defaultOption = new Dictionary<string,object>(); | |
} | |
else | |
{ | |
_defaultOption = Json.Deserialize(optionJson) as Dictionary<string,object>; | |
} | |
} | |
public IEnumerator Post(string message) | |
{ | |
_lastSend = Time.realtimeSinceStartup; | |
var headers = new Dictionary<string, string>(); | |
headers.Add("Content-Type", "application/x-www-form-urlencoded"); | |
var form = new WWWForm(); | |
if (_defaultOption.ContainsKey("text")) | |
{ | |
_defaultOption["text"] = message; | |
} | |
else | |
{ | |
_defaultOption.Add("text", message); | |
} | |
form.AddField("payload", Json.Serialize(_defaultOption)); | |
var www = new WWW(_webhookUrl, form.data, headers); | |
yield return www; | |
Debug.LogFormat("slack result {0}", www.text); | |
} | |
public float lastSend | |
{ | |
get { return _lastSend; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Require
MiniJson
Usage