public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var activity = await item as Activity;
            var userId   = context.Activity.From.Id;

            using (ConversationDataContext dataContext = new ConversationDataContext())
            {
                if (!dataContext.UserConversations.Any(
                        c => c.UserId == userId && c.ConversationId == context.Activity.Conversation.Id))
                {
                    dataContext.UserConversations.Add(new UserConversation
                    {
                        ID             = Guid.NewGuid().ToString(),
                        ConversationId = context.Activity.Conversation.Id,
                        UserId         = userId
                    });
                    try
                    {
                        dataContext.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        //TODO: Add error logging
                        Console.WriteLine(e);
                        throw;
                    }
                }
            }

            var reply = activity.CreateReply();

            if (activity.Text == null)
            {
                var value = activity.Value as Newtonsoft.Json.Linq.JObject;
                if (value != null)
                {
                    var cardValue = value.ToObject <CardValue>();

                    var activityValue = activity.Value as Newtonsoft.Json.Linq.JObject;
                    if (activityValue != null)
                    {
                        var categorySelection = activityValue.ToObject <CategorySelection>();
                        var category          = categorySelection.Category;
                        //query the database for products of this category type, create another adaptive card displaying the products and send to user


                        reply.Text = "Category=" + category;
                        await context.PostAsync(reply);
                    }
                    return;
                }
            }

            else if (activity.Text.ToLowerInvariant().Contains("hero"))
            {
                ReplyHeroCard(activity, reply);
            }
            else if (activity.Text.ToLowerInvariant().Contains("adaptive"))
            {
                ReplyAdaptiveCard(context, activity, reply);
            }
            else
            {
                int length = (activity.Text ?? string.Empty).Length;
                reply.Text = $"You sent {activity.Text} which was {length} characters";
            }



            await context.PostAsync(reply);

            context.Wait(MessageReceivedAsync);
        }
Ejemplo n.º 2
0
        async Task IBotDataStore <BotData> .SaveAsync(IAddress key, BotStoreType botStoreType, BotData botData, CancellationToken cancellationToken)
        {
            SqlBotDataEntity entity = new SqlBotDataEntity(botStoreType, key.BotId, key.ChannelId, key.ConversationId, key.UserId, botData.Data)
            {
                ETag       = botData.ETag,
                ServiceUrl = key.ServiceUrl
            };

            using (var context = new ConversationDataContext(_connectionStringName))
            {
                try
                {
                    if (String.IsNullOrEmpty(entity.ETag))
                    {
                        context.BotData.Add(entity);
                    }
                    else if (entity.ETag == "*")
                    {
                        var foundData = SqlBotDataEntity.GetSqlBotDataEntity(key, botStoreType, context);
                        if (botData.Data != null)
                        {
                            if (foundData == null)
                            {
                                context.BotData.Add(entity);
                            }
                            else
                            {
                                foundData.Data       = entity.Data;
                                foundData.ServiceUrl = entity.ServiceUrl;
                            }
                        }
                        else
                        {
                            if (foundData != null)
                            {
                                context.BotData.Remove(foundData);
                            }
                        }
                    }
                    else
                    {
                        var foundData = SqlBotDataEntity.GetSqlBotDataEntity(key, botStoreType, context);
                        if (botData.Data != null)
                        {
                            if (foundData == null)
                            {
                                context.BotData.Add(entity);
                            }
                            else
                            {
                                foundData.Data       = entity.Data;
                                foundData.ServiceUrl = entity.ServiceUrl;
                                foundData.ETag       = entity.ETag;
                            }
                        }
                        else
                        {
                            if (foundData != null)
                            {
                                context.BotData.Remove(foundData);
                            }
                        }
                    }
                    context.SaveChanges();
                }
                catch (System.Data.SqlClient.SqlException err)
                {
                    throw new HttpException((int)HttpStatusCode.InternalServerError, err.Message);
                }
            }
        }