-
Notifications
You must be signed in to change notification settings - Fork 5
/
MicrosoftTeamsChannelFunction.java
161 lines (127 loc) · 6.09 KB
/
MicrosoftTeamsChannelFunction.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.takipi.udf.microsoftteams;
import java.util.Base64;
import java.util.List;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.takipi.api.client.ApiClient;
import com.takipi.api.client.data.view.SummarizedView;
import com.takipi.api.client.request.event.EventRequest;
import com.takipi.api.client.request.event.EventsRequest;
import com.takipi.api.client.result.event.EventResult;
import com.takipi.api.client.result.event.EventsResult;
import com.takipi.api.client.util.event.EventUtil;
import com.takipi.api.client.util.view.ViewUtil;
import com.takipi.api.core.url.UrlClient;
import com.takipi.common.util.CollectionUtil;
import com.takipi.udf.ContextArgs;
import com.takipi.udf.util.TestUtil;
public class MicrosoftTeamsChannelFunction {
public static final int MINUTES_TIME_SPAN = 50000;
public static String validateInput(String rawInput) {
return getInput(rawInput).toString();
}
private static MicrosoftTeamsInput getInput(String rawInput) {
if (Strings.isNullOrEmpty(rawInput))
throw new IllegalArgumentException("Input is empty");
MicrosoftTeamsInput input;
try {
input = MicrosoftTeamsInput.of(rawInput);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
input.checkUrl();
return input;
}
public static void execute(String rawContextArgs, String rawInput) {
try {
executeImplementation(rawContextArgs, rawInput);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void executeImplementation(String rawContextArgs, String rawInput) {
MicrosoftTeamsUtil.logUDFInput(rawContextArgs, rawInput);
MicrosoftTeamsInput input = getInput(rawInput);
ContextArgs args = (new Gson()).fromJson(rawContextArgs, ContextArgs.class);
if (!args.validate()) {
throw new IllegalArgumentException("Bad context args: " + rawContextArgs);
}
if (!args.eventValidate()) {
return;
}
ApiClient apiClient = args.apiClient();
UrlClient.Response<EventResult> eventResultResponse = getEventResultResponse(args, apiClient);
System.out.println("Got exceptionClassName, exception type, exceptionLocationPath, stack_frames, deployment");
MicrosoftTeamsUtil.TimeSlot timeSlot = MicrosoftTeamsUtil.getTimeSlot(MINUTES_TIME_SPAN);
String exceptionLinkToOverOps = EventUtil.getEventRecentLink(apiClient, args.serviceId, args.eventId,
timeSlot.from, timeSlot.to, null, null, null);
System.out.println("Got ARC link");
EventResult eventResult = eventResultResponse.data;
MicrosoftTeamsChannelRequest microsoftTeamsChannelRequest = MicrosoftTeamsChannelRequest.newBuilder()
.setUrl(input.url).setEventResult(eventResult).setExceptionLink(exceptionLinkToOverOps)
.setServer(eventResult.introduced_by_server).setApplication(eventResult.introduced_by_application)
.setEnvironmentName(MicrosoftTeamsUtil.getEnvironmentName(apiClient, args.serviceId))
.setDeployment(eventResult.introduced_by).setDoNotAlertLink(getDoNotAlertLink(args, eventResult.type))
.build();
UrlClient.Response<String> post = SimpleUrlClient.newBuilder().build().post(microsoftTeamsChannelRequest);
System.out.println("Post Microsoft Teams Webhook Channel request");
if (post.isBadResponse()) {
throw new IllegalStateException("Can't send card to " + input.url);
}
}
private static String getDoNotAlertLink(ContextArgs args, String exceptionType) {
return args.appHost + "/index.html?key=" + args.serviceId + "&nav=archivemailitem&exception_class="
+ Base64.getEncoder().encodeToString(exceptionType.getBytes());
}
private static UrlClient.Response<EventResult> getEventResultResponse(ContextArgs args, ApiClient apiClient) {
EventRequest eventRequest = EventRequest.newBuilder().setEventId(args.eventId).setServiceId(args.serviceId)
.setIncludeStacktrace(true).build();
UrlClient.Response<EventResult> eventResultResponse = apiClient.get(eventRequest);
if ((!eventResultResponse.isOK()) || (eventResultResponse.data == null)) {
throw new IllegalStateException("Event is not present or api issue. eventId = " + args.eventId
+ " , serviceId = " + args.serviceId);
}
return eventResultResponse;
}
// A sample program on how to programmatically activate
public static void main(String[] args) {
ContextArgs contextArgs = TestUtil.getDefaultContextArgsBuilder().setApiKey("api_token").setServiceId("SXXXXX")
.build();
// get "All Events" view
SummarizedView view = ViewUtil.getServiceViewByName(contextArgs.apiClient(), contextArgs.serviceId, "View 1");
contextArgs.viewId = view.id;
MicrosoftTeamsUtil.TimeSlot timeSlot = MicrosoftTeamsUtil.getTimeSlot(MINUTES_TIME_SPAN);
// date parameter must be properly formatted
DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZoneUTC();
// get all events within the date range
EventsRequest eventsRequest = EventsRequest.newBuilder().setServiceId(contextArgs.serviceId)
.setViewId(contextArgs.viewId).setFrom(timeSlot.from.toString(fmt)).setTo(timeSlot.to.toString(fmt))
.build();
// create a new API Client
ApiClient apiClient = contextArgs.apiClient();
// execute API GET request
UrlClient.Response<EventsResult> eventsResponse = apiClient.get(eventsRequest);
// check for a bad API response
if (eventsResponse.isBadResponse())
throw new IllegalStateException("Failed getting events.");
// retrieve event data from the result
EventsResult eventsResult = eventsResponse.data;
// exit if there are no events - increase date range if this occurs
if (CollectionUtil.safeIsEmpty(eventsResult.events)) {
System.out.println("NO EVENTS");
return;
}
// retrieve a list of events from the result
List<EventResult> events = eventsResult.events;
// get the first event
contextArgs.eventId = events.get(0).id;
// set url similar to "url=https://outlook.office.com/webhook/..."
String rawInput = "url=https://outlook.office.com/webhook/...";
// convert context args to a JSON string
String rawContextArgs = new Gson().toJson(contextArgs);
// execute the UDF
MicrosoftTeamsChannelFunction.execute(rawContextArgs, rawInput);
}
}