Created
June 5, 2018 14:31
-
-
Save glitchersgames/2e482f3cb60beb73ba587ba41f60a04b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Instructions for setting up a slackbot: | |
* https://get.slack.help/hc/en-us/articles/115005265703-Create-a-bot-for-your-workspace | |
* | |
* By @hugosslade for @glitchers | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Glitchers | |
{ | |
public class BugBot : MonoBehaviour | |
{ | |
#region Serialized | |
[SerializeField] | |
private string botToken; | |
#endregion | |
#region Lifecycle | |
private void Awake() | |
{ | |
Object.DontDestroyOnLoad(gameObject); | |
Application.logMessageReceived += Application_logMessageReceived; | |
} | |
private void OnDestroy() | |
{ | |
Application.logMessageReceived -= Application_logMessageReceived; | |
} | |
#endregion | |
#region Events | |
private void Application_logMessageReceived(string condition, string stackTrace, LogType type) | |
{ | |
if (type == LogType.Error || type == LogType.Assert || type == LogType.Exception) | |
{ | |
SendToSlack(type, condition, stackTrace); | |
} | |
} | |
#endregion | |
#region Methods | |
private void SendToSlack(LogType type, string log, string stackTrace) | |
{ | |
var url = "https://slack.com/api/chat.postMessage"; | |
var data = new WWWForm(); | |
data.AddField("token", botToken); | |
data.AddField("channel", "bugs"); | |
data.AddField("username", "bugbot"); | |
data.AddField("text", string.Format("[{0}] {1}\n{2}", type, log, stackTrace)); | |
var post = new WWW(url, data); | |
} | |
#endregion | |
#region Debug | |
[ContextMenu("Log Error")] | |
private void LogError() | |
{ | |
Debug.LogError("BugBot - LogError"); | |
} | |
[ContextMenu("Log Assert")] | |
private void LogAssert() | |
{ | |
Debug.LogAssertion("BugBot - LogAssert"); | |
} | |
[ContextMenu("Log Exception")] | |
private void LogException() | |
{ | |
throw new System.Exception("BugBot - LogException"); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment