コード例 #1
0
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var text = turnContext.Activity.Text.Trim();

                _logger.LogInformation("----- Receiving message activity - Text: {Text}", text);

                if (text.StartsWith("timer ", StringComparison.InvariantCultureIgnoreCase))
                {
                    var seconds = Convert.ToInt32(text.Substring(text.IndexOf(" ")));

                    await turnContext.SendActivityAsync($"Starting a {seconds}s timer");

                    _timers.AddTimer(turnContext.Activity.GetConversationReference(), seconds);
                }
                else if (text.StartsWith("timers", StringComparison.InvariantCultureIgnoreCase))
                {
                    var alarms = string.Join("\n", _timers.List.Select(a => $"- #{a.Number} [{a.Seconds}s] - {a.Status} ({a.Elapsed / 1000:n3}s)"));

                    await turnContext.SendActivityAsync($"**TIMERS**\n{alarms}");
                }
                else
                {
                    // Echo back to the user whatever they typed.
                    await turnContext.SendActivityAsync($"You typed \"{text}\"");
                }
            }
            else
            {
                await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");
            }
        }
コード例 #2
0
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
        {
            var activityType          = turnContext.Activity.Type;
            var conversationReference = (ConversationReference)null;

            if (activityType == ActivityTypes.Message || activityType == ActivityTypes.ConversationUpdate)
            {
                conversationReference = turnContext.Activity.GetConversationReference();

                _logger.LogTrace("----- ProactiveBot - Get conversation reference - Activity type: {ActivityType} User: \"{User}\" - ConversationReference: {@ConversationReference}", activityType, conversationReference.User.Name, conversationReference);

                if (conversationReference.User.Name != null)
                {
                    _conversations.Save(conversationReference);
                }
            }

            if (activityType == ActivityTypes.Message)
            {
                var text = turnContext.Activity.Text.Trim();

                _logger.LogInformation("----- Receiving message activity - Text: {Text}", text);

                var username = conversationReference.User.Name;

                if (text.StartsWith("timer ", StringComparison.InvariantCultureIgnoreCase))
                {
                    var seconds = Convert.ToInt32(text.Substring(text.IndexOf(" ")));

                    await turnContext.SendActivityAsync($"Starting a {seconds}s timer");

                    _logger.LogInformation("----- Adding timer - ConversationReference: {@ConversationReference}", conversationReference);

                    _timers.AddTimer(conversationReference, seconds);
                }
                else if (text.StartsWith("timers", StringComparison.InvariantCultureIgnoreCase))
                {
                    var alarms = string.Join("\n", _timers.List.Select(a => $"- **#{a.Number}** [{a.Seconds}s] - {a.Status} ({a.Elapsed / 1000:n3}s)"));

                    await turnContext.SendActivityAsync($"**TIMERS**\n{alarms}");
                }
                else if (text.StartsWith("conversations", StringComparison.InvariantCultureIgnoreCase))
                {
                    var conversations = string.Join("\n", _conversations.Select(c => $"- **{c.Key}**: {c.Value.ChannelId} ({c.Value.User.Id})"));

                    await turnContext.SendActivityAsync($"**CONVERSATIONS**\n{conversations}");
                }
                else
                {
                    // Echo back to the user whatever they typed.
                    await turnContext.SendActivityAsync($"You (\"**{username}**\") typed \"{text}\"");
                }
            }
            else if (activityType == ActivityTypes.Event)
            {
                var name  = turnContext.Activity.Name;
                var value = turnContext.Activity.Value;

                _logger.LogInformation("----- Receiving event activity - Name: {Name} ({Value})", name, value);

                await turnContext.SendActivityAsync($"**{name}** event detected - Payload: {value}");
            }
            else
            {
                await turnContext.SendActivityAsync($"**{turnContext.Activity.Type}** activity received");
            }
        }