Not receiving Change Notification of Calendar Events containing custom properties - outlook

We want to receive change notifications from our user's Outlook calendars. We'd like to limit those notifications to only those calendar items that contain our custom property.
CODE WHICH CREATES CALENDAR EVENT
private static async Task<Event> CreateAppointmentAsync(GraphServiceClient graphClient)
{
var newEvent = new Microsoft.Graph.Event
{
Subject = "Test Calendar Appointmnt",
Start = new DateTimeTimeZone() { TimeZone = TimeZoneInfo.Local.Id, DateTime = "2020-11-21T21:00:00" },
End = new DateTimeTimeZone() { TimeZone = TimeZoneInfo.Local.Id, DateTime = "2020-11-21T22:00:00" },
Location = new Location() { DisplayName = "Somewhere" },
Body = new ItemBody { Content = "Some Random Text" },
};
Microsoft.Graph.Event addedEvent;
try
{
newEvent.SingleValueExtendedProperties = new EventSingleValueExtendedPropertiesCollectionPage();
newEvent.SingleValueExtendedProperties.Add(new SingleValueLegacyExtendedProperty { Id = "String {00020329-0000-0000-C000-000000000046} Name CompanyID", Value = "12345" });
addedEvent = await graphClient.Me.Calendar.Events.Request().AddAsync(newEvent);
}
catch (Exception e)
{
throw e;
}
return addedEvent;
}
THE SUBSCRIPTION CODE SNIPPET
var subscription = new Subscription
{
ChangeType = "created",
NotificationUrl ="<OUR-URL>",
Resource = "me/events/?$filter=singleValueExtendedProperties/any(ep: ep/id eq 'String {00020329-0000-0000-C000-000000000046} Name CompanyID' and ep/value ne null)",
ExpirationDateTime = DateTimeOffset.Parse("2020-11-13T18:23:45.9356913Z"),
ClientState = "custom_data_state",
LatestSupportedTlsVersion = "v1_2"
};
The subscription is created successfully, but notifications are not being sent for those items containing the specific custom property described above.

It turns out I was only capturing the "created" notification. I neglected to add "updated" and "deleted".
I was testing with an event that already existed in my calendar. No events were being fired because I didn't create the subscription to detect updates and deletes.
Here is the corrected subscription:
var subscription = new Subscription
{
ChangeType = "created,updated,deleted",
NotificationUrl ="<OUR-URL>",
Resource = "me/events/?$filter=singleValueExtendedProperties/any(ep: ep/id eq 'String {00020329-0000-0000-C000-000000000046} Name CompanyID' and ep/value ne null)",
ExpirationDateTime = DateTimeOffset.Parse("2020-11-13T18:23:45.9356913Z"),
ClientState = "custom_data_state",
LatestSupportedTlsVersion = "v1_2"
};

Related

Bot Framework v4 - IndexOutOfRangeException when 2 tabs are open

