Skip to content

Instantly share code, notes, and snippets.

@glitchersgames
Created June 5, 2018 14:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glitchersgames/2e482f3cb60beb73ba587ba41f60a04b to your computer and use it in GitHub Desktop.
Save glitchersgames/2e482f3cb60beb73ba587ba41f60a04b to your computer and use it in GitHub Desktop.
/*
* 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