Exemplo n.º 1
0
        /// <summary>
        /// Standard BotBuilder adapter method to send a message from the bot to the messaging API.
        /// </summary>
        /// <param name="context">A TurnContext representing the current incoming message and environment.</param>
        /// <param name="activities">An array of outgoing activities to be sent back to the messaging API.</param>
        /// <param name="cancellationToken">A cancellation token for the task.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public override async Task <ResourceResponse[]> SendActivitiesAsync(ITurnContext context, Activity[] activities, CancellationToken cancellationToken)
        {
            var responses = new List <ResourceResponse>();

            foreach (var activity in activities)
            {
                if (activity.Type != ActivityTypes.Message)
                {
                    throw new Exception("Only Activities of type Message are supported for sending.");
                }

                var message = FacebookHelper.ActivityToFacebook(activity);

                if (message.Message.Attachment != null)
                {
                    message.Message.Attachments = null;
                    message.Message.Text        = null;
                }

                var res = await _facebookClient.SendMessageAsync("/me/messages", message, null, cancellationToken).ConfigureAwait(false);

                var response = new ResourceResponse()
                {
                    Id = res,
                };

                responses.Add(response);
            }

            return(responses.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends activities to the conversation.
        /// </summary>
        /// <param name="turnContext">The context object for the turn.</param>
        /// <param name="activities">The activities to send.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        /// <remarks>If the activities are successfully sent, the task result contains
        /// an array of <see cref="ResourceResponse"/> objects containing the IDs that
        /// the receiving channel assigned to the activities.</remarks>
        public override async Task <ResourceResponse[]> SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
        {
            var responses = new List <ResourceResponse>();

            foreach (var activity in activities)
            {
                if (activity.Type != ActivityTypes.Message && activity.Type != ActivityTypes.Event)
                {
                    _logger.LogTrace($"Unsupported Activity Type: '{activity.Type}'. Only Activities of type 'Message' or 'Event' are supported.");
                }
                else
                {
                    var message = CreateFacebookMessageFromActivity(activity);

                    if (message.Message?.Attachment != null)
                    {
                        message.Message.Text = null;
                    }

                    var res = await _facebookClient.SendMessageAsync("/me/messages", message, null, cancellationToken)
                              .ConfigureAwait(false);

                    if (activity.Type == ActivityTypes.Event)
                    {
                        if (activity.Name.Equals(HandoverConstants.PassThreadControl, StringComparison.Ordinal))
                        {
                            var recipient = (string)activity.Value == "inbox" ? HandoverConstants.PageInboxId : (string)activity.Value;
                            await _facebookClient.PassThreadControlAsync(recipient, activity.Conversation.Id, HandoverConstants.MetadataPassThreadControl, cancellationToken).ConfigureAwait(false);
                        }
                        else if (activity.Name.Equals(HandoverConstants.TakeThreadControl, StringComparison.Ordinal))
                        {
                            await _facebookClient.TakeThreadControlAsync(activity.Conversation.Id, HandoverConstants.MetadataTakeThreadControl, cancellationToken).ConfigureAwait(false);
                        }
                        else if (activity.Name.Equals(HandoverConstants.RequestThreadControl, StringComparison.Ordinal))
                        {
                            await _facebookClient.RequestThreadControlAsync(activity.Conversation.Id, HandoverConstants.MetadataRequestThreadControl, cancellationToken).ConfigureAwait(false);
                        }
                    }

                    var response = new ResourceResponse()
                    {
                        Id = res,
                    };

                    responses.Add(response);
                }
            }

            return(responses.ToArray());
        }