Skip to content

Instantly share code, notes, and snippets.

@asus4
Created June 15, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/4d60e5fdf0ee526b3a412928cde7f7ca to your computer and use it in GitHub Desktop.
Save asus4/4d60e5fdf0ee526b3a412928cde7f7ca to your computer and use it in GitHub Desktop.
Unity to slack webhook url
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; }
}
}
}
@asus4
Copy link
Author

asus4 commented Jun 15, 2016

Require

MiniJson

Usage

string option = @"{
    ""username"":""user"",
    ""icon_emoji"":"":ghost:"",
    ""attachments"":[
        {
            ""color"":""#ff0000""
            ""text"":""Warning""
        }
    ]
}";
var slack = new Slack("https://hooks.slack.com/services/xxxxxxxx", option);
StartCoroutine(slack.Post("<!channel> hello"));

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