Example #1
0
        public async Task Locate(IDialogContext context, LuisResult result)
        {
            EntityRecommendation entityType;
            EntityRecommendation date;
            string entityTypeString = string.Empty;
            string dateString       = string.Empty;

            if (result.TryFindEntity(Entity_Entity_Type, out entityType))
            {
                entityTypeString = entityType.Entity;
            }
            if (result.TryFindEntity(Entity_Date, out date))
            {
                List <DateTime> dates = date.ParseDateTimes();
                if (dates != null && dates.Count > 0)
                {
                    dateString = dates[0].ToString();
                }
            }
            if (!string.IsNullOrEmpty(entityTypeString) && !string.IsNullOrEmpty(dateString))
            {
                //Microsoft.Dynamics.CRM.Between(PropertyName='birthdate',PropertyValues=["1990-01-01","1990-01-01"])
                await CrmFunctions.RetrieveMultiple(context, entityTypeString, $"Microsoft.Dynamics.CRM.Between(PropertyName='createdon',PropertyValues=[\"{dateString}\",\"{DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")}\"])", new string[] { "fullname" }, context.Activity.ChannelId, context.Activity.From.Id);
            }
            context.Wait(MessageReceived);
        }
Example #2
0
        public virtual async Task <HttpResponseMessage> Post([FromBody] Activity message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));

            try
            {
                // check if activity is of type message
                if (message != null && message.GetActivityType() == ActivityTypes.Message)
                {
                    ChatState state = ChatState.RetrieveChatState(message.ChannelId, message.From.Id);

                    if (string.IsNullOrEmpty(state.OrganizationUrl) && CrmFunctions.ParseCrmUrl(message) == string.Empty)
                    {
                        await connector.Conversations.ReplyToActivityAsync(message.CreateReply("Hi there, before we can work together you need to tell me your Dynamics 365 URL (e.g. https://contoso.crm.dynamics.com)"));
                    }
                    else if (string.IsNullOrEmpty(state.AccessToken) || CrmFunctions.ParseCrmUrl(message) != string.Empty)
                    {
                        string extraQueryParams = string.Empty;

                        string crmUrl = CrmFunctions.ParseCrmUrl(message);

                        if (crmUrl != string.Empty && state.OrganizationUrl != crmUrl)
                        {
                            if (!string.IsNullOrEmpty(state.OrganizationUrl) && state.OrganizationUrl != crmUrl)
                            {
                                extraQueryParams = "prompt=login";
                            }
                            state.OrganizationUrl = crmUrl;
                        }

                        Activity replyToConversation = message.CreateReply();
                        replyToConversation.Recipient   = message.From;
                        replyToConversation.Type        = "message";
                        replyToConversation.Attachments = new List <Attachment>();

                        List <CardAction> cardButtons = new List <CardAction>();
                        CardAction        plButton    = new CardAction()
                        {
                            // ASP.NET Web Application Hosted in Azure
                            // Pass the user id
                            Value = $"{ConfigurationManager.AppSettings["BotAuthUrl"]}?channelId={HttpUtility.UrlEncode(message.ChannelId)}&userId={HttpUtility.UrlEncode(message.From.Id)}&userName={HttpUtility.UrlEncode(message.From.Name)}&fromId={HttpUtility.UrlEncode(message.Recipient.Id)}&fromName={HttpUtility.UrlEncode(message.Recipient.Name)}&serviceUrl={HttpUtility.UrlEncode(message.ServiceUrl)}&conversationId={HttpUtility.UrlEncode(message.Conversation.Id)}&extraQueryParams={extraQueryParams}",
                            Type  = "signin",
                            Title = "Connect"
                        };

                        cardButtons.Add(plButton);

                        SigninCard plCard = new SigninCard("Click connect to signin to Dynamics 365 (" + state.OrganizationUrl + ").", new List <CardAction>()
                        {
                            plButton
                        });
                        Attachment plAttachment = plCard.ToAttachment();
                        replyToConversation.Attachments.Add(plAttachment);
                        await connector.Conversations.SendToConversationAsync(replyToConversation);
                    }
                    else
                    {
                        await Conversation.SendAsync(message, () => new CrmLuisDialog());
                    }
                }
                else
                {
                    HandleSystemMessage(message);
                }
            }
            catch (Exception ex)
            {
                await connector.Conversations.ReplyToActivityAsync(message.CreateReply($"Kabloooey! Well played human you just fried my circuits. Thanks for being patient, I'm still learning to do some things while in preview. Hopefully, I'll get this worked out soon. Here's your prize: {ex.Message}"));
            }
            return(new HttpResponseMessage(System.Net.HttpStatusCode.Accepted));
        }