Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
prepping first release
  • Loading branch information
AdhocAdam committed Sep 6, 2019
1 parent 746481e commit 90ca6b4
Show file tree
Hide file tree
Showing 9 changed files with 1,337 additions and 0 deletions.
90 changes: 90 additions & 0 deletions AdhocAdam.Advanced.Action.Log.Notifier/AddActionLogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Advanced.Action.Log.Notifier
{
public class AddActionLogEntry
{
//Modified from Anton Gritsenko/Rob Ford on TechNet
//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

//Follows similiar SMLets Exchange Connector logic
//https://github.com/AdhocAdam/smletsexchangeconnector/blob/5e488ed24ff9467e0a8691df56cc993eabfb9c7c/smletsExchangeConnector.ps1#L1876

public String AddToActionLog(EnterpriseManagementGroup emg, EnterpriseManagementObject WorkItem, string Comment, string User, string CommentType)
{
try
{
//Get the System.WorkItem.Library mp
ManagementPack mpWorkItemLibrary = emg.ManagementPacks.GetManagementPack(new Guid("405D5590-B45F-1C97-024F-24338290453E"));

//Get the Action Log class, only 1 of 2 could be incoming - User Comments or Analyst Comments
/*
ManagementPackClass CommentLogType = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.AnalystCommentLog", mpWorkItemLibrary); //UserCommentLog
ManagementPackClass UserCommentLog = emg.EntityTypes.GetClass("System.WorkItem.TroubleTicket.UserCommentLog", mpWorkItemLibrary); //AnalystComments
*/
string commentClassName = "System.WorkItem.TroubleTicket." + CommentType;
ManagementPackClass CommentLogType = emg.EntityTypes.GetClass(commentClassName, mpWorkItemLibrary);

//Create a new action log entry
CreatableEnterpriseManagementObject objectActionLog = new CreatableEnterpriseManagementObject(emg, CommentLogType);

//Check description
Comment += "\n";
if (Comment.Length > 4000) Comment = Comment.Substring(0, 4000);

//Setup the action log entry
Guid NewCommentGuid = Guid.NewGuid();
objectActionLog[CommentLogType, "Id"].Value = NewCommentGuid.ToString();
objectActionLog[CommentLogType, "DisplayName"].Value = NewCommentGuid.ToString();
objectActionLog[CommentLogType, "Comment"].Value = Comment;
objectActionLog[CommentLogType, "EnteredBy"].Value = "Azure Translate/" + User;
objectActionLog[CommentLogType, "EnteredDate"].Value = DateTime.Now.ToUniversalTime();

//Get the enumeration and relationship
ManagementPackRelationship wiActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItemHasCommentLog", mpWorkItemLibrary);
ManagementPackRelationship userActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasUserComment", mpWorkItemLibrary);
ManagementPackRelationship analystActionLogRel = emg.EntityTypes.GetRelationshipClass("System.WorkItem.TroubleTicketHasAnalystComment", mpWorkItemLibrary);

//Get the projection for the incident
EnterpriseManagementObjectProjection emopWorkItem = new EnterpriseManagementObjectProjection(WorkItem);

//Add the comment and save
ManagementPackRelationship WorkItemActionLogRelationship = null;
switch (CommentType)
{
case "UserCommentLog":
{WorkItemActionLogRelationship = userActionLogRel;}
break;
case "AnalystCommentLog":
{WorkItemActionLogRelationship = analystActionLogRel;
objectActionLog[CommentLogType, "IsPrivate"].Value = false;}
break;
}

//change the relationship if the work item is a Service Request
if (WorkItem.LeastDerivedNonAbstractManagementPackClassId.ToString() == "04b69835-6343-4de2-4b19-6be08c612989")
{
WorkItemActionLogRelationship = wiActionLogRel;
}

//write the new comment into the Action Log
emopWorkItem.Add(objectActionLog, WorkItemActionLogRelationship.Target);
emopWorkItem.Commit();

//return the new Comment Guid to notify on
return objectActionLog.Id.ToString();
}
catch
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Advanced.Action.Log.Notifier
{
public class AzureCognitiveServicesTranslate
{
//Detect a Language using Azure Translate
//https://docs.microsoft.com/en-us/azure/cognitive-services/translator/tutorial-wpf-translation-csharp#detect-language-of-source-text
public String LanguageDetect(EnterpriseManagementGroup emg, string text)
{
//Get the API key for Azure Translate from the Admin Settings MP
EnterpriseManagementObject emoAdminSetting = emg.EntityObjects.GetObject<EnterpriseManagementObject>(new Guid("49a053e7-6080-e211-fd79-ca3607eecce7"), ObjectQueryOptions.Default);
string ACSTranslateKey = emoAdminSetting[null, "ACSAPIKey"].Value.ToString();

//site
string ACSTranslateSite = "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0";

try
{
// Create request to Detect languages with Translator Text
HttpWebRequest detectLanguageWebRequest = (HttpWebRequest)WebRequest.Create(ACSTranslateSite);
detectLanguageWebRequest.Headers.Add("Ocp-Apim-Subscription-Key", ACSTranslateKey);
detectLanguageWebRequest.ContentType = "application/json; charset=utf-8";
detectLanguageWebRequest.Method = "POST";

// Send request
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonText = serializer.Serialize(text);

string body = "[{ \"Text\": " + jsonText + " }]";
byte[] data = Encoding.UTF8.GetBytes(body);

detectLanguageWebRequest.ContentLength = data.Length;

using (var requestStream = detectLanguageWebRequest.GetRequestStream())
requestStream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)detectLanguageWebRequest.GetResponse();

// Read and parse JSON response
var responseStream = response.GetResponseStream();
var jsonString = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")).ReadToEnd();
dynamic jsonResponse = serializer.DeserializeObject(jsonString);

// Fish out the detected language code
var languageInfo = jsonResponse[0];
if (languageInfo["score"] > (decimal)0.5)
{
return languageInfo["language"];
}
else
{
return "Unable to confidently detect input language.";
}
}
catch
{
return "Unable to contact Azure.";
}

}

//Translate a Language
//https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-csharp-translate
//modified to follow suit from above request
public String LanguageTranslate(EnterpriseManagementGroup emg, string SourceLanguageCode, string TargetLanguageCode, string TextToTranslate)
{
//Get the API key for Azure Translate from the Admin Settings MP
EnterpriseManagementObject emoAdminSetting = emg.EntityObjects.GetObject<EnterpriseManagementObject>(new Guid("49a053e7-6080-e211-fd79-ca3607eecce7"), ObjectQueryOptions.Default);
string ACSTranslateKey = emoAdminSetting[null, "ACSAPIKey"].Value.ToString();

//site
//https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=$($SourceLanguage)&to=$($TargetLanguage)
string ACSTranslateSite = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";

//prepare the text
TextToTranslate = TextToTranslate.Trim();

// send HTTP request to perform the translation
string uri = string.Format(ACSTranslateSite + "&from={0}&to={1}", SourceLanguageCode, TargetLanguageCode);

// Send request
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonText = serializer.Serialize(TextToTranslate);

string body = "[{ \"Text\": " + jsonText + " }]";
byte[] data = Encoding.UTF8.GetBytes(body);

try
{
// Create request to Detect languages with Translator Text
HttpWebRequest translateLanguageWebRequest = (HttpWebRequest)WebRequest.Create(uri);
translateLanguageWebRequest.Headers.Add("Ocp-Apim-Subscription-Key", ACSTranslateKey);
translateLanguageWebRequest.ContentType = "application/json; charset=utf-8";
translateLanguageWebRequest.Method = "POST";

translateLanguageWebRequest.ContentLength = data.Length;

using (var requestStream = translateLanguageWebRequest.GetRequestStream())
requestStream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)translateLanguageWebRequest.GetResponse();

// Read and parse JSON response
var responseStream = response.GetResponseStream();
var jsonString = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")).ReadToEnd();
dynamic jsonResponse = serializer.DeserializeObject(jsonString);

//Return the translated text
var translation = jsonResponse[0];
return translation["translations"][0]["text"];
}
catch
{
return "Unable to contact Azure.";
}
}
}
}
36 changes: 36 additions & 0 deletions AdhocAdam.Advanced.Action.Log.Notifier/NotifyUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Workflow.Common;
using Microsoft.EnterpriseManagement.Notifications.Workflows;
using System.Collections.Generic;

namespace Advanced.Action.Log.Notifier
{
public class NotifyUser : SendNotificationsActivity
{
public void SendNotification(Guid SubscriptionId, string[] DataItems, string InstanceId, string[] TemplateIds, string[] PrimaryUserGuids, ActivityExecutionContext executionContext)
{
NotifyUser notice = new NotifyUser();
notice.SubscriptionId = SubscriptionId;
notice.DataItems = DataItems;
notice.InstanceId = InstanceId;
notice.TemplateIds = TemplateIds;
notice.PrimaryUserList = PrimaryUserGuids;

notice.Execute(executionContext);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 90ca6b4

Please sign in to comment.