I made a bot with bot framework v4, using C#, and it's on a webpage, https://websitebotv2.azurewebsites.net/, if there's only 1 user it works fine but the moment I open it on a new tab it gives a IndexOutOfRangeException when I start the conversation.
What do I need to do to make it work with multiple tabs open?
When my bot stars it creates a waterfall dialog asking the name and greeting the user:
public dialogBotBot(dialogBotAccessors accessors, LuisRecognizer luis, QnAMaker qna)
{
// Set the _accessors
_accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
// The DialogSet needs a DialogState accessor, it will call it when it has a turn context.
_dialogs = new DialogSet(accessors.ConversationDialogState);
// This array defines how the Waterfall will execute.
var waterfallSteps = new WaterfallStep[] {
NameStepAsync,
NameConfirmStepAsync,
};
// The incoming luis variable is the LUIS Recognizer we added above.
this.Recognizer = luis ?? throw new System.ArgumentNullException(nameof(luis));
// The incoming QnA variable is the QnAMaker we added above.
this.QnA = qna ?? throw new System.ArgumentNullException(nameof(qna));
// Add named dialogs to the DialogSet. These names are saved in the dialog state.
_dialogs.Add(new WaterfallDialog("details", waterfallSteps));
_dialogs.Add(new TextPrompt("name"));
}
Then I will save his name on UserProfile class, which contains the field Name and Context, the Context has the purpose of saving the conversation.
This works the first time, but if I open a new tab or refresh the current tab for a new conversation the bot will fetch the first conversation data.
The Exception is thrown in Startup.cs in:
services.AddBot<dialogBotBot>(options =>
{
options.CredentialProvider = new ConfigurationCredentialProvider(Configuration);
// Catches any errors that occur during a conversation turn and logs them to currently
// configured ILogger.
ILogger logger = _loggerFactory.CreateLogger<dialogBotBot>();
options.OnTurnError = async (context, exception) =>
{
logger.LogError($"Exception caught : {exception}");
await context.SendActivityAsync(exception + "\nSorry, it looks like something went wrong.\n" + exception.Message);
};
// Create and add conversation state.
var conversationState = new ConversationState(dataStore);
options.State.Add(conversationState);
// Create and add user state.
var userState = new UserState(dataStore);
options.State.Add(userState);
});
My onTurnAsync method is:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
// Handle Message activity type, which is the main activity type for shown within a conversational interface
// Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
// see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
if (turnContext.Activity.Type == ActivityTypes.Message)
{
//Get the current user profile
userProfile = await _accessors.UserProfile.GetAsync(turnContext, () => new UserProfile(), cancellationToken);
userProfile.Contexto.Add(turnContext.Activity.Text);
foreach (string s in userProfile.Contexto)
await turnContext.SendActivityAsync(s);
// Get the conversation state from the turn context.
var state = await _accessors.CounterState.GetAsync(turnContext, () => new CounterState());
// Bump the turn count for this conversation.
state.TurnCount++;
// Check LUIS model
var recognizerResult = await this.Recognizer.RecognizeAsync(turnContext, cancellationToken);
var topIntent = recognizerResult?.GetTopScoringIntent();
// Get the Intent as a string
string strIntent = (topIntent != null) ? topIntent.Value.intent : "";
// Get the IntentScore as a double
double dblIntentScore = (topIntent != null) ? topIntent.Value.score : 0.0;
// Only proceed with LUIS if there is an Intent
// and the score for the Intent is greater than 95
if (strIntent != "" && (dblIntentScore > 2))
{
switch (strIntent)
{
case "None":
//add the bot response to contexto
await turnContext.SendActivityAsync("Desculpa, não percebi.");
break;
case "Utilities_Help":
//add the bot response to contexto
await turnContext.SendActivityAsync("Quero-te ajudar!\nO que precisas?");
break;
default:
// Received an intent we didn't expect, so send its name and score.
//add the bot response to contexto
await turnContext.SendActivityAsync($"Intent: {topIntent.Value.intent} ({topIntent.Value.score}).");
break;
}
}
else
{
if (userProfile.Name == null)
{
// Run the DialogSet - let the framework identify the current state of the dialog from the dialog stack and figure out what (if any) is the active dialog.
var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
var results = await dialogContext.ContinueDialogAsync(cancellationToken);
// If the DialogTurnStatus is Empty we should start a new dialog.
if (results.Status == DialogTurnStatus.Empty)
{
await dialogContext.BeginDialogAsync("details", null, cancellationToken);
}
}
else
{
var answers = await this.QnA.GetAnswersAsync(turnContext);
if (answers.Any() && answers[0].Score > 0.7)
{
// If the service produced one or more answers, send the first one.
await turnContext.SendActivityAsync(answers[0].Answer + "\n" + state.TurnCount);
}
else
{
var responseMessage = $"Ainda não sei a resposta mas vou averiguar\nPosso-te ajudar com mais alguma coisa?";
String connectionString = "Data Source=botdataserverv1.database.windows.net;" +
"Initial Catalog=botDataBase;" +
"User id=AzureAdmin#botdataserverv1.database.windows.net;" +
"Password=admin_123;";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command;
String sms = turnContext.Activity.Text;
float result = answers[0].Score;
String insertMessage = "insert into Mensagem(texto,contexto,grauCerteza)" +
"values('" + sms + "', 'Falta apurar o contexto' ," + result + ")";
connection.Open();
command = new SqlCommand(insertMessage, connection);
adapter.InsertCommand = new SqlCommand(insertMessage, connection);
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
connection.Close();
await turnContext.SendActivityAsync(responseMessage);
}
}
// Save the user profile updates into the user state.
await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
// Set the property using the accessor.
await _accessors.CounterState.SetAsync(turnContext, state);
// Save the new turn count into the conversation state.
await _accessors.ConversationState.SaveChangesAsync(turnContext);
}
}
}
Your problem is here:
float result = answers[0].Score;
You have:
// Check to see if we have any answers
if (answers.Any() && answers[0].Score > 0.7)
{
[...]
// This is fine
}
else // else, WE HAVE NO ANSWERS
{
[...]
// At this point, answers is an empty array, so answers[0] throws an IndexOutOfRangeException
float result = answers[0].Score;
The reason this happens on refresh is because the new tab's user uses the same User Id. The bot already knows their name, so doesn't show the dialog, and when it calls await this.Recognizer.RecognizeAsync(turnContext, cancellationToken);, the user hasn't entered anything in the new turnContext, so it returns an empty array.
Sidenote: You can set the userID in WebChat with this:
window.WebChat.renderWebChat(
{
directLine: directLine,
userID: "USER_ID" // Make is use Math.random() or something if you want it to be random for each refresh
},
this.botWindowElement.nativeElement
);

How do I Attach an AdaptiveCardFromJson in a LUIS Bot C#?

I asked a similar question recently but wasn't specific enough. I see that there is some code with the AdaptiveCards NuGet Package to attach an AdaptiveCardFromJson and AdaptiveCardFromSDK, which under a the normal Microsoft Bot Model is available.
However, under the Microsoft LUIS Bot Model isn't an option, here's the code I have which returns an employee lookup result from a SQL DB Search:
[LuisIntent("Who_is_Employee")]
public async Task Who_is_EmployeeIntent(IDialogContext context, LuisResult result)
{
EntityRecommendation recommendation;
if (result.TryFindEntity("Communication.ContactName", out recommendation))
{
List<Employee> results = EmployeeService.FindEmployees(recommendation.Entity);
if (results.Count > 0)
{
string response = "";
foreach (Employee e in results)
{
string name = e.FullName;
string title = e.JobTitle;
response += " " + name + " " + title + "\n";
}
await context.PostAsync(response);
}
}
else
{
await context.PostAsync(" Sorry, I couldn't find who you were looking for.");
}
}
I would like that information to be returned as an AdaptiveCard, how do I achieve this?
Mark,
you need to craft your adaptive card either as json or using the SDK to create an instance of AdaptiveCard. Here is a great place to learn more about this.
Once you've crafted your card and have an instance of the AdaptiveCard class, you need to create a new message and attach the card to that message. The new message is what you'll post back to the user.
The code will look something like this
var card = AdaptiveCard.FromJson(<your json here>);
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var myRespsonse = context.MakeMessage();
myRespsonse.Attachments.Add(attachment);
await context.PostAsync(myRespsonse, CancellationToken.None);
This was the code I ended up having to use to make this successful:
[LuisIntent("Who_is_Employee")]
public async Task Who_is_EmployeeIntent(IDialogContext context, LuisResult result)
{
EntityRecommendation recommendation;
if (result.TryFindEntity("Communication.ContactName", out recommendation))
{
List<Employee> results = EmployeeService.FindEmployees(recommendation.Entity);
if (results.Count > 0)
{
/* Single line per result */
/*
string response = "";
foreach (Employee e in results)
{
string name = e.FullName;
string title = e.JobTitle;
response += " " + name + " " + title + "\n";
}
await context.PostAsync(response);
*/
/* Adaptive card per result */
// Load json template
string physicalPath = System.Web.HttpContext.Current.Server.MapPath("../AdaptiveCards/EmployeeLookup.json");
string jsonTemplate = "";
using (StreamReader r = new StreamReader(physicalPath))
{
jsonTemplate = r.ReadToEnd();
}
var respsonse = context.MakeMessage();
foreach (Employee e in results)
{
string employeeJson = jsonTemplate;
employeeJson = employeeJson.Replace("{{FullName}}", e.FullName);
employeeJson = employeeJson.Replace("{{JobTitle}}", e.JobTitle);
employeeJson = employeeJson.Replace("{{Reference}}", e.Reference);
employeeJson = employeeJson.Replace("{{Phone}}", e.Phone);
employeeJson = employeeJson.Replace("{{Email}}", e.Email);
employeeJson = employeeJson.Replace("{{Mobile}}", e.Mobile);
AdaptiveCard card = AdaptiveCard.FromJson(employeeJson).Card;
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
respsonse.Attachments.Add(attachment);
}
await context.PostAsync(respsonse);
}
}
else
{
await context.PostAsync(" Sorry, I couldn't find who you were looking for.");
}
}

Modify "LocalizedName" for an Business-process-flow in Dynamics 365 (Version 9.0)

I need to unify the "display name" of an business-process-flow in our environment.
The standard BPF "Opportunity Sales Process", should be called "Opportunity Sales Process" regardless of the UI language of the use (e.g. "Vertriebsprozess Verkaufschance" is the "LocalizedName" for German users).
I have not found any way to change the "LocalizedName" value - the only option is see, is the direct update of the customizations.xml.
Is is possible to update the "LocalizedName" of an BPF via code?
IOrganizationService os; // todo - initialize
Its not clear what you're after :) If you're after updating entity metadata - that is doable:
var request = new RetrieveEntityRequest { LogicalName = "opportunitysalesprocess" };
var response = (RetrieveEntityResponse)os.Execute(request);
var label = response.EntityMetadata.DisplayName.LocalizedLabels
.First(l => l.LanguageCode == 1033);
label.Label = "Thats Not My Name";
os.Execute(new UpdateEntityRequest { Entity = response.EntityMetadata });
If you're after updating process name in grid of processes; it is:
var sec = new SetLocLabelsRequest
{
AttributeName = "name",
Labels = new LocalizedLabel[]
{
new LocalizedLabel
{
Label = "Thats not my name",
LanguageCode = 1033
}
},
EntityMoniker = new EntityReference("workflow",
new Guid("3E8EBEE6-A2BC-4451-9C5F-B146B085413A"))
};
var res = (SetLocLabelsResponse)os.Execute(sec);
Refer to
https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.setloclabelsrequest?view=dynamics-general-ce-9
https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.messages.updateentityrequest?view=dynamics-general-ce-9

CRM SDK 2013 Activity doesn't exist just after creating it

I have a custom comment activity which I'm updating in code and this used to work but has started failing recently. It creates the activity but when I try to retrieve it or execute SetStateResponse on it, I get "tk_comment With Id = 9a1686d1-7d9d-e611-80e3-00155d001104 Does Not Exist" - which doesn't make sense as I've just created it! The activity record shows up against the account but I can't click on it there or do anything (Record is unavailable - The requested record was not found or you do not have sufficient permissions to view it.).
This is the code I'm using. I'd love you to tell me I've made some simple mistake :)
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
_serviceProxy.EnableProxyTypes();
try
{
tk_comment comment = new tk_comment();
int maxLength = 190; //subject has a max length of 200 characters
if (subject.Length > maxLength)
{
comment.Subject = subject.Substring(0, maxLength);
comment.Description = subject.Substring(maxLength, subject.Length - maxLength);
}
else
{
comment.Subject = subject;
}
comment.RegardingObjectId = entity.ToEntityReference();
comment.ActualStart = CommentDate;
comment.ActualEnd = CommentDate;
comment.ScheduledStart = CommentDate;
comment.ScheduledEnd = CommentDate;
Guid commentID = _serviceProxy.Create(comment);
try
{
tk_comment aComment = (tk_comment)_serviceProxy.Retrieve(tk_comment.EntityLogicalName, commentID, new ColumnSet(allColumns: true));
}
catch (Exception ex)
{
SingletonLogger.Instance.Error("Always an error here " + ex.Message);
}
Account test = (Account) _serviceProxy.Retrieve(Account.EntityLogicalName, entity.Id, new ColumnSet(allColumns: true));
// tk_comment newComment = (tk_comment)_serviceProxy.Retrieve(tk_comment.EntityLogicalName, commentID, new ColumnSet(allColumns: true));
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = new EntityReference(tk_comment.EntityLogicalName, commentID);
request.State = new OptionSetValue((int) tk_commentState.Completed); //completed
request.Status = new OptionSetValue(2); //completed
SetStateResponse response = (SetStateResponse)_serviceProxy.Execute(request); //always an error here too
}
Appreciate any suggestions
Cheers, Mick
It seems like you don't have proper read permission of this custom activity entity.
OR You need to validate all the permissions of this custom entity.

Sending email with template using trigger

I have the following Trigger:
trigger send_notification on Inquery__c (after update) {
Inquery__c inquery = trigger.new[0];
String[] toAddresses = new String[] {inquery.email__c};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(inquery.OwnerID);
mail.setSenderDisplayName('Salesforce Support');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
if (Trigger.isUpdate) {
if(inquery.Quilification__c == 'Qualified') {
EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Invitation_to_register_for_Class'];
mail.setTemplateId(et.id);
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
if(inquery.Quilification__c == 'Disqualified') {
EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Ineligible_course_candidate'];
mail.setTemplateId(et.id);
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}
I managed to fix this from its original problem,
And just wanted to share,
Thanks
I managed to fix it and send the email,
I have updated the code,
i.e. the code above works

Resources