Exemple #1
0
        /// <summary>
        /// Handles each individual message inside a webhook payload (webhook may deliver more than one message at a time).
        /// </summary>
        /// <param name="message">The message to be processed.</param>
        /// <returns>An Activity with the result.</returns>
        public static Activity ProcessSingleMessage(FacebookMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.Sender == null && message.OptIn?.UserRef != null)
            {
                message.Sender = new FacebookBotUser {
                    Id = message.OptIn?.UserRef
                };
            }

            var activity = new Activity()
            {
                ChannelId    = "facebook",
                Timestamp    = default,
        /// <summary>
        /// Handles each individual message inside a webhook payload (webhook may deliver more than one message at a time).
        /// </summary>
        /// <param name="message">The message to be processed.</param>
        /// <returns>An Activity with the result.</returns>
        public static Activity ProcessSingleMessage(FacebookMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.Sender == null && message.OptIn?.UserRef != null)
            {
                message.Sender = new FacebookBotUser {
                    Id = message.OptIn?.UserRef
                };
            }

            var activity = new Activity()
            {
                ChannelId    = "facebook",
                Timestamp    = new DateTime(),
                Conversation = new ConversationAccount()
                {
                    Id = message.Sender?.Id,
                },
                From = new ChannelAccount()
                {
                    Id   = message.Sender?.Id,
                    Name = message.Sender?.Id,
                },
                Recipient = new ChannelAccount()
                {
                    Id   = message.Recipient.Id,
                    Name = message.Recipient.Id,
                },
                ChannelData = message,
                Type        = ActivityTypes.Event,
                Text        = null,
            };

            if (message.Message != null)
            {
                activity.Type = ActivityTypes.Message;
                activity.Text = message.Message.Text;

                if (activity.GetChannelData <FacebookMessage>().Message.IsEcho)
                {
                    activity.Type = ActivityTypes.Event;
                }

                // copy all fields (like attachments, sticker, quick_reply, nlp, etc.)
                activity.ChannelData = message.Message;
                if (message.Message.Attachments != null && message.Message.Attachments.Count > 0)
                {
                    activity.Attachments = HandleMessageAttachments(message.Message);
                }
            }
            else if (message.PostBack != null)
            {
                activity.Type = ActivityTypes.Message;
                activity.Text = message.PostBack.Payload;
            }

            return(activity);
        }