Skip to content

Commit 90ca6b4

Browse files
authored
first version
prepping first release
1 parent 746481e commit 90ca6b4

9 files changed

+1337
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.EnterpriseManagement;
2+
using Microsoft.EnterpriseManagement.Common;
3+
using Microsoft.EnterpriseManagement.Configuration;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Advanced.Action.Log.Notifier
11+
{
12+
public class AddActionLogEntry
13+
{
14+
//Modified from Anton Gritsenko/Rob Ford on TechNet
15+
//https://social.technet.microsoft.com/Forums/WINDOWS/en-US/1f06e71c-00f4-4cf7-9f7e-a9a78b4b907c/creating-action-log-entry-for-incident-via-sdk-in-c?forum=customization
16+
17+
//Follows similiar SMLets Exchange Connector logic
18+
//https://github.com/AdhocAdam/smletsexchangeconnector/blob/5e488ed24ff9467e0a8691df56cc993eabfb9c7c/smletsExchangeConnector.ps1#L1876
19+
20+
public String AddToActionLog(EnterpriseManagementGroup emg, EnterpriseManagementObject WorkItem, string Comment, string User, string CommentType)
21+
{
22+
try
23+
{
24+
//Get the System.WorkItem.Library mp
25+
ManagementPack mpWorkItemLibrary = emg.ManagementPacks.GetManagementPack(new Guid("405D5590-B45F-1C97-024F-24338290453E"));
26+
27+
//Get the Action Log class, only 1 of 2 could be incoming - User Comments or Analyst Comments
28+
/*
29+
ManagementPackClass CommentLogType = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.AnalystCommentLog", mpWorkItemLibrary); //UserCommentLog
30+
ManagementPackClass UserCommentLog = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.UserCommentLog", mpWorkItemLibrary); //AnalystComments
31+
*/
32+
string commentClassName = "System.WorkItem.TroubleTicket." + CommentType;
33+
ManagementPackClass CommentLogType = emg.EntityTypes.GetClass(commentClassName, mpWorkItemLibrary);
34+
35+
//Create a new action log entry
36+
CreatableEnterpriseManagementObject objectActionLog = new CreatableEnterpriseManagementObject(emg, CommentLogType);
37+
38+
//Check description
39+
Comment += "\n";
40+
if (Comment.Length > 4000) Comment = Comment.Substring(0, 4000);
41+
42+
//Setup the action log entry
43+
Guid NewCommentGuid = Guid.NewGuid();
44+
objectActionLog[CommentLogType, "Id"].Value = NewCommentGuid.ToString();
45+
objectActionLog[CommentLogType, "DisplayName"].Value = NewCommentGuid.ToString();
46+
objectActionLog[CommentLogType, "Comment"].Value = Comment;
47+
objectActionLog[CommentLogType, "EnteredBy"].Value = "Azure Translate/" + User;
48+
objectActionLog[CommentLogType, "EnteredDate"].Value = DateTime.Now.ToUniversalTime();
49+
50+
//Get the enumeration and relationship
51+
ManagementPackRelationship wiActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItemHasCommentLog", mpWorkItemLibrary);
52+
ManagementPackRelationship userActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasUserComment", mpWorkItemLibrary);
53+
ManagementPackRelationship analystActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasAnalystComment", mpWorkItemLibrary);
54+
55+
//Get the projection for the incident
56+
EnterpriseManagementObjectProjection emopWorkItem = new EnterpriseManagementObjectProjection(WorkItem);
57+
58+
//Add the comment and save
59+
ManagementPackRelationship WorkItemActionLogRelationship = null;
60+
switch (CommentType)
61+
{
62+
case "UserCommentLog":
63+
{WorkItemActionLogRelationship = userActionLogRel;}
64+
break;
65+
case "AnalystCommentLog":
66+
{WorkItemActionLogRelationship = analystActionLogRel;
67+
objectActionLog[CommentLogType, "IsPrivate"].Value = false;}
68+
break;
69+
}
70+
71+
//change the relationship if the work item is a Service Request
72+
if (WorkItem.LeastDerivedNonAbstractManagementPackClassId.ToString() == "04b69835-6343-4de2-4b19-6be08c612989")
73+
{
74+
WorkItemActionLogRelationship = wiActionLogRel;
75+
}
76+
77+
//write the new comment into the Action Log
78+
emopWorkItem.Add(objectActionLog, WorkItemActionLogRelationship.Target);
79+
emopWorkItem.Commit();
80+
81+
//return the new Comment Guid to notify on
82+
return objectActionLog.Id.ToString();
83+
}
84+
catch
85+
{
86+
return null;
87+
}
88+
}
89+
}
90+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using Microsoft.EnterpriseManagement;
2+
using Microsoft.EnterpriseManagement.Common;
3+
using Microsoft.EnterpriseManagement.Configuration;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Net;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace Advanced.Action.Log.Notifier
13+
{
14+
public class AzureCognitiveServicesTranslate
15+
{
16+
//Detect a Language using Azure Translate
17+
//https://docs.microsoft.com/en-us/azure/cognitive-services/translator/tutorial-wpf-translation-csharp#detect-language-of-source-text
18+
public String LanguageDetect(EnterpriseManagementGroup emg, string text)
19+
{
20+
//Get the API key for Azure Translate from the Admin Settings MP
21+
EnterpriseManagementObject emoAdminSetting = emg.EntityObjects.GetObject<EnterpriseManagementObject>(new Guid("49a053e7-6080-e211-fd79-ca3607eecce7"), ObjectQueryOptions.Default);
22+
string ACSTranslateKey = emoAdminSetting[null, "ACSAPIKey"].Value.ToString();
23+
24+
//site
25+
string ACSTranslateSite = "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0";
26+
27+
try
28+
{
29+
// Create request to Detect languages with Translator Text
30+
HttpWebRequest detectLanguageWebRequest = (HttpWebRequest)WebRequest.Create(ACSTranslateSite);
31+
detectLanguageWebRequest.Headers.Add("Ocp-Apim-Subscription-Key", ACSTranslateKey);
32+
detectLanguageWebRequest.ContentType = "application/json; charset=utf-8";
33+
detectLanguageWebRequest.Method = "POST";
34+
35+
// Send request
36+
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
37+
string jsonText = serializer.Serialize(text);
38+
39+
string body = "[{ \"Text\": " + jsonText + " }]";
40+
byte[] data = Encoding.UTF8.GetBytes(body);
41+
42+
detectLanguageWebRequest.ContentLength = data.Length;
43+
44+
using (var requestStream = detectLanguageWebRequest.GetRequestStream())
45+
requestStream.Write(data, 0, data.Length);
46+
47+
HttpWebResponse response = (HttpWebResponse)detectLanguageWebRequest.GetResponse();
48+
49+
// Read and parse JSON response
50+
var responseStream = response.GetResponseStream();
51+
var jsonString = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")).ReadToEnd();
52+
dynamic jsonResponse = serializer.DeserializeObject(jsonString);
53+
54+
// Fish out the detected language code
55+
var languageInfo = jsonResponse[0];
56+
if (languageInfo["score"] > (decimal)0.5)
57+
{
58+
return languageInfo["language"];
59+
}
60+
else
61+
{
62+
return "Unable to confidently detect input language.";
63+
}
64+
}
65+
catch
66+
{
67+
return "Unable to contact Azure.";
68+
}
69+
70+
}
71+
72+
//Translate a Language
73+
//https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-csharp-translate
74+
//modified to follow suit from above request
75+
public String LanguageTranslate(EnterpriseManagementGroup emg, string SourceLanguageCode, string TargetLanguageCode, string TextToTranslate)
76+
{
77+
//Get the API key for Azure Translate from the Admin Settings MP
78+
EnterpriseManagementObject emoAdminSetting = emg.EntityObjects.GetObject<EnterpriseManagementObject>(new Guid("49a053e7-6080-e211-fd79-ca3607eecce7"), ObjectQueryOptions.Default);
79+
string ACSTranslateKey = emoAdminSetting[null, "ACSAPIKey"].Value.ToString();
80+
81+
//site
82+
//https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=$($SourceLanguage)&to=$($TargetLanguage)
83+
string ACSTranslateSite = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";
84+
85+
//prepare the text
86+
TextToTranslate = TextToTranslate.Trim();
87+
88+
// send HTTP request to perform the translation
89+
string uri = string.Format(ACSTranslateSite + "&from={0}&to={1}", SourceLanguageCode, TargetLanguageCode);
90+
91+
// Send request
92+
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
93+
string jsonText = serializer.Serialize(TextToTranslate);
94+
95+
string body = "[{ \"Text\": " + jsonText + " }]";
96+
byte[] data = Encoding.UTF8.GetBytes(body);
97+
98+
try
99+
{
100+
// Create request to Detect languages with Translator Text
101+
HttpWebRequest translateLanguageWebRequest = (HttpWebRequest)WebRequest.Create(uri);
102+
translateLanguageWebRequest.Headers.Add("Ocp-Apim-Subscription-Key", ACSTranslateKey);
103+
translateLanguageWebRequest.ContentType = "application/json; charset=utf-8";
104+
translateLanguageWebRequest.Method = "POST";
105+
106+
translateLanguageWebRequest.ContentLength = data.Length;
107+
108+
using (var requestStream = translateLanguageWebRequest.GetRequestStream())
109+
requestStream.Write(data, 0, data.Length);
110+
111+
HttpWebResponse response = (HttpWebResponse)translateLanguageWebRequest.GetResponse();
112+
113+
// Read and parse JSON response
114+
var responseStream = response.GetResponseStream();
115+
var jsonString = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")).ReadToEnd();
116+
dynamic jsonResponse = serializer.DeserializeObject(jsonString);
117+
118+
//Return the translated text
119+
var translation = jsonResponse[0];
120+
return translation["translations"][0]["text"];
121+
}
122+
catch
123+
{
124+
return "Unable to contact Azure.";
125+
}
126+
}
127+
}
128+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.ComponentModel.Design;
4+
using System.Collections;
5+
using System.Linq;
6+
using System.Workflow.ComponentModel.Compiler;
7+
using System.Workflow.ComponentModel.Serialization;
8+
using System.Workflow.ComponentModel;
9+
using System.Workflow.ComponentModel.Design;
10+
using System.Workflow.Runtime;
11+
using System.Workflow.Activities;
12+
using System.Workflow.Activities.Rules;
13+
using Microsoft.EnterpriseManagement;
14+
using Microsoft.EnterpriseManagement.Configuration;
15+
using Microsoft.EnterpriseManagement.Common;
16+
using Microsoft.EnterpriseManagement.Workflow.Common;
17+
using Microsoft.EnterpriseManagement.Notifications.Workflows;
18+
using System.Collections.Generic;
19+
20+
namespace Advanced.Action.Log.Notifier
21+
{
22+
public class NotifyUser : SendNotificationsActivity
23+
{
24+
public void SendNotification(Guid SubscriptionId, string[] DataItems, string InstanceId, string[] TemplateIds, string[] PrimaryUserGuids, ActivityExecutionContext executionContext)
25+
{
26+
NotifyUser notice = new NotifyUser();
27+
notice.SubscriptionId = SubscriptionId;
28+
notice.DataItems = DataItems;
29+
notice.InstanceId = InstanceId;
30+
notice.TemplateIds = TemplateIds;
31+
notice.PrimaryUserList = PrimaryUserGuids;
32+
33+
notice.Execute(executionContext);
34+
}
35+
}
36+
}

AdhocAdam.Advanced.Action.Log.Notifier/TroubleTicketAnalystNotification.Designer.cs

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)