Ejemplo n.º 1
0
        /// <summary>
        /// Receives message from user and reply to it
        /// </summary>
        /// <param name="activity">Activity object</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation</returns>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            try
            {
                UserTelemetryInitializer.SetTelemetryUserId(HttpContext.Current, activity.From.Id);
                this.LogUserActivity(activity);

                using (var dialogScope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                {
                    this.connectorClient = dialogScope.Resolve <IConnectorClient>();

                    if (activity.Type == ActivityTypes.Message)
                    {
                        if (activity.Value != null)
                        {
                            // Process messageBack events using the dialog framework
                            var dialog = dialogScope.Resolve <RootDialog>();
                            await Conversation.SendAsync(activity, () => dialog);
                        }
                        else
                        {
                            // Send welcome card if user send any message to bot
                            var reply = activity.CreateReply();
                            reply.Attachments.Add(CelebrationCard.GetWelcomeCardInResponseToUserMessage().ToAttachment());
                            await this.connectorClient.Conversations.SendToConversationWithRetriesAsync(reply);
                        }
                    }
                    else if (activity.Type == ActivityTypes.ConversationUpdate)
                    {
                        this.logProvider.LogInfo("Processing conversationUpdate activity");
                        switch (activity.Conversation.ConversationType)
                        {
                        case "personal":
                            await this.HandlePersonalConversationUpdateAsync(activity);

                            break;

                        case "channel":
                            await this.HandleTeamConversationUpdateAsync(activity);

                            break;

                        default:
                            this.logProvider.LogWarning($"Received unexpected conversationUpdate activity with conversationType {activity.Conversation.ConversationType}");
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.logProvider.LogError($"Failed to process activity {activity.Type}", ex);
                throw;
            }

            return(this.Request.CreateResponse(HttpStatusCode.OK));
        }
        /// <summary>
        /// Receives message from user and reply to it.
        /// </summary>
        /// <param name="activity">activity object.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            UserTelemetryInitializer.SetTelemetryUserId(HttpContext.Current, activity.From.Id);

            this.LogUserActivity(activity);
            this.teamMembers = null;

            if (activity.Type == ActivityTypes.Invoke || (activity.Type == ActivityTypes.Message && activity.Value != null))
            {
                using (var dialogScope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                {
                    var dialog = dialogScope.Resolve <RootDialog>();
                    await Conversation.SendAsync(activity, () => dialog);
                }
            }
            else
            {
                using (var dialogScope = DialogModule.BeginLifetimeScope(this.scope, activity))
                {
                    IConnectorClient connectorClient = dialogScope.Resolve <IConnectorClient>();
                    this.connectorServiceHelper = new ConnectorServiceHelper(connectorClient, this.logProvider);
                    if (activity.Type == ActivityTypes.Message)
                    {
                        Activity welcomeActivity = activity.CreateReply();
                        welcomeActivity.Attachments.Add(CelebrationCard.GetWelcomeCardInResponseToUserMessage().ToAttachment());
                        await connectorClient.Conversations.ReplyToActivityAsync(welcomeActivity);
                    }
                    else
                    {
                        await this.HandleSystemMessageAsync(connectorClient, activity);
                    }
                }
            }

            var response = this.Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }