Пример #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());
        }
Пример #2
0
        /// <summary>
        /// Standard BotBuilder adapter method to send a message from the bot to the messaging API.
        /// </summary>
        /// <param name="turnContext">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 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 = FacebookHelper.ActivityToFacebook(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).ConfigureAwait(false);
                        }
                        else if (activity.Name.Equals(HandoverConstants.TakeThreadControl, StringComparison.Ordinal))
                        {
                            await _facebookClient.TakeThreadControlAsync(activity.Conversation.Id, HandoverConstants.MetadataTakeThreadControl).ConfigureAwait(false);
                        }
                        else if (activity.Name.Equals(HandoverConstants.RequestThreadControl, StringComparison.Ordinal))
                        {
                            await _facebookClient.RequestThreadControlAsync(activity.Conversation.Id, HandoverConstants.MetadataRequestThreadControl).ConfigureAwait(false);
                        }
                    }

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

                    responses.Add(response);
                }
            }

            return(responses.ToArray());
        }
Пример #3
0
 /// <summary>
 /// Factory method to create the <see cref="FacebookMessage"/> instance of the <see cref="Activity"/> to be sent to Facebook.
 /// </summary>
 /// <remarks>
 /// This lets an override add a Facebook-supported message tag to an outgoing message.
 /// See https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags/.
 /// </remarks>
 /// <param name="activity">An <see cref="Activity"/> instance to build the message.</param>
 /// <returns>A <see cref="FacebookMessage"/> built from the activity instance.</returns>
 protected virtual FacebookMessage CreateFacebookMessageFromActivity(Activity activity)
 {
     return(FacebookHelper.ActivityToFacebook(activity));
 }