示例#1
0
        public async Task SendActivity(IBotContext context, IList <IActivity> activities, Middleware.MiddlewareSet.NextDelegate next)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityListNotNull(activities);

            await next().ConfigureAwait(false);

            await _bot.Adapter.Send(activities).ConfigureAwait(false);
        }
        public async Task SendActivity(IBotContext context, IList <IActivity> activities, Middleware.MiddlewareSet.NextDelegate next)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityListNotNull(activities);
            // Ensure template activities are bound .
            foreach (var activity in activities)
            {
                if (activity.Type == TEMPLATE)
                {
                    await this.BindActivityTemplate(context, activity).ConfigureAwait(false);
                }
            }

            await next().ConfigureAwait(false);
        }
        public async Task SendActivity(IBotContext context, IList <Activity> activities, MiddlewareSet.NextDelegate next)
        {
            BotAssert.ContextNotNull(context);
            BotAssert.ActivityListNotNull(activities);

            foreach (var activity in activities)
            {
                if (string.IsNullOrWhiteSpace(activity.Type))
                {
                    activity.Type = ActivityTypes.Message;
                }

                ApplyConversationReference(activity, context.ConversationReference);
            }

            await next().ConfigureAwait(false);
        }
示例#4
0
        public async override Task Send(IList <IActivity> activities)
        {
            BotAssert.ActivityListNotNull(activities);

            foreach (Activity activity in activities)
            {
                if (activity.Type == "delay")
                {
                    // The Activity Schema doesn't have a delay type build in, so it's simulated
                    // here in the Bot. This matches the behavior in the Node connector.
                    int delayMs = (int)activity.Value;
                    await Task.Delay(delayMs).ConfigureAwait(false);
                }
                else
                {
                    var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl), _credentials);
                    await connectorClient.Conversations.SendToConversationAsync(activity).ConfigureAwait(false);
                }
            }
        }
示例#5
0
        private async Task SendActivityInternal(IBotContext context, IList <Activity> activities, ISendActivity[] middleware)
        {
            BotAssert.MiddlewareNotNull(middleware);
            BotAssert.ActivityListNotNull(activities);

            if (middleware.Length == 0) // No middleware to run.
            {
                return;
            }

            async Task next()
            {
                // Remove the first item from the list of middleware to call,
                // so that the next call just has the remaining items to worry about.
                ISendActivity[] remainingMiddleware = middleware.Skip(1).ToArray();
                await SendActivityInternal(context, activities, remainingMiddleware).ConfigureAwait(false);
            }

            // Grab the current middleware, which is the 1st element in the array, and execute it
            await middleware[0].SendActivity(context, activities, next).ConfigureAwait(false);
        }
        public static void SeparateAttachments(this List <Activity> activities)
        {
            BotAssert.ActivityListNotNull(activities);

            // We need to iterate backwards because we're potentially changing the length of the list
            for (int i = activities.Count() - 1; i > -1; i--)
            {
                var activity        = activities[i];
                var attachments     = activity.Attachments;
                var attachmentCount = attachments?.Count();
                var hasText         = activity.Text != null;

                // Should the activity be split into multiple activities?
                if ((attachmentCount > 0 && hasText) ||
                    (attachmentCount > 1 && activity.AttachmentLayout != AttachmentLayoutTypes.Carousel))
                {
                    var separateActivities = new List <Activity>();

                    // Prepare the activity for serialization by removing its attachments
                    activity.Attachments = null;

                    // If an activity ID is present then it should be removed
                    // because two activities shouldn't have the same ID
                    activity.Id = null;

                    var jss = new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                        TypeNameHandling  = TypeNameHandling.All,
                    };

                    // Serialize the activity to JSON so it can be cloned
                    var json = JsonConvert.SerializeObject(activity, jss);

                    // Leave the original activity intact just in case it's needed later
                    activity.Attachments = attachments;

                    if (hasText)
                    {
                        var textActivity = JsonConvert.DeserializeObject <Activity>(json, jss);

                        separateActivities.Add(textActivity);
                    }

                    // AttachmentLayoutTypes.List is the default in the sense that
                    // ABS interprets any string other that "carousel" as "list"
                    if (activity.AttachmentLayout == AttachmentLayoutTypes.Carousel)
                    {
                        var carouselActivity = JsonConvert.DeserializeObject <Activity>(json, jss);

                        carouselActivity.Text        = null;
                        carouselActivity.Attachments = attachments;
                        separateActivities.Add(carouselActivity);
                    }
                    else
                    {
                        foreach (var attachment in attachments)
                        {
                            var attachmentActivity = JsonConvert.DeserializeObject <Activity>(json, jss);

                            attachmentActivity.Text        = null;
                            attachmentActivity.Attachments = new List <Attachment> {
                                attachment
                            };
                            separateActivities.Add(attachmentActivity);
                        }
                    }

                    activities.RemoveAt(i);
                    activities.InsertRange(i, separateActivities);
                }
            }
        }