SlackIntegration.cs
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using UnityEngine;
using System.Collections;
using WebKit;
public class SlackIntegration : MonoBehaviour
{
// Incoming WebHook url. Get this by configuring a new Incoming WebHook integration in the slack services page of their site
public string webhookUrl;
// Optional notification
public string optionalMessage;
void Awake()
{
Application.logMessageReceived += Application_logMessageReceived;
DontDestroyOnLoad(this);
}
void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
if (condition.Contains("CMSRequest")) return;
if (type == LogType.Error || type == LogType.Exception)
Post(type.ToString() + ": " + condition + "\n" + stackTrace, type);
}
private void Post(string message, LogType type)
{
Request request = new Request();
request.url = webhookUrl;
request.method = RequestMethod.POST;
string emoji = ":impressed:";
string botname = "Information";
switch (type)
{
case LogType.Error:
botname = "Error";
emoji = ":cold_sweat:";
break;
case LogType.Exception:
botname = "Exception";
emoji = ":scream:";
break;
}
string postMessage = "Message recieved from app:\n*" + Application.companyName + " - " + Application.productName + "*\n" + message;
if (!string.IsNullOrEmpty(optionalMessage))
postMessage = optionalMessage + "\n" + postMessage;
var postData = new System.Collections.Generic.Dictionary<string, object>()
{
{"username", botname},
{"icon_emoji",emoji},
{"text", postMessage},
};
request.data = postData;
request.Send();
}
void OnDestroy()
{
Application.logMessageReceived -= Application_logMessageReceived;
}
